mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 05:21:33 +00:00
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
import logging
|
|
|
|
from .log_inotify_observer import LogInotifyObserver
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class ProcessLogHandler:
|
|
keys = ['logmonitor']
|
|
"""
|
|
Monitors the output of a supervisor process (stdout, stderr) and
|
|
sends the results to the frontend.
|
|
|
|
Accepts messages that have a data['command'] key specifying
|
|
a command to be executed.
|
|
|
|
The API of each command is documented in their respective handler.
|
|
"""
|
|
def __init__(self, *, process_name, supervisor_uri, log_tail=0):
|
|
self.connector = None
|
|
self.process_name = process_name
|
|
self._supervisor_uri = supervisor_uri
|
|
self._initial_log_tail = log_tail
|
|
self._monitor = None
|
|
|
|
self.command_handlers = {
|
|
'process_name': self.handle_process_name,
|
|
'log_tail': self.handle_log_tail
|
|
}
|
|
|
|
def start(self):
|
|
self._monitor = LogInotifyObserver(
|
|
connector=self.connector,
|
|
supervisor_uri=self._supervisor_uri,
|
|
process_name=self.process_name,
|
|
log_tail=self._initial_log_tail
|
|
)
|
|
self._monitor.start()
|
|
|
|
def handle_event(self, message, _):
|
|
try:
|
|
data = message['data']
|
|
self.command_handlers[data['command']](data)
|
|
except KeyError:
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
|
|
|
def handle_process_name(self, data):
|
|
"""
|
|
Changes the monitored process.
|
|
|
|
:param data: TFW message data containing 'value'
|
|
(name of the process to monitor)
|
|
"""
|
|
self._monitor.process_name = data['value']
|
|
|
|
def handle_log_tail(self, data):
|
|
"""
|
|
Sets tail length of the log the monitor will send
|
|
to the frontend (the monitor will send back the last
|
|
'value' characters of the log).
|
|
|
|
:param data: TFW message data containing 'value'
|
|
(new tail length)
|
|
"""
|
|
self._monitor.log_tail = data['value']
|
|
|
|
def cleanup(self):
|
|
self._monitor.stop()
|