mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-22 18:11:56 +00:00
Implement watchdog observer to watch the logs of a supervisor process
This commit is contained in:
parent
a6d9b50b93
commit
029f4a9eb2
@ -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
|
||||
|
43
lib/tfw/components/log_monitor.py
Normal file
43
lib/tfw/components/log_monitor.py
Normal 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)
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue
Block a user