Implement watchdog observer to watch the logs of a supervisor process

This commit is contained in:
Kristóf Tóth 2018-05-29 10:26:15 +02:00
parent a6d9b50b93
commit 029f4a9eb2
2 changed files with 44 additions and 0 deletions

View File

@ -7,3 +7,4 @@ from .terminal_event_handler import TerminalEventHandler
from .ide_event_handler import IdeEventHandler from .ide_event_handler import IdeEventHandler
from .history_monitor import HistoryMonitor, BashMonitor, GDBMonitor from .history_monitor import HistoryMonitor, BashMonitor, GDBMonitor
from .terminal_commands import TerminalCommands from .terminal_commands import TerminalCommands
from .log_monitor import LogMonitor

View File

@ -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)
}
})