From beb4f14be9695d43206be4e4ac18045acd2d32b6 Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Fri, 15 Sep 2023 11:25:02 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20jobexecution=20?= =?UTF-8?q?=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/api/job.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/apps/ops/api/job.py b/apps/ops/api/job.py index 6fac1bc95..f2bf92cae 100644 --- a/apps/ops/api/job.py +++ b/apps/ops/api/job.py @@ -1,4 +1,5 @@ from django.conf import settings +from django.db import transaction from django.db.models import Count from django.db.transaction import atomic from django.shortcuts import get_object_or_404 @@ -25,9 +26,9 @@ from perms.models import PermNode from perms.utils import UserPermAssetUtil -def set_task_to_serializer_data(serializer, task): +def set_task_to_serializer_data(serializer, task_id): data = getattr(serializer, "_data", {}) - data["task_id"] = task.id + data["task_id"] = task_id setattr(serializer, "_data", data) @@ -85,8 +86,10 @@ class JobViewSet(OrgBulkModelViewSet): execution = job.create_execution() execution.creator = self.request.user execution.save() - task = run_ops_job_execution.delay(str(execution.id)) - set_task_to_serializer_data(serializer, task) + + set_task_to_serializer_data(serializer, execution.id) + transaction.on_commit( + lambda: run_ops_job_execution.apply_async((str(execution.id),), task_id=str(execution.id))) class JobExecutionViewSet(OrgBulkModelViewSet): @@ -96,7 +99,10 @@ class JobExecutionViewSet(OrgBulkModelViewSet): search_fields = ('material',) filterset_fields = ['status', 'job_id'] - @atomic + @staticmethod + def start_deploy(instance, serializer): + task = run_ops_job_execution.apply_async((str(instance.id),), task_id=str(instance.id)) + def perform_create(self, serializer): instance = serializer.save() instance.job_version = instance.job.version @@ -104,8 +110,10 @@ class JobExecutionViewSet(OrgBulkModelViewSet): instance.job_type = Types[instance.job.type].value instance.creator = self.request.user instance.save() - task = run_ops_job_execution.delay(str(instance.id)) - set_task_to_serializer_data(serializer, task) + + set_task_to_serializer_data(serializer, instance.id) + transaction.on_commit( + lambda: run_ops_job_execution.apply_async((str(instance.id),), task_id=str(instance.id))) def get_queryset(self): queryset = super().get_queryset()