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

68 lines
2.1 KiB
Python
Raw Normal View History

# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from tfw import EventHandlerBase
from tfw.mixins import MonitorManagerMixin
2018-05-30 13:21:36 +00:00
from tfw.config.logs import logging
from .log_monitor import LogMonitor
2018-05-30 13:21:36 +00:00
LOG = logging.getLogger(__name__)
class LogMonitoringEventHandler(EventHandlerBase, MonitorManagerMixin):
2018-05-31 12:03:11 +00:00
"""
Monitors the output of a supervisor process (stdout, stderr) and
sends the results to the frontend.
2018-06-01 14:20:20 +00:00
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.
2018-05-31 12:03:11 +00:00
"""
2018-05-30 13:21:36 +00:00
def __init__(self, key, process_name, log_tail=0):
super().__init__(key)
self.process_name = process_name
self.log_tail = log_tail
MonitorManagerMixin.__init__(
self,
LogMonitor,
self.process_name,
self.log_tail
)
2018-05-30 13:21:36 +00:00
self.command_handlers = {
'process_name': self.handle_process_name,
'log_tail': self.handle_log_tail
}
def handle_event(self, message):
2018-05-30 13:21:36 +00:00
try:
data = message['data']
self.command_handlers[data['command']](data)
self.reload_monitor()
except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
def handle_process_name(self, data):
2018-05-31 12:03:11 +00:00
"""
Changes the monitored process.
2018-06-01 14:20:20 +00:00
:param data: TFW message data containing 'value'
(name of the process to monitor)
2018-05-31 12:03:11 +00:00
"""
self.set_monitor_args(data['value'], self.log_tail)
2018-05-30 13:21:36 +00:00
def handle_log_tail(self, data):
2018-05-31 12:03:11 +00:00
"""
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).
2018-06-01 14:20:20 +00:00
:param data: TFW message data containing 'value'
(new tail length)
2018-05-31 12:03:11 +00:00
"""
self.set_monitor_args(self.process_name, data['value'])
def cleanup(self):
self.monitor.stop()