mirror of https://github.com/openspug/spug
F fix issue for lost db connection
parent
8b146ceb3b
commit
43dc8eeca1
|
@ -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)
|
||||
|
|
|
@ -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}')
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue