mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 23:41:22 +00:00
42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
from functools import wraps
|
|
|
|
from tfw.event_handler_base import TriggerlessEventHandler
|
|
from tfw.components.mixins import SupervisorMixin
|
|
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
|
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}
|
|
|
|
def __call__(self, command, process_name):
|
|
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()
|
|
self.uplink = ServerUplinkConnector()
|
|
|
|
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, data_json):
|
|
data = data_json['data']
|
|
self.processmanager(data['command'], data['process_name'])
|
|
self.uplink.send(self.key, {'data': {'process_name': data['process_name']}})
|