[GMG-Devel] [PATCH 40/83] Add try-except block to event listeners.

Alon Levy alon at pobox.com
Tue Feb 25 14:58:23 EST 2014


From: Praveen Kumar <praveen97uma at gmail.com>

---
 mediagoblin/plugins/search/__init__.py  | 11 ++++++++--
 mediagoblin/plugins/search/listeners.py | 39 +++++++++++++++++++++++----------
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/mediagoblin/plugins/search/__init__.py b/mediagoblin/plugins/search/__init__.py
index 8be286d..3701223 100644
--- a/mediagoblin/plugins/search/__init__.py
+++ b/mediagoblin/plugins/search/__init__.py
@@ -71,8 +71,15 @@ def setup_plugin():
     ]
 
     pluginapi.register_routes(routes)
-    register_indices()
-    activate_orm_events_listeners()
+    try:
+        register_indices()
+    except Exception as e:
+        _log.debug("Failed to register search indices: %s"%(e))
+
+    try:
+        activate_orm_events_listeners()
+    except Exception as e:
+        _log.debug("Failed to start event listeners: %s"%(e))
     #event.listen(MediaEntry, 'after_insert', mediaentry_add_listener)
     #event.listen(MediaEntry, 'before_insert', mediaentry_before_add_listener)
     #_log.info("Registered listening event") 
diff --git a/mediagoblin/plugins/search/listeners.py b/mediagoblin/plugins/search/listeners.py
index 8212957..1b7a066 100644
--- a/mediagoblin/plugins/search/listeners.py
+++ b/mediagoblin/plugins/search/listeners.py
@@ -14,24 +14,36 @@ class ORMEventsListener(object):
         self.listeners = []
 
     def _after_insert_event_listener(self):
-        event.listen(self.model, 'after_insert',
+        try:
+            event.listen(self.model, 'after_insert',
                 self.insert_event_handler)
+        except Exception, e:
+            _log.debug("Listener activation failed: %s"%(e))
         _log.info("Listening for after_insert event for %s",self.model.__name__)
 
     def _after_update_event_listener(self):
-        event.listen(self.model, 'after_update',
+        try:
+            event.listen(self.model, 'after_update',
                 self.update_event_handler)
+        except Exception as e:
+            _log.debug("Listener activation failed: %s"%(e))
         _log.info("Listening for after_update event for %s",self.model.__name__)
     
     def _after_delete_event_listener(self):
-        event.listen(self.model, 'after_delete',
+        try:
+            event.listen(self.model, 'after_delete',
                 self.delete_event_handler)
+        except Exception as e:
+            _log.debug("Listener activation failed: %s"%(e))
         _log.info("Listening for after_delete event for %s",self.model.__name__)
 
     def insert_event_handler(self, mapper, connection, target):
         index = self.index_registry.get_index_for_object(target)
-        index.add_document_from_model_obj(target)
-    
+        try:
+            index.add_document_from_model_obj(target)
+        except Exception as e:
+            _log.debug("Failed to add object to index: %s"%(e))
+
     def update_event_handler(self, mapper, connection, target):
         index = self.index_registry.get_index_for_object(target)
         index.update_document_from_model_obj(target)
@@ -41,10 +53,13 @@ class ORMEventsListener(object):
         index.delete_document_from_model_obj(target)
     
     def activate_listeners(self):
-        self.listeners = [
-            self._after_insert_event_listener,
-            self._after_update_event_listener,
-            self._after_delete_event_listener,
-        ]
-        for listener in self.listeners:
-            listener()
+        try:
+            self.listeners = [
+                self._after_insert_event_listener,
+                self._after_update_event_listener,
+                self._after_delete_event_listener,
+            ]
+            for listener in self.listeners:
+                listener()
+        except Exception as e:
+            _log.debug("Activating listeners failed: %s"%(e))
-- 
1.8.5.3



More information about the devel mailing list