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]