mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-06 01:31:21 +00:00
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
import xmlrpc.client
|
|
from xmlrpc.client import Fault as SupervisorFault
|
|
from contextlib import suppress
|
|
from os import remove
|
|
|
|
from tfw.config import TFWENV
|
|
|
|
|
|
def get_supervisor_instance():
|
|
return xmlrpc.client.ServerProxy(TFWENV.SUPERVISOR_HTTP_URI).supervisor
|
|
|
|
|
|
class SupervisorBaseMixin:
|
|
supervisor = get_supervisor_instance()
|
|
|
|
def threadlocalise_supervisor_instance(self):
|
|
"""
|
|
Give this instance non-static, thread local xmlrpc client
|
|
"""
|
|
self.supervisor = get_supervisor_instance()
|
|
|
|
|
|
class SupervisorMixin(SupervisorBaseMixin):
|
|
def stop_process(self, process_name):
|
|
with suppress(SupervisorFault):
|
|
self.supervisor.stopProcess(process_name)
|
|
|
|
def start_process(self, process_name):
|
|
self.supervisor.startProcess(process_name)
|
|
|
|
def restart_process(self, process_name):
|
|
self.stop_process(process_name)
|
|
self.start_process(process_name)
|
|
|
|
|
|
class SupervisorLogMixin(SupervisorBaseMixin):
|
|
def read_stdout(self, process_name, tail=0):
|
|
return self.supervisor.readProcessStdoutLog(process_name, -tail, 0)
|
|
|
|
def read_stderr(self, process_name, tail=0):
|
|
return self.supervisor.readProcessStderrLog(process_name, -tail, 0)
|
|
|
|
def clear_logs(self, process_name):
|
|
for logfile in ('stdout_logfile', 'stderr_logfile'):
|
|
try:
|
|
remove(self.supervisor.getProcessInfo(process_name)[logfile])
|
|
except FileNotFoundError:
|
|
pass
|
|
self.supervisor.clearProcessLogs(process_name)
|