From bfd1efa9f95c95c14e7197cc9a72f3a1c305d720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 24 May 2018 11:20:00 +0200 Subject: [PATCH] Append deployment logs to every response of ProcessManagingEventHandler --- .../process_managing_event_handler.py | 12 +++++++----- lib/tfw/mixins/supervisor_mixin.py | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index 958ae81..9706096 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -13,10 +13,11 @@ LOG = logging.getLogger(__name__) class ProcessManager(SupervisorMixin): def __init__(self): - self.commands = {'start': self.start_process, - 'stop': self.stop_process, - 'restart': self.restart_process, - 'readlog': self.read_log} + self.commands = {'start': self.start_process, + 'stop': self.stop_process, + 'restart': self.restart_process, + 'readlog': self.read_log_stdout, + 'readerrlog': self.read_log_stderr} def __call__(self, command, process_name): return self.commands[command](process_name) @@ -46,10 +47,11 @@ class ProcessManagingEventHandler(EventHandlerBase): try: data = message['data'] self.processmanager(data['command'], data['process_name']) + message['data']['log'] = self.processmanager.read_log_stdout(message['data']['process_name']) return message except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) except SupervisorFault as fault: message['data']['error'] = fault.faultString - message['data']['log'] = self.processmanager.read_log(message['data']['process_name']) + message['data']['log'] = self.processmanager.read_log_stderr(message['data']['process_name']) return message diff --git a/lib/tfw/mixins/supervisor_mixin.py b/lib/tfw/mixins/supervisor_mixin.py index 9a350b9..a95b769 100644 --- a/lib/tfw/mixins/supervisor_mixin.py +++ b/lib/tfw/mixins/supervisor_mixin.py @@ -19,11 +19,21 @@ class SupervisorMixin: def start_process(self, process_name): self.supervisor.startProcess(process_name) - def read_log(self, process_name): - logs = self.supervisor.readProcessStderrLog(process_name, 0, 0) - remove(self.supervisor.getProcessInfo(process_name)['stderr_logfile']) + def read_log_stdout(self, process_name): + return self._read_log_internal(self.supervisor.readProcessStdoutLog, process_name) + + def read_log_stderr(self, process_name): + return self._read_log_internal(self.supervisor.readProcessStderrLog, process_name) + + def _read_log_internal(self, read_method, process_name): + log = read_method(process_name, 0, 0) + self.clear_logs(process_name) + return log + + def clear_logs(self, process_name): + for logfile in ('stdout_logfile', 'stderr_logfile'): + remove(self.supervisor.getProcessInfo(process_name)[logfile]) self.supervisor.clearProcessLogs(process_name) - return logs def restart_process(self, process_name): self.stop_process(process_name)