From 1195779bb7af7342b5587c02b9f71b6e1969efd4 Mon Sep 17 00:00:00 2001 From: Victor Torres Date: Thu, 8 Dec 2016 18:16:25 -0300 Subject: [PATCH] Add before and after handle --- greenswitch/esl.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/greenswitch/esl.py b/greenswitch/esl.py index 00e3cc0..c9cdd8d 100644 --- a/greenswitch/esl.py +++ b/greenswitch/esl.py @@ -125,6 +125,13 @@ def handle_event(self, event): event.parse_data(data) self._esl_event_queue.put(event) + def _safe_exec_handler(self, handler, event): + try: + handler(event) + except: + logging.exception('ESL %s raised exception.' % handler.__name__) + logging.error(pprint.pformat(event.headers)) + def process_events(self): logging.debug('Event Processor Running') while self._run: @@ -144,14 +151,14 @@ def process_events(self): if not handlers: continue + if hasattr(self, 'before_handle'): + self._safe_exec_handler(self.before_handle, event) + for handle in handlers: - try: - handle(event) - except: - logging.exception( - 'ESL handler %s raised exception.' % - handle.__name__) - logging.error(pprint.pformat(event.headers)) + self._safe_exec_handler(handle, event) + + if hasattr(self, 'after_handle'): + self._safe_exec_handler(self.after_handle, event) def send(self, data): if not self.connected: