From 029f4a9eb20e88095d43c578e171b44bc0bf1101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 29 May 2018 10:26:15 +0200 Subject: [PATCH] Implement watchdog observer to watch the logs of a supervisor process --- lib/tfw/components/__init__.py | 1 + lib/tfw/components/log_monitor.py | 43 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 lib/tfw/components/log_monitor.py diff --git a/lib/tfw/components/__init__.py b/lib/tfw/components/__init__.py index f707c24..dee92a1 100644 --- a/lib/tfw/components/__init__.py +++ b/lib/tfw/components/__init__.py @@ -7,3 +7,4 @@ from .terminal_event_handler import TerminalEventHandler from .ide_event_handler import IdeEventHandler from .history_monitor import HistoryMonitor, BashMonitor, GDBMonitor from .terminal_commands import TerminalCommands +from .log_monitor import LogMonitor diff --git a/lib/tfw/components/log_monitor.py b/lib/tfw/components/log_monitor.py new file mode 100644 index 0000000..db66132 --- /dev/null +++ b/lib/tfw/components/log_monitor.py @@ -0,0 +1,43 @@ +# Copyright (C) 2018 Avatao.com Innovative Learning Kft. +# All Rights Reserved. See LICENSE file for details. + +from os.path import dirname + +from watchdog.events import PatternMatchingEventHandler as PatternMatchingWatchdogEventHandler + +from tfw.networking.event_handlers import ServerUplinkConnector +from tfw.decorators import RateLimiter +from tfw.mixins import ObserverMixin, SupervisorLogMixin + + +class LogMonitor(ObserverMixin): + def __init__(self, process_name): + ObserverMixin.__init__(self) + event_handler = SendLogWatchdogEventHandler(process_name) + self.observer.schedule( + event_handler, + event_handler.path + ) + + +class SendLogWatchdogEventHandler(PatternMatchingWatchdogEventHandler, SupervisorLogMixin): + def __init__(self, process_name): + self.process_name = process_name + self.procinfo = self.supervisor.getProcessInfo(self.process_name) + super().__init__([self.procinfo['stdout_logfile'], self.procinfo['stderr_logfile']]) + self.uplink = ServerUplinkConnector() + + @property + def path(self): + return dirname(self.procinfo['stdout_logfile']) + + @RateLimiter(rate_per_second=5) + def on_modified(self, event): + self.uplink.send({ + 'key': 'console', + 'data': { + 'command': 'writelog', + 'stdout': self.read_stdout(self.process_name), + 'stderr': self.read_stderr(self.process_name) + } + })