Rework log deletion – use log_tail arguments instead

This commit is contained in:
Kristóf Tóth 2018-05-29 17:59:16 +02:00
parent 4815b97200
commit 28c16e1127
3 changed files with 14 additions and 13 deletions

View File

@ -12,10 +12,10 @@ from tfw.mixins import ObserverMixin, SupervisorLogMixin
class LogMonitor(ObserverMixin):
def __init__(self, process_name):
def __init__(self, process_name, log_tail=0):
self.prevent_log_recursion()
ObserverMixin.__init__(self)
event_handler = SendLogWatchdogEventHandler(process_name)
event_handler = SendLogWatchdogEventHandler(process_name, log_tail=log_tail)
self.observer.schedule(
event_handler,
event_handler.path
@ -28,12 +28,13 @@ class LogMonitor(ObserverMixin):
class SendLogWatchdogEventHandler(PatternMatchingWatchdogEventHandler, SupervisorLogMixin):
def __init__(self, process_name):
def __init__(self, process_name, log_tail=0):
self.threadlocalise_supervisor_instance()
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()
self.log_tail = log_tail
@property
def path(self):
@ -45,7 +46,7 @@ class SendLogWatchdogEventHandler(PatternMatchingWatchdogEventHandler, Superviso
'key': 'processlog',
'data': {
'command': 'new_log',
'stdout': self.read_stdout(self.process_name),
'stderr': self.read_stderr(self.process_name)
'stdout': self.read_stdout(self.process_name, tail=self.log_tail),
'stderr': self.read_stderr(self.process_name, tail=self.log_tail)
}
})

View File

@ -34,11 +34,12 @@ class ProcessManagingEventHandler(EventHandlerBase):
Commands available: start, stop, restart, readlog
(the names are as self-documenting as it gets)
"""
def __init__(self, key, dirmonitor=None):
def __init__(self, key, dirmonitor=None, log_tail=0):
super().__init__(key)
self.key = key
self.monitor = dirmonitor
self.processmanager = ProcessManager()
self.log_tail = log_tail
@with_monitor_paused
def handle_event(self, message):
@ -49,9 +50,8 @@ class ProcessManagingEventHandler(EventHandlerBase):
except SupervisorFault as fault:
message['data']['error'] = fault.faultString
finally:
message['data']['stdout'] = self.processmanager.read_stdout(data['process_name'])
message['data']['stderr'] = self.processmanager.read_stderr(data['process_name'])
self.processmanager.clear_logs(data['process_name'])
message['data']['stdout'] = self.processmanager.read_stdout(data['process_name'], self.log_tail)
message['data']['stderr'] = self.processmanager.read_stderr(data['process_name'], self.log_tail)
return message
except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)

View File

@ -37,11 +37,11 @@ class SupervisorMixin(SupervisorBaseMixin):
class SupervisorLogMixin(SupervisorBaseMixin):
def read_stdout(self, process_name):
return self.supervisor.readProcessStdoutLog(process_name, 0, 0)
def read_stdout(self, process_name, tail=0):
return self.supervisor.readProcessStdoutLog(process_name, -tail, 0)
def read_stderr(self, process_name):
return self.supervisor.readProcessStderrLog(process_name, 0, 0)
def read_stderr(self, process_name, tail=0):
return self.supervisor.readProcessStderrLog(process_name, -tail, 0)
def clear_logs(self, process_name):
for logfile in ('stdout_logfile', 'stderr_logfile'):