Skip to content

Commit b649481

Browse files
committed
F 修复任务计划测试执行Python脚本报错的问题
1 parent 3330f5e commit b649481

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

spug_api/apps/schedule/executors.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def host_executor(host, command):
3737
return code, round(time.time() - now, 3), out
3838

3939

40-
def schedule_worker_handler(job):
41-
history_id, host_id, interpreter, command = json.loads(job)
40+
def dispatch_job(host_id, interpreter, command):
4241
if interpreter == 'python':
4342
attach = 'INTERPRETER=python\ncommand -v python3 &> /dev/null && INTERPRETER=python3'
4443
command = f'{attach}\n$INTERPRETER << EOF\n# -*- coding: UTF-8 -*-\n{command}\nEOF'
@@ -50,6 +49,12 @@ def schedule_worker_handler(job):
5049
code, duration, out = 1, 0, f'unknown host id for {host_id!r}'
5150
else:
5251
code, duration, out = host_executor(host, command)
52+
return code, duration, out
53+
54+
55+
def schedule_worker_handler(job):
56+
history_id, host_id, interpreter, command = json.loads(job)
57+
code, duration, out = dispatch_job(host_id, interpreter, command)
5358

5459
close_old_connections()
5560
with transaction.atomic():

spug_api/apps/schedule/views.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from apscheduler.triggers.cron import CronTrigger
88
from apps.schedule.scheduler import Scheduler
99
from apps.schedule.models import Task, History
10-
from apps.schedule.executors import local_executor, host_executor
10+
from apps.schedule.executors import dispatch_job
1111
from apps.host.models import Host
1212
from django.conf import settings
1313
from libs import json_response, JsonParser, Argument, human_datetime, auth
@@ -121,14 +121,7 @@ def post(self, request, t_id):
121121
return json_response(error='未找到指定任务')
122122
outputs, status = {}, 1
123123
for host_id in json.loads(task.targets):
124-
if host_id == 'local':
125-
code, duration, out = local_executor(task.command)
126-
else:
127-
host = Host.objects.filter(pk=host_id).first()
128-
if not host:
129-
code, duration, out = 1, 0, f'unknown host id for {host_id!r}'
130-
else:
131-
code, duration, out = host_executor(host, task.command)
124+
code, duration, out = dispatch_job(host_id, task.interpreter, task.command)
132125
if code != 0:
133126
status = 2
134127
outputs[host_id] = [code, duration, out]

0 commit comments

Comments
 (0)