mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-09-19 22:41:33 +00:00
100 lines
3.4 KiB
Python
100 lines
3.4 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
import logging
|
|
|
|
from tfw.networking import Scope
|
|
from tfw.event_handlers import FrontendEventHandlerBase
|
|
from tfw.components.inotify import InotifyObserver
|
|
from tfw.mixins.supervisor_mixin import SupervisorLogMixin
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class LogInotifyObserver(InotifyObserver, SupervisorLogMixin):
|
|
def __init__(self, server_connector, process_name, log_tail=0):
|
|
self.prevent_log_recursion()
|
|
self.server_connector = server_connector
|
|
self._process_name = process_name
|
|
self.log_tail = log_tail
|
|
self.procinfo = self.supervisor.getProcessInfo(self.process_name)
|
|
InotifyObserver.__init__(self, [self.procinfo['stdout_logfile'], self.procinfo['stderr_logfile']])
|
|
|
|
@property
|
|
def process_name(self):
|
|
return self._process_name
|
|
|
|
@process_name.setter
|
|
def process_name(self, process_name):
|
|
self._process_name = process_name
|
|
self.procinfo = self.supervisor.getProcessInfo(process_name)
|
|
self.paths = [self.procinfo['stdout_logfile'], self.procinfo['stderr_logfile']]
|
|
|
|
def on_modified(self, event):
|
|
self.server_connector.send_message({
|
|
'key': 'processlog',
|
|
'data': {
|
|
'command': 'new_log',
|
|
'stdout': self.read_stdout(self.process_name, tail=self.log_tail),
|
|
'stderr': self.read_stderr(self.process_name, tail=self.log_tail)
|
|
}
|
|
}, Scope.BROADCAST)
|
|
|
|
@staticmethod
|
|
def prevent_log_recursion():
|
|
# This is done to prevent inotify event logs triggering themselves (infinite log recursion)
|
|
logging.getLogger('watchdog.observers.inotify_buffer').propagate = False
|
|
|
|
|
|
class LogMonitoringEventHandler(FrontendEventHandlerBase):
|
|
"""
|
|
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, key, process_name, log_tail=0):
|
|
super().__init__(key)
|
|
self.process_name = process_name
|
|
self.log_tail = log_tail
|
|
self.monitor = LogInotifyObserver(self.server_connector, process_name, log_tail)
|
|
self.monitor.start()
|
|
|
|
self.command_handlers = {
|
|
'process_name': self.handle_process_name,
|
|
'log_tail': self.handle_log_tail
|
|
}
|
|
|
|
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()
|