mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-09 01:27:17 +00:00
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
import logging
|
|
from xmlrpc.client import Fault as SupervisorFault
|
|
|
|
from tfw.event_handlers import FrontendEventHandlerBase
|
|
from tfw.mixins.supervisor_mixin import SupervisorMixin, SupervisorLogMixin
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class ProcessManager(SupervisorMixin, SupervisorLogMixin):
|
|
def __init__(self):
|
|
self.commands = {
|
|
'start': self.start_process,
|
|
'stop': self.stop_process,
|
|
'restart': self.restart_process
|
|
}
|
|
|
|
def __call__(self, command, process_name):
|
|
return self.commands[command](process_name)
|
|
|
|
|
|
class ProcessManagingEventHandler(FrontendEventHandlerBase):
|
|
"""
|
|
Event handler that can manage processes managed by supervisor.
|
|
|
|
This EventHandler accepts messages that have a data['command'] key specifying
|
|
a command to be executed.
|
|
Every message must contain a data['process_name'] field with the name of the
|
|
process to manage. This is the name specified in supervisor config files like so:
|
|
[program:someprogram]
|
|
|
|
Commands available: start, stop, restart, readlog
|
|
(the names are as self-documenting as it gets)
|
|
"""
|
|
def __init__(self, key, log_tail=0):
|
|
super().__init__(key)
|
|
self.processmanager = ProcessManager()
|
|
self.log_tail = log_tail
|
|
|
|
def handle_event(self, message):
|
|
try:
|
|
data = message['data']
|
|
try:
|
|
self.processmanager(data['command'], data['process_name'])
|
|
except SupervisorFault as fault:
|
|
message['data']['error'] = fault.faultString
|
|
finally:
|
|
message['data']['stdout'] = self.processmanager.read_stdout(
|
|
data['process_name'],
|
|
self.log_tail
|
|
)
|
|
message['data']['stderr'] = self.processmanager.read_stderr(
|
|
data['process_name'],
|
|
self.log_tail
|
|
)
|
|
return message
|
|
except KeyError:
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|