1
0
Fork 0

Fixing multithread issue

This commit is contained in:
Joao Gilberto Magalhaes 2022-08-24 17:38:06 -05:00
parent 50fae1c659
commit 0687ba60a2
2 changed files with 117 additions and 11 deletions

View file

@ -1,4 +1,5 @@
from datetime import datetime
from multiprocessing import Process, Lock
import subprocess
import shlex
@ -41,7 +42,7 @@ class Functions:
line = process.stdout.readline().rstrip()
output.append(line) if return_result else None
Functions.log(source, "info", line) if log_output else None
Functions.log(source, "error", process.stderr.readline())
Functions.log(source, "warning", process.stderr.readline())
return_code = process.poll()
if return_code is not None:
lines = []
@ -49,9 +50,111 @@ class Functions:
output.append(line.rstrip()) if return_result else None
lines.append(line.rstrip())
Functions.log(source, "info", lines) if log_output else None
Functions.log(source, "error", process.stderr.readlines())
Functions.log(source, "warning", process.stderr.readlines())
break
return output
except Exception as e:
Functions.log(source, 'error', "%s" % (e))
@staticmethod
def log_ts(source, level, message, lock):
if message is None or message == "":
return
if not isinstance(message, (list, tuple)):
message = [message]
lock.acquire()
try:
for line in message:
print("[%s] %s [%s]: %s" % (source, datetime.now().strftime("%x %X"), level, line.rstrip()))
finally:
lock.release()
@staticmethod
def run_bash_ts(source, command, lock):
if not isinstance(command, (list, tuple)):
command = shlex.split(command)
try:
process = subprocess.Popen(command,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=-1,
universal_newlines=True)
with process.stdout:
for line in iter(process.stdout.readline, b''):
Functions.log(source, "info", line)
returncode = process.wait()
Functions.log(source, "info", "Return code %s" % (returncode))
except Exception as e:
Functions.log_ts(source, 'error', "%s" % (e), lock)
class DaemonizeHAProxy:
def __init__(self):
self.process = None
self.thread = None
def haproxy(self, action):
if action == "start":
self.__prepare("/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /var/run/haproxy.sock")
else:
pid = "".join(Functions().run_bash("HAPROXY", "cat /run/haproxy.pid", log_output=False))
self.__prepare("/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -x /var/run/haproxy.sock -sf %s" % (pid))
if self.process is None:
return
self.thread = Process(target=self.__start, args=())
self.thread.start()
def __prepare(self, command):
source = "HAPROXY"
if not isinstance(command, (list, tuple)):
command = shlex.split(command)
try:
self.process = subprocess.Popen(command,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=-1,
universal_newlines=True)
except Exception as e:
Functions.log_ts(source, 'error', "%s" % (e), lock)
def __start(self):
source = "HAPROXY"
try:
with self.process.stdout:
for line in iter(self.process.stdout.readline, b''):
Functions.log(source, "info", line)
returncode = self.process.wait()
Functions.log(source, "info", "Return code %s" % (returncode))
except Exception as e:
Functions.log_ts(source, 'error', "%s" % (e), lock)
def is_alive(self):
return self.thread.is_alive()
def kill(self):
self.process.kill()
self.thread.kill()
def terminate(self):
self.process.terminate()
self.thread.terminate()