F fix issue for lost db connection

pull/220/head
vapao 2020-10-26 12:22:33 +08:00
parent 8b146ceb3b
commit 43dc8eeca1
3 changed files with 5 additions and 4 deletions

View File

@ -1,6 +1,7 @@
# Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
# Copyright: (c) <spug.dev@gmail.com>
# 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)

View File

@ -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}')

View File

@ -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