Refactor LogMonitoringEventHandler

This commit is contained in:
Kristóf Tóth 2019-06-27 15:22:48 +02:00
parent c5c5d2c10a
commit 6cc3d32097

View File

@ -13,12 +13,21 @@ LOG = logging.getLogger(__name__)
class LogInotifyObserver(InotifyObserver, SupervisorLogMixin): class LogInotifyObserver(InotifyObserver, SupervisorLogMixin):
def __init__(self, server_connector, process_name, log_tail=0): def __init__(self, server_connector, process_name, log_tail=0):
self.prevent_log_recursion() self._prevent_log_recursion()
self.server_connector = server_connector self._server_connector = server_connector
self._process_name = process_name self._process_name = process_name
self.log_tail = log_tail self.log_tail = log_tail
self.procinfo = self.supervisor.getProcessInfo(self.process_name) self._procinfo = None
InotifyObserver.__init__(self, [self.procinfo['stdout_logfile'], self.procinfo['stderr_logfile']]) InotifyObserver.__init__(self, self._get_logfiles())
@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
def _get_logfiles(self):
self._procinfo = self.supervisor.getProcessInfo(self._process_name)
return self._procinfo['stdout_logfile'], self._procinfo['stderr_logfile']
@property @property
def process_name(self): def process_name(self):
@ -27,11 +36,10 @@ class LogInotifyObserver(InotifyObserver, SupervisorLogMixin):
@process_name.setter @process_name.setter
def process_name(self, process_name): def process_name(self, process_name):
self._process_name = process_name self._process_name = process_name
self.procinfo = self.supervisor.getProcessInfo(process_name) self.paths = self._get_logfiles()
self.paths = [self.procinfo['stdout_logfile'], self.procinfo['stderr_logfile']]
def on_modified(self, event): def on_modified(self, event):
self.server_connector.send_message({ self._server_connector.send_message({
'key': 'processlog', 'key': 'processlog',
'data': { 'data': {
'command': 'new_log', 'command': 'new_log',
@ -40,11 +48,6 @@ class LogInotifyObserver(InotifyObserver, SupervisorLogMixin):
} }
}, Scope.BROADCAST) }, 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): class LogMonitoringEventHandler(FrontendEventHandlerBase):
""" """
@ -59,9 +62,8 @@ class LogMonitoringEventHandler(FrontendEventHandlerBase):
def __init__(self, key, process_name, log_tail=0): def __init__(self, key, process_name, log_tail=0):
super().__init__(key) super().__init__(key)
self.process_name = process_name self.process_name = process_name
self.log_tail = log_tail self._monitor = LogInotifyObserver(self.server_connector, process_name, log_tail)
self.monitor = LogInotifyObserver(self.server_connector, process_name, log_tail) self._monitor.start()
self.monitor.start()
self.command_handlers = { self.command_handlers = {
'process_name': self.handle_process_name, 'process_name': self.handle_process_name,
@ -82,7 +84,7 @@ class LogMonitoringEventHandler(FrontendEventHandlerBase):
:param data: TFW message data containing 'value' :param data: TFW message data containing 'value'
(name of the process to monitor) (name of the process to monitor)
""" """
self.monitor.process_name = data['value'] self._monitor.process_name = data['value']
def handle_log_tail(self, data): def handle_log_tail(self, data):
""" """
@ -93,7 +95,7 @@ class LogMonitoringEventHandler(FrontendEventHandlerBase):
:param data: TFW message data containing 'value' :param data: TFW message data containing 'value'
(new tail length) (new tail length)
""" """
self.monitor.log_tail = data['value'] self._monitor.log_tail = data['value']
def cleanup(self): def cleanup(self):
self.monitor.stop() self._monitor.stop()