F 修复任务计划测试执行Python脚本报错的问题

pull/494/head
vapao 2022-05-16 00:12:53 +08:00
parent 3330f5e0a9
commit b6494811a0
2 changed files with 9 additions and 11 deletions

View File

@ -37,8 +37,7 @@ def host_executor(host, command):
return code, round(time.time() - now, 3), out return code, round(time.time() - now, 3), out
def schedule_worker_handler(job): def dispatch_job(host_id, interpreter, command):
history_id, host_id, interpreter, command = json.loads(job)
if interpreter == 'python': if interpreter == 'python':
attach = 'INTERPRETER=python\ncommand -v python3 &> /dev/null && INTERPRETER=python3' attach = 'INTERPRETER=python\ncommand -v python3 &> /dev/null && INTERPRETER=python3'
command = f'{attach}\n$INTERPRETER << EOF\n# -*- coding: UTF-8 -*-\n{command}\nEOF' 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}' code, duration, out = 1, 0, f'unknown host id for {host_id!r}'
else: else:
code, duration, out = host_executor(host, command) 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() close_old_connections()
with transaction.atomic(): with transaction.atomic():

View File

@ -7,7 +7,7 @@ from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger from apscheduler.triggers.cron import CronTrigger
from apps.schedule.scheduler import Scheduler from apps.schedule.scheduler import Scheduler
from apps.schedule.models import Task, History 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 apps.host.models import Host
from django.conf import settings from django.conf import settings
from libs import json_response, JsonParser, Argument, human_datetime, auth from libs import json_response, JsonParser, Argument, human_datetime, auth
@ -121,14 +121,7 @@ class HistoryView(View):
return json_response(error='未找到指定任务') return json_response(error='未找到指定任务')
outputs, status = {}, 1 outputs, status = {}, 1
for host_id in json.loads(task.targets): for host_id in json.loads(task.targets):
if host_id == 'local': code, duration, out = dispatch_job(host_id, task.interpreter, task.command)
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)
if code != 0: if code != 0:
status = 2 status = 2
outputs[host_id] = [code, duration, out] outputs[host_id] = [code, duration, out]