diff --git a/spug_api/apps/monitor/executors.py b/spug_api/apps/monitor/executors.py index d88eb02..3c60a94 100644 --- a/spug_api/apps/monitor/executors.py +++ b/spug_api/apps/monitor/executors.py @@ -1,6 +1,7 @@ # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug # Copyright: (c) # Released under the AGPL-3.0 License. +from django.db import close_old_connections from apps.host.models import Host from socket import socket import subprocess @@ -70,5 +71,6 @@ 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/schedule/executors.py b/spug_api/apps/schedule/executors.py index c78b4b8..c5fb5b4 100644 --- a/spug_api/apps/schedule/executors.py +++ b/spug_api/apps/schedule/executors.py @@ -35,14 +35,13 @@ def host_executor(q, host, command): q.put((host.id, exit_code, round(time.time() - now, 3), out)) -def dispatch(command, targets, in_view=False): - if not in_view: - close_old_connections() +def dispatch(command, targets): 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 022dbfa..6c3a831 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), True) + data = dispatch(task.command, json.loads(task.targets)) score = 0 for item in data: score += 1 if item[1] else 0