mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-15 05:47:16 +00:00
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
import logging
|
|
from xmlrpc.client import Fault as SupervisorFault
|
|
|
|
from tfw.internals.networking import Scope
|
|
|
|
from .supervisor import ProcessManager, ProcessLogManager
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class ProcessHandler(ProcessManager, ProcessLogManager):
|
|
keys = ['process']
|
|
|
|
def __init__(self, *, supervisor_uri, log_tail=0):
|
|
ProcessManager.__init__(self, supervisor_uri)
|
|
ProcessLogManager.__init__(self, supervisor_uri)
|
|
self.log_tail = log_tail
|
|
|
|
self.commands = {
|
|
'process.start': self.start_process,
|
|
'process.stop': self.stop_process,
|
|
'process.restart': self.restart_process
|
|
}
|
|
|
|
def handle_event(self, message, connector):
|
|
try:
|
|
try:
|
|
self.commands[message['key']](message['name'])
|
|
except SupervisorFault as fault:
|
|
message['error'] = fault.faultString
|
|
finally:
|
|
message['stdout'] = self.read_stdout(
|
|
message['name'],
|
|
self.log_tail
|
|
)
|
|
message['stderr'] = self.read_stderr(
|
|
message['name'],
|
|
self.log_tail
|
|
)
|
|
connector.send_message(message, scope=Scope.WEBSOCKET)
|
|
except KeyError:
|
|
if not message['key'].startswith('process.log'):
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|