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

29 lines
1.0 KiB
Python

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, supervisor_process_name):
self.process_name = supervisor_process_name
self.commands = {'start': self.start_process,
'stop': self.stop_process,
'restart': self.restart_process}
def __call__(self, command):
self.commands[command]()
class ProcessManagingEventHandler(TriggerlessEventHandler):
def __init__(self, key, supervisor_process_name):
super().__init__(key)
self.key = key
self.processmanager = ProcessManager(supervisor_process_name)
self.uplink = ServerUplinkConnector()
def handle_event(self, key, data_json):
self.processmanager(data_json['data']['command'])
self.uplink.send(self.key, {'key': self.key})