From 94b5d8b9e99763840d9ae01cd1e4a3f520f1dbc7 Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Thu, 11 Apr 2024 11:41:07 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=A4=84=E7=90=86=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=BC=82=E5=B8=B8=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/api/job.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/apps/ops/api/job.py b/apps/ops/api/job.py index c65392e06..55c342c00 100644 --- a/apps/ops/api/job.py +++ b/apps/ops/api/job.py @@ -237,18 +237,21 @@ class JobExecutionViewSet(OrgBulkModelViewSet): {'error': _('The task is being created and cannot be interrupted. Please try again later.')}, status=400 ) - - task = AsyncResult(task_id, app=app) - inspect = app.control.inspect() - - for worker in inspect.registered().keys(): - if not worker.startswith('ansible'): - continue - if task_id not in [at['id'] for at in inspect.active().get(worker, [])]: - # 在队列中未执行使用revoke执行 - task.revoke(terminate=True) - instance.set_error('Job stop by "revoke task {}"'.format(task_id)) - return Response({'task_id': task_id}, status=200) + try: + task = AsyncResult(task_id, app=app) + inspect = app.control.inspect() + + for worker in inspect.registered().keys(): + if not worker.startswith('ansible'): + continue + if task_id not in [at['id'] for at in inspect.active().get(worker, [])]: + # 在队列中未执行使用revoke执行 + task.revoke(terminate=True) + instance.set_error('Job stop by "revoke task {}"'.format(task_id)) + return Response({'task_id': task_id}, status=200) + except Exception as e: + instance.set_error(str(e)) + return Response({'error': f'Error while stopping the task {task_id}: {e}'}, status=400) instance.stop() return Response({'task_id': task_id}, status=200)