baseimage-tutorial-framework/lib/tfw/components/process_managing_event_handler.py

41 lines
1.4 KiB
Python
Raw Normal View History

from xmlrpc.client import Fault as SupervisorFault
from tfw.event_handler_base import TriggerlessEventHandler
2018-02-16 11:07:16 +00:00
from tfw.components.mixins import SupervisorMixin
from tfw.components.directory_monitor import with_monitor_paused
from tfw.config.logs import logging
LOG = logging.getLogger(__name__)
2018-02-16 11:07:16 +00:00
2018-02-20 13:45:16 +00:00
class ProcessManager(SupervisorMixin):
def __init__(self):
2018-02-20 13:45:16 +00:00
self.commands = {'start': self.start_process,
'stop': self.stop_process,
'restart': self.restart_process,
'readlog': self.read_log}
2018-02-20 13:45:16 +00:00
def __call__(self, command, process_name):
return self.commands[command](process_name)
2018-02-20 13:45:16 +00:00
class ProcessManagingEventHandler(TriggerlessEventHandler):
def __init__(self, key, dirmonitor=None):
super().__init__(key)
self.key = key
self.monitor = dirmonitor
self.processmanager = ProcessManager()
2018-02-16 11:07:16 +00:00
@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