From e32d51253a205cd012488c3f0fffb99502092c4b Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Fri, 23 Dec 2022 18:23:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=B9=E9=87=8F=E5=91=BD=E4=BB=A4api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/api/job.py | 20 +++++++++++++++++++- apps/ops/urls/api_urls.py | 1 + apps/ops/ws.py | 3 ++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/apps/ops/api/job.py b/apps/ops/api/job.py index 0f37732bd..0cf8ade0c 100644 --- a/apps/ops/api/job.py +++ b/apps/ops/api/job.py @@ -5,11 +5,12 @@ from rest_framework.response import Response from ops.models import Job, JobExecution from ops.serializers.job import JobSerializer, JobExecutionSerializer -__all__ = ['JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView', 'JobAssetDetail', ] +__all__ = ['JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView', 'JobAssetDetail', 'JobExecutionTaskDetail'] from ops.tasks import run_ops_job_execution from ops.variables import JMS_JOB_VARIABLE_HELP from orgs.mixins.api import OrgBulkModelViewSet +from orgs.utils import tmp_to_org, get_current_org_id, get_current_org def set_task_to_serializer_data(serializer, task): @@ -93,3 +94,20 @@ class JobAssetDetail(APIView): if execution_id: execution = get_object_or_404(JobExecution, id=execution_id) return Response(data=execution.assent_result_detail) + + +class JobExecutionTaskDetail(APIView): + rbac_perms = () + permission_classes = () + + def get(self, request, **kwargs): + org = get_current_org() + task_id = request.query_params.get('task_id') + if task_id: + with tmp_to_org(org): + execution = get_object_or_404(JobExecution, task_id=task_id) + return Response(data={ + 'is_finished': execution.is_finished, + 'is_success': execution.is_success, + 'time_cost': execution.time_cost, + }) diff --git a/apps/ops/urls/api_urls.py b/apps/ops/urls/api_urls.py index 6fad435bf..20f581b1d 100644 --- a/apps/ops/urls/api_urls.py +++ b/apps/ops/urls/api_urls.py @@ -25,6 +25,7 @@ router.register(r'task-executions', api.CeleryTaskExecutionViewSet, 'task-execut urlpatterns = [ path('variables/help/', api.JobRunVariableHelpAPIView.as_view(), name='variable-help'), path('job-execution/asset-detail/', api.JobAssetDetail.as_view(), name='asset-detail'), + path('job-execution/task-detail/', api.JobExecutionTaskDetail.as_view(), name='task-detail'), path('ansible/job-execution//log/', api.AnsibleTaskLogApi.as_view(), name='job-execution-log'), path('celery/task//task-execution//log/', api.CeleryTaskExecutionLogApi.as_view(), diff --git a/apps/ops/ws.py b/apps/ops/ws.py index 24023a33e..5c261f76f 100644 --- a/apps/ops/ws.py +++ b/apps/ops/ws.py @@ -67,11 +67,12 @@ class TaskLogWebsocket(AsyncJsonWebsocketConsumer): task_end_mark.append(1) elif len(task_end_mark) == 2: logger.debug('Task log end: {}'.format(task_id)) + await self.send_json({'event': 'end', 'task': task_id}) break await asyncio.sleep(0.2) except OSError as e: logger.warn('Task log path open failed: {}'.format(e)) - await self.close() + # await self.close() async def disconnect(self, close_code): self.disconnected = True