From 4daadbfa0a172277cce2ff215aa62cadda1e241d Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Fri, 10 Feb 2023 18:37:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=20=E4=BF=AE=E5=A4=8D=20JOB=20=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E4=B8=8D=E6=89=A7=E8=A1=8C=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/api/job.py | 2 +- apps/ops/models/job.py | 4 ++-- apps/ops/tasks.py | 23 ++++++++++++----------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/apps/ops/api/job.py b/apps/ops/api/job.py index cbb88360a..ae9748964 100644 --- a/apps/ops/api/job.py +++ b/apps/ops/api/job.py @@ -72,7 +72,7 @@ class JobExecutionViewSet(OrgBulkModelViewSet): instance = serializer.save() instance.job_version = instance.job.version instance.material = instance.job.material - instance.type = Types[instance.job.type].value + instance.job_type = Types[instance.job.type].value instance.creator = self.request.user instance.save() task = run_ops_job_execution.delay(instance.id) diff --git a/apps/ops/models/job.py b/apps/ops/models/job.py index 529e1adda..ebc6b6dca 100644 --- a/apps/ops/models/job.py +++ b/apps/ops/models/job.py @@ -73,9 +73,9 @@ class Job(JMSOrgBaseModel, PeriodTaskModelMixin): return total_cost / finished_count if finished_count else 0 def get_register_task(self): - from ..tasks import run_ops_job_execution + from ..tasks import run_ops_job name = "run_ops_job_period_{}".format(str(self.id)[:8]) - task = run_ops_job_execution.name + task = run_ops_job.name args = (str(self.id),) kwargs = {} return name, task, args, kwargs diff --git a/apps/ops/tasks.py b/apps/ops/tasks.py index 4ba139577..c8688eb9f 100644 --- a/apps/ops/tasks.py +++ b/apps/ops/tasks.py @@ -27,17 +27,18 @@ logger = get_logger(__file__) @shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task")) def run_ops_job(job_id): job = get_object_or_none(Job, id=job_id) - execution = job.create_execution() - run_ops_job_execution(execution) - - -# -# @shared_task(soft_time_limit=60, queue="ansible") -# def show_env(): -# import json -# print(os.environ) -# data = json.dumps(dict(os.environ), indent=4) -# return data + with tmp_to_org(job.org): + execution = job.create_execution() + execution.creator = job.creator + run_ops_job_execution(execution.id) + try: + execution.start() + except SoftTimeLimitExceeded: + execution.set_error('Run timeout') + logger.error("Run adhoc timeout") + except Exception as e: + execution.set_error(e) + logger.error("Start adhoc execution error: {}".format(e)) @shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task execution"))