From 79ef2c46a99728e1dc4b06b732b4a9a18f7987c6 Mon Sep 17 00:00:00 2001 From: vapao Date: Wed, 28 Oct 2020 17:38:58 +0800 Subject: [PATCH] F fix issue --- spug_api/apps/monitor/executors.py | 5 +++-- spug_api/apps/monitor/views.py | 2 +- spug_api/apps/schedule/executors.py | 5 +++-- spug_api/apps/schedule/views.py | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/spug_api/apps/monitor/executors.py b/spug_api/apps/monitor/executors.py index 3c60a94..46998ba 100644 --- a/spug_api/apps/monitor/executors.py +++ b/spug_api/apps/monitor/executors.py @@ -58,7 +58,9 @@ def host_executor(host, command): return False, f'异常信息:{e}' -def dispatch(tp, addr, extra): +def dispatch(tp, addr, extra, in_view=False): + if not in_view: + close_old_connections() if tp == '1': return site_check(addr) elif tp == '2': @@ -71,6 +73,5 @@ def dispatch(tp, addr, extra): command = extra else: raise TypeError(f'invalid monitor type: {tp!r}') - close_old_connections() host = Host.objects.filter(pk=addr).first() return host_executor(host, command) diff --git a/spug_api/apps/monitor/views.py b/spug_api/apps/monitor/views.py index 40d9d2f..c072b2e 100644 --- a/spug_api/apps/monitor/views.py +++ b/spug_api/apps/monitor/views.py @@ -88,6 +88,6 @@ def run_test(request): Argument('extra', required=False) ).parse(request.body) if error is None: - is_success, message = dispatch(form.type, form.addr, form.extra) + is_success, message = dispatch(form.type, form.addr, form.extra, True) return json_response({'is_success': is_success, 'message': message}) return json_response(error=error) diff --git a/spug_api/apps/schedule/executors.py b/spug_api/apps/schedule/executors.py index c5fb5b4..c78b4b8 100644 --- a/spug_api/apps/schedule/executors.py +++ b/spug_api/apps/schedule/executors.py @@ -35,13 +35,14 @@ def host_executor(q, host, command): q.put((host.id, exit_code, round(time.time() - now, 3), out)) -def dispatch(command, targets): +def dispatch(command, targets, in_view=False): + if not in_view: + close_old_connections() threads, q = [], Queue() for t in targets: if t == 'local': threads.append(Thread(target=local_executor, args=(q, command))) elif isinstance(t, int): - close_old_connections() host = Host.objects.filter(pk=t).first() if not host: raise ValueError(f'unknown host id: {t!r}') diff --git a/spug_api/apps/schedule/views.py b/spug_api/apps/schedule/views.py index 6c3a831..022dbfa 100644 --- a/spug_api/apps/schedule/views.py +++ b/spug_api/apps/schedule/views.py @@ -110,7 +110,7 @@ class HistoryView(View): task = Task.objects.filter(pk=t_id).first() if not task: return json_response(error='未找到指定任务') - data = dispatch(task.command, json.loads(task.targets)) + data = dispatch(task.command, json.loads(task.targets), True) score = 0 for item in data: score += 1 if item[1] else 0