mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 07:41:32 +00:00
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
from functools import wraps
|
|
from xmlrpc.client import Fault as SupervisorFault
|
|
|
|
from tfw.event_handler_base import TriggerlessEventHandler
|
|
from tfw.components.mixins import SupervisorMixin
|
|
from tfw.config.logs import logging
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class ProcessManager(SupervisorMixin):
|
|
def __init__(self):
|
|
self.commands = {'start': self.start_process,
|
|
'stop': self.stop_process,
|
|
'restart': self.restart_process,
|
|
'readlog': self.read_log}
|
|
|
|
def __call__(self, command, process_name):
|
|
return self.commands[command](process_name)
|
|
|
|
|
|
class ProcessManagingEventHandler(TriggerlessEventHandler):
|
|
def __init__(self, key, dirmonitor=None):
|
|
super().__init__(key)
|
|
self.key = key
|
|
self.monitor = dirmonitor
|
|
self.processmanager = ProcessManager()
|
|
|
|
def _with_monitor_paused(fun):
|
|
@wraps(fun)
|
|
def wrapper(self, *args, **kwargs):
|
|
if self.monitor:
|
|
with self.monitor.pauser:
|
|
return fun(self, *args, **kwargs)
|
|
return fun(self, *args, **kwargs)
|
|
return wrapper
|
|
|
|
@_with_monitor_paused
|
|
def handle_event(self, key, message):
|
|
try:
|
|
data = message['data']
|
|
self.processmanager(data['command'], data['process_name'])
|
|
return message
|
|
except KeyError:
|
|
log.error('IGNORING MESSAGE: Invalid message received: {}'.format(message))
|
|
except SupervisorFault as fault:
|
|
message['data']['error'] = fault.faultString
|
|
message['data']['log'] = self.processmanager.read_log(message['data']['process_name'])
|
|
return message
|