# Copyright (c) 2022, JINGROW
# For license information, please see license.txt

import json

import jingrow
from jingrow.model.page import Page
from jingrow.utils.safe_exec import safe_exec


class JcloudJobStep(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.types import DF

		attempts: DF.Int
		duration: DF.Duration | None
		end: DF.Datetime | None
		job: DF.Link
		job_type: DF.Link
		name: DF.Int | None
		result: DF.Code | None
		start: DF.Datetime | None
		status: DF.Literal["Pending", "Running", "Skipped", "Success", "Failure"]
		step_name: DF.Data
		traceback: DF.Code | None
		wait_until_true: DF.Check
	# end: auto-generated types

	@jingrow.whitelist()
	def execute(self):
		if not self.start:
			self.start = jingrow.utils.now_datetime()
		self.status = "Running"
		script = jingrow.db.get_value(
			"Jcloud Job Type Step",
			{"parent": self.job_type, "step_name": self.step_name},
			"script",
		)
		job = jingrow.get_pg("Jcloud Job", self.job)
		arguments = json.loads(job.arguments)
		try:
			local = {"arguments": jingrow._dict(arguments), "result": None, "pg": job}
			safe_exec(script, _locals=local)
			result = local["result"]

			if self.wait_until_true:
				self.attempts = self.attempts + 1
				if result[0]:
					self.status = "Success"
				elif result[1]:
					self.status = "Failure"
				else:
					self.status = "Pending"
					import time

					time.sleep(1)
			else:
				self.status = "Success"
			self.result = str(result)
		except Exception:
			self.status = "Failure"
			self.traceback = jingrow.get_traceback(with_context=True)

		self.end = jingrow.utils.now_datetime()
		self.duration = (self.end - self.start).total_seconds()
		self.save()

		if self.status == "Failure":
			job.fail(local["arguments"])
		else:
			job.next(local["arguments"])
