diff --git a/spug_api/apps/schedule/executors.py b/spug_api/apps/schedule/executors.py index e2aab19..495e583 100644 --- a/spug_api/apps/schedule/executors.py +++ b/spug_api/apps/schedule/executors.py @@ -14,7 +14,7 @@ def local_executor(q, command): exit_code = task.wait() out = task.stdout.read() + task.stderr.read() finally: - q.put(('local', exit_code, time.time() - now, out.decode())) + q.put(('local', exit_code, round(time.time() - now, 3), out.decode())) def host_executor(q, host, pkey, command): @@ -23,7 +23,7 @@ def host_executor(q, host, pkey, command): cli = SSH(host.hostname, host.port, host.username, pkey=pkey) exit_code, out = cli.exec_command(command) finally: - q.put((host.id, exit_code, time.time() - now, out.decode())) + q.put((host.id, exit_code, round(time.time() - now, 3), out.decode())) def dispatch(command, targets): diff --git a/spug_api/apps/schedule/scheduler.py b/spug_api/apps/schedule/scheduler.py index 5ee92da..f5e67de 100644 --- a/spug_api/apps/schedule/scheduler.py +++ b/spug_api/apps/schedule/scheduler.py @@ -30,6 +30,7 @@ class Scheduler: raise TypeError(f'unknown schedule policy: {trigger!r}') def _handle_event(self, event): + # TODO: notify to user if event.code == events.EVENT_SCHEDULER_SHUTDOWN: logger.info(f'EVENT_SCHEDULER_SHUTDOWN: {event}') if event.code == events.EVENT_JOB_MAX_INSTANCES: @@ -67,7 +68,6 @@ class Scheduler: while True: _, data = rds_cli.blpop(settings.SCHEDULE_KEY) task = AttrDict(json.loads(data)) - print(f'queue: {task!r}') if task.action in ('add', 'modify'): trigger = self.parse_trigger(task.trigger, task.trigger_args) self.scheduler.add_job( diff --git a/spug_api/apps/schedule/urls.py b/spug_api/apps/schedule/urls.py index 99690a9..160da03 100644 --- a/spug_api/apps/schedule/urls.py +++ b/spug_api/apps/schedule/urls.py @@ -1,7 +1,9 @@ from django.conf.urls import url +from django.urls import path from .views import * urlpatterns = [ url(r'^$', Schedule.as_view()), + path('/', ScheduleInfo.as_view()), ] diff --git a/spug_api/apps/schedule/views.py b/spug_api/apps/schedule/views.py index b8f8108..bc66bbf 100644 --- a/spug_api/apps/schedule/views.py +++ b/spug_api/apps/schedule/views.py @@ -1,6 +1,7 @@ from django.views.generic import View from django_redis import get_redis_connection from apps.schedule.models import Task +from apps.host.models import Host from django.conf import settings from libs import json_response, JsonParser, Argument, human_time import json @@ -32,6 +33,7 @@ class Schedule(View): **form ) form.action = 'modify' + form.targets = json.loads(form.targets) rds_cli = get_redis_connection() rds_cli.rpush(settings.SCHEDULE_KEY, json.dumps(form)) else: @@ -67,3 +69,23 @@ class Schedule(View): return json_response(error='该任务在运行中,请先停止任务再尝试删除') task.delete() return json_response(error=error) + + +class ScheduleInfo(View): + def get(self, request, t_id): + task = Task.objects.filter(pk=t_id).first() + outputs = json.loads(task.latest_output) + host_ids = (x[0] for x in outputs if isinstance(x[0], int)) + hosts_info = {x.id: x.name for x in Host.objects.filter(id__in=host_ids)} + data = {'run_time': task.latest_run_time, 'success': 0, 'failure': 0, 'duration': 0, 'outputs': []} + for h_id, code, duration, out in outputs: + key = 'success' if code == 0 else 'failure' + data[key] += 1 + data['duration'] += duration + data['outputs'].append({ + 'name': hosts_info.get(h_id, '本机'), + 'code': code, + 'duration': duration, + 'output': out}) + data['duration'] = f"{data['duration'] / len(outputs):.3f}" + return json_response(data)