From 2c816d5a3831e2ce81834cab290027c994fa9586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Mar 2018 14:45:43 +0100 Subject: [PATCH 1/4] Implement error handling on EventHandler exposing APIs --- lib/tfw/components/process_managing_event_handler.py | 9 ++++++--- lib/tfw/components/source_code_event_handler.py | 11 +++++++---- lib/tfw/components/terminado_event_handler.py | 7 +++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index 6fcc248..ab63607 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -36,6 +36,9 @@ class ProcessManagingEventHandler(TriggerlessEventHandler): @_with_monitor_paused def handle_event(self, key, data_json): - data = data_json['data'] - self.processmanager(data['command'], data['process_name']) - self.uplink.send(self.key, {'data': {'process_name': data['process_name']}}) + try: + data = data_json['data'] + self.processmanager(data['command'], data['process_name']) + self.uplink.send(self.key, {'data': {'process_name': data['process_name']}}) + except KeyError: + log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index dd26072..003264a 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -84,10 +84,13 @@ class SourceCodeEventHandler(TriggerlessEventHandler): data['files'] = self.filemanager.files def handle_event(self, key, data_json): - data = data_json['data'] - data_json['data'] = self.commands[data['command']](data) - self.attach_fileinfo(data) - return data_json + try: + data = data_json['data'] + data_json['data'] = self.commands[data['command']](data) + self.attach_fileinfo(data) + return data_json + except KeyError: + log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) def cleanup(self): self.monitor.stop() diff --git a/lib/tfw/components/terminado_event_handler.py b/lib/tfw/components/terminado_event_handler.py index 4a8f98a..649f823 100644 --- a/lib/tfw/components/terminado_event_handler.py +++ b/lib/tfw/components/terminado_event_handler.py @@ -23,8 +23,11 @@ class TerminadoEventHandler(TriggerlessEventHandler): def handle_event(self, key, data_json): log.debug('TerminadoEventHandler received event: {}'.format(data_json)) - data_json['data'] = self.commands[data_json['data']['command']](data_json['data']) - return data_json + try: + data_json['data'] = self.commands[data_json['data']['command']](data_json['data']) + return data_json + except KeyError: + log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) def write(self, data): self.terminado_server.pty.write(data['shellcmd']) From d11d9a70cb466583b39140c92a67f15d321297c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Mar 2018 14:47:24 +0100 Subject: [PATCH 2/4] Remove TFW antipattern I wrote without thinking --- lib/tfw/components/process_managing_event_handler.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index ab63607..ea28103 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -2,7 +2,6 @@ from functools import wraps from tfw.event_handler_base import TriggerlessEventHandler from tfw.components.mixins import SupervisorMixin -from tfw.networking.event_handlers.server_connector import ServerUplinkConnector from tfw.config.logs import logging log = logging.getLogger(__name__) @@ -23,7 +22,6 @@ class ProcessManagingEventHandler(TriggerlessEventHandler): self.key = key self.monitor = dirmonitor self.processmanager = ProcessManager() - self.uplink = ServerUplinkConnector() def _with_monitor_paused(fun): @wraps(fun) @@ -39,6 +37,6 @@ class ProcessManagingEventHandler(TriggerlessEventHandler): try: data = data_json['data'] self.processmanager(data['command'], data['process_name']) - self.uplink.send(self.key, {'data': {'process_name': data['process_name']}}) + return data_json except KeyError: log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) From 9b550f724d2b00a7f47d25617bd35f8438bf0088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Mar 2018 15:14:33 +0100 Subject: [PATCH 3/4] Extend ProcessManagingEventHander with deployment failure detection --- lib/tfw/components/process_managing_event_handler.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index ea28103..322f9c0 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -1,4 +1,5 @@ from functools import wraps +from xmlrpc.client import Fault as SupervisorFault from tfw.event_handler_base import TriggerlessEventHandler from tfw.components.mixins import SupervisorMixin @@ -37,6 +38,11 @@ class ProcessManagingEventHandler(TriggerlessEventHandler): try: data = data_json['data'] self.processmanager(data['command'], data['process_name']) + data_json['data']['error'] = False return data_json except KeyError: log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) + except SupervisorFault: + data_json['data']['error'] = True + log.debug('Failed to execute command on process: {}'.format(data_json)) + return data_json From 8588328a8f18f0bb9c24437fc78dad50065839f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Mar 2018 16:35:55 +0100 Subject: [PATCH 4/4] Improve handling of supervisor errors in ProcessManagingEventHandler --- lib/tfw/components/process_managing_event_handler.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index 322f9c0..938c5aa 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -38,11 +38,10 @@ class ProcessManagingEventHandler(TriggerlessEventHandler): try: data = data_json['data'] self.processmanager(data['command'], data['process_name']) - data_json['data']['error'] = False return data_json except KeyError: log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) - except SupervisorFault: - data_json['data']['error'] = True + except SupervisorFault as fault: + data_json['data']['error'] = fault.faultString log.debug('Failed to execute command on process: {}'.format(data_json)) return data_json