From b6494811a06cae876b3769c8b5cc85be560d7c29 Mon Sep 17 00:00:00 2001 From: vapao Date: Mon, 16 May 2022 00:12:53 +0800 Subject: [PATCH] =?UTF-8?q?F=20=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E8=AE=A1=E5=88=92=E6=B5=8B=E8=AF=95=E6=89=A7=E8=A1=8CPython?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/schedule/executors.py | 9 +++++++-- spug_api/apps/schedule/views.py | 11 ++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/spug_api/apps/schedule/executors.py b/spug_api/apps/schedule/executors.py index c247a9a..bb3a913 100644 --- a/spug_api/apps/schedule/executors.py +++ b/spug_api/apps/schedule/executors.py @@ -37,8 +37,7 @@ def host_executor(host, command): return code, round(time.time() - now, 3), out -def schedule_worker_handler(job): - history_id, host_id, interpreter, command = json.loads(job) +def dispatch_job(host_id, interpreter, command): if interpreter == 'python': attach = 'INTERPRETER=python\ncommand -v python3 &> /dev/null && INTERPRETER=python3' command = f'{attach}\n$INTERPRETER << EOF\n# -*- coding: UTF-8 -*-\n{command}\nEOF' @@ -50,6 +49,12 @@ def schedule_worker_handler(job): code, duration, out = 1, 0, f'unknown host id for {host_id!r}' else: code, duration, out = host_executor(host, command) + return code, duration, out + + +def schedule_worker_handler(job): + history_id, host_id, interpreter, command = json.loads(job) + code, duration, out = dispatch_job(host_id, interpreter, command) close_old_connections() with transaction.atomic(): diff --git a/spug_api/apps/schedule/views.py b/spug_api/apps/schedule/views.py index 8a648bd..8bce691 100644 --- a/spug_api/apps/schedule/views.py +++ b/spug_api/apps/schedule/views.py @@ -7,7 +7,7 @@ from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.triggers.cron import CronTrigger from apps.schedule.scheduler import Scheduler from apps.schedule.models import Task, History -from apps.schedule.executors import local_executor, host_executor +from apps.schedule.executors import dispatch_job from apps.host.models import Host from django.conf import settings from libs import json_response, JsonParser, Argument, human_datetime, auth @@ -121,14 +121,7 @@ class HistoryView(View): return json_response(error='未找到指定任务') outputs, status = {}, 1 for host_id in json.loads(task.targets): - if host_id == 'local': - code, duration, out = local_executor(task.command) - else: - host = Host.objects.filter(pk=host_id).first() - if not host: - code, duration, out = 1, 0, f'unknown host id for {host_id!r}' - else: - code, duration, out = host_executor(host, task.command) + code, duration, out = dispatch_job(host_id, task.interpreter, task.command) if code != 0: status = 2 outputs[host_id] = [code, duration, out]