2018-03-07 14:14:33 +00:00
|
|
|
from xmlrpc.client import Fault as SupervisorFault
|
2018-03-02 09:21:56 +00:00
|
|
|
|
2018-02-27 13:39:54 +00:00
|
|
|
from tfw.event_handler_base import TriggerlessEventHandler
|
2018-02-16 11:07:16 +00:00
|
|
|
from tfw.components.mixins import SupervisorMixin
|
2018-03-21 16:18:16 +00:00
|
|
|
from tfw.components.directory_monitor import with_monitor_paused
|
2018-02-27 13:11:13 +00:00
|
|
|
from tfw.config.logs import logging
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-02-16 11:07:16 +00:00
|
|
|
|
|
|
|
|
2018-02-20 13:45:16 +00:00
|
|
|
class ProcessManager(SupervisorMixin):
|
2018-02-27 15:51:10 +00:00
|
|
|
def __init__(self):
|
2018-02-20 13:45:16 +00:00
|
|
|
self.commands = {'start': self.start_process,
|
|
|
|
'stop': self.stop_process,
|
2018-03-08 15:04:35 +00:00
|
|
|
'restart': self.restart_process,
|
|
|
|
'readlog': self.read_log}
|
2018-02-20 13:45:16 +00:00
|
|
|
|
2018-02-27 14:54:31 +00:00
|
|
|
def __call__(self, command, process_name):
|
2018-03-08 15:04:35 +00:00
|
|
|
return self.commands[command](process_name)
|
2018-02-20 13:45:16 +00:00
|
|
|
|
|
|
|
|
2018-02-27 13:39:54 +00:00
|
|
|
class ProcessManagingEventHandler(TriggerlessEventHandler):
|
2018-03-02 09:21:56 +00:00
|
|
|
def __init__(self, key, dirmonitor=None):
|
2018-02-27 13:11:13 +00:00
|
|
|
super().__init__(key)
|
2018-02-27 14:26:16 +00:00
|
|
|
self.key = key
|
2018-03-02 09:21:56 +00:00
|
|
|
self.monitor = dirmonitor
|
2018-02-27 15:51:10 +00:00
|
|
|
self.processmanager = ProcessManager()
|
2018-02-16 11:07:16 +00:00
|
|
|
|
2018-03-21 16:18:16 +00:00
|
|
|
@with_monitor_paused
|
2018-03-08 15:11:43 +00:00
|
|
|
def handle_event(self, key, message):
|
2018-03-07 13:45:43 +00:00
|
|
|
try:
|
2018-03-08 15:11:43 +00:00
|
|
|
data = message['data']
|
2018-03-07 13:45:43 +00:00
|
|
|
self.processmanager(data['command'], data['process_name'])
|
2018-03-08 15:11:43 +00:00
|
|
|
return message
|
2018-03-07 13:45:43 +00:00
|
|
|
except KeyError:
|
2018-03-25 14:06:59 +00:00
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: {}'.format(message))
|
2018-03-07 15:35:55 +00:00
|
|
|
except SupervisorFault as fault:
|
2018-03-08 15:11:43 +00:00
|
|
|
message['data']['error'] = fault.faultString
|
|
|
|
message['data']['log'] = self.processmanager.read_log(message['data']['process_name'])
|
|
|
|
return message
|