2019-07-05 13:25:59 +00:00
|
|
|
from os import remove
|
|
|
|
from contextlib import suppress
|
2018-02-13 16:51:19 +00:00
|
|
|
import xmlrpc.client
|
|
|
|
from xmlrpc.client import Fault as SupervisorFault
|
|
|
|
|
|
|
|
|
2019-07-05 13:25:59 +00:00
|
|
|
class SupervisorBase:
|
|
|
|
def __init__(self, supervisor_uri):
|
|
|
|
self.supervisor = xmlrpc.client.ServerProxy(supervisor_uri).supervisor
|
2018-02-13 16:51:19 +00:00
|
|
|
|
2018-05-28 12:32:11 +00:00
|
|
|
|
2019-07-05 13:25:59 +00:00
|
|
|
class ProcessManager(SupervisorBase):
|
2018-02-27 14:54:10 +00:00
|
|
|
def stop_process(self, process_name):
|
2018-02-13 16:51:19 +00:00
|
|
|
with suppress(SupervisorFault):
|
2018-02-27 14:54:10 +00:00
|
|
|
self.supervisor.stopProcess(process_name)
|
2018-02-13 16:51:19 +00:00
|
|
|
|
2018-02-27 14:54:10 +00:00
|
|
|
def start_process(self, process_name):
|
|
|
|
self.supervisor.startProcess(process_name)
|
2018-02-13 16:51:19 +00:00
|
|
|
|
2018-05-28 12:32:11 +00:00
|
|
|
def restart_process(self, process_name):
|
|
|
|
self.stop_process(process_name)
|
|
|
|
self.start_process(process_name)
|
|
|
|
|
|
|
|
|
2019-07-05 13:25:59 +00:00
|
|
|
class LogManager(SupervisorBase):
|
2018-05-29 15:59:16 +00:00
|
|
|
def read_stdout(self, process_name, tail=0):
|
|
|
|
return self.supervisor.readProcessStdoutLog(process_name, -tail, 0)
|
2018-05-24 09:20:00 +00:00
|
|
|
|
2018-05-29 15:59:16 +00:00
|
|
|
def read_stderr(self, process_name, tail=0):
|
|
|
|
return self.supervisor.readProcessStderrLog(process_name, -tail, 0)
|
2018-05-24 09:20:00 +00:00
|
|
|
|
|
|
|
def clear_logs(self, process_name):
|
|
|
|
for logfile in ('stdout_logfile', 'stderr_logfile'):
|
2018-05-30 12:03:59 +00:00
|
|
|
with suppress(FileNotFoundError):
|
2018-05-28 14:36:59 +00:00
|
|
|
remove(self.supervisor.getProcessInfo(process_name)[logfile])
|
2018-03-08 15:05:44 +00:00
|
|
|
self.supervisor.clearProcessLogs(process_name)
|