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) OpenSpug Organization. https://github.com/openspug/spug
|
||||||
# Copyright: (c) <spug.dev@gmail.com>
|
# Copyright: (c) <spug.dev@gmail.com>
|
||||||
# Released under the AGPL-3.0 License.
|
# Released under the AGPL-3.0 License.
|
||||||
|
from django.db import close_old_connections
|
||||||
from apps.host.models import Host
|
from apps.host.models import Host
|
||||||
from socket import socket
|
from socket import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -70,5 +71,6 @@ def dispatch(tp, addr, extra):
|
||||||
command = extra
|
command = extra
|
||||||
else:
|
else:
|
||||||
raise TypeError(f'invalid monitor type: {tp!r}')
|
raise TypeError(f'invalid monitor type: {tp!r}')
|
||||||
|
close_old_connections()
|
||||||
host = Host.objects.filter(pk=addr).first()
|
host = Host.objects.filter(pk=addr).first()
|
||||||
return host_executor(host, command)
|
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))
|
q.put((host.id, exit_code, round(time.time() - now, 3), out))
|
||||||
|
|
||||||
|
|
||||||
def dispatch(command, targets, in_view=False):
|
def dispatch(command, targets):
|
||||||
if not in_view:
|
|
||||||
close_old_connections()
|
|
||||||
threads, q = [], Queue()
|
threads, q = [], Queue()
|
||||||
for t in targets:
|
for t in targets:
|
||||||
if t == 'local':
|
if t == 'local':
|
||||||
threads.append(Thread(target=local_executor, args=(q, command)))
|
threads.append(Thread(target=local_executor, args=(q, command)))
|
||||||
elif isinstance(t, int):
|
elif isinstance(t, int):
|
||||||
|
close_old_connections()
|
||||||
host = Host.objects.filter(pk=t).first()
|
host = Host.objects.filter(pk=t).first()
|
||||||
if not host:
|
if not host:
|
||||||
raise ValueError(f'unknown host id: {t!r}')
|
raise ValueError(f'unknown host id: {t!r}')
|
||||||
|
|
|
@ -110,7 +110,7 @@ class HistoryView(View):
|
||||||
task = Task.objects.filter(pk=t_id).first()
|
task = Task.objects.filter(pk=t_id).first()
|
||||||
if not task:
|
if not task:
|
||||||
return json_response(error='未找到指定任务')
|
return json_response(error='未找到指定任务')
|
||||||
data = dispatch(task.command, json.loads(task.targets), True)
|
data = dispatch(task.command, json.loads(task.targets))
|
||||||
score = 0
|
score = 0
|
||||||
for item in data:
|
for item in data:
|
||||||
score += 1 if item[1] else 0
|
score += 1 if item[1] else 0
|
||||||
|
|
Loading…
Reference in New Issue