2018-05-30 11:14:25 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
|
|
|
|
2019-06-24 12:29:31 +00:00
|
|
|
from tfw.networking import Scope
|
2019-06-27 12:36:18 +00:00
|
|
|
from tfw.event_handlers import FrontendEventHandlerBase
|
2019-06-24 12:29:31 +00:00
|
|
|
from tfw.components.inotify import InotifyObserver
|
|
|
|
from tfw.mixins.supervisor_mixin import SupervisorLogMixin
|
2018-05-30 11:14:25 +00:00
|
|
|
|
2018-05-30 13:21:36 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2018-05-30 11:14:25 +00:00
|
|
|
|
2019-06-24 12:29:31 +00:00
|
|
|
class LogInotifyObserver(InotifyObserver, SupervisorLogMixin):
|
2019-06-27 12:12:01 +00:00
|
|
|
def __init__(self, server_connector, process_name, log_tail=0):
|
2019-06-24 12:29:31 +00:00
|
|
|
self.prevent_log_recursion()
|
2019-06-27 12:12:01 +00:00
|
|
|
self.server_connector = server_connector
|
|
|
|
self._process_name = process_name
|
2019-06-24 12:29:31 +00:00
|
|
|
self.log_tail = log_tail
|
|
|
|
self.procinfo = self.supervisor.getProcessInfo(self.process_name)
|
2019-06-27 12:12:01 +00:00
|
|
|
InotifyObserver.__init__(self, [self.procinfo['stdout_logfile'], self.procinfo['stderr_logfile']])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def process_name(self):
|
|
|
|
return self._process_name
|
2019-06-27 12:36:18 +00:00
|
|
|
|
2019-06-27 12:12:01 +00:00
|
|
|
@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']]
|
2019-06-24 12:29:31 +00:00
|
|
|
|
|
|
|
def on_modified(self, event):
|
2019-06-27 12:12:01 +00:00
|
|
|
self.server_connector.send_message({
|
2019-06-24 12:29:31 +00:00
|
|
|
'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):
|
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
|
2019-06-27 12:12:01 +00:00
|
|
|
self.monitor = LogInotifyObserver(self.server_connector, process_name, log_tail)
|
2019-06-24 12:29:31 +00:00
|
|
|
self.monitor.start()
|
2018-05-30 13:21:36 +00:00
|
|
|
|
|
|
|
self.command_handlers = {
|
|
|
|
'process_name': self.handle_process_name,
|
|
|
|
'log_tail': self.handle_log_tail
|
|
|
|
}
|
2018-05-30 11:14:25 +00:00
|
|
|
|
|
|
|
def handle_event(self, message):
|
2018-05-30 13:21:36 +00:00
|
|
|
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):
|
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
|
|
|
"""
|
2019-06-27 12:12:01 +00:00
|
|
|
self.monitor.process_name = data['value']
|
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
|
|
|
"""
|
2019-06-27 12:12:01 +00:00
|
|
|
self.monitor.log_tail = data['value']
|
2018-06-01 13:15:07 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
self.monitor.stop()
|