Use ZODBConnection as context manager when starting indexer process
authorThierry Florac <thierry.florac@onf.fr>
Fri, 26 Jan 2018 16:46:11 +0100
changeset 41 929e3470cfeb
parent 40 8d54446fe172
child 42 242331259942
Use ZODBConnection as context manager when starting indexer process
src/pyams_content_es/process.py
--- a/src/pyams_content_es/process.py	Sun Jan 14 12:44:17 2018 +0100
+++ b/src/pyams_content_es/process.py	Fri Jan 26 16:46:11 2018 +0100
@@ -65,38 +65,35 @@
         manager = None
         zodb_name = settings.get('zodb_name')
         logger.debug("Opening ZODB connection...")
-        zodb = ZODBConnection(name=zodb_name)
-        connection = zodb.get_connection()
-        try:
-            root = connection.root()
-            logger.debug("Getting connection root {0!r}".format(root))
-            application_name = registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME)
-            application = root.get(application_name)
-            logger.debug("Loading application {0!r} named {1}".format(application, application_name))
-            if application is not None:
-                # set local registry
-                sm = application.getSiteManager()
-                set_local_registry(sm)
-                logger.debug("Setting local registry {0!r}".format(sm))
-                # find document
-                intids = get_utility(IIntIds)
-                document = intids.queryObject(document_id)
-                if document is None:
-                    logger.warning("Can't find requested document {0}!".format(document_id))
-                    return
-                # index document
-                logger.debug("Starting indexing for {0!r}".format(document))
-                manager = ITransactionManager(document)
-                for attempt in manager.attempts():
-                    with attempt as t:
-                        self.update_index(es_client, document)
-                    if t.status == 'Committed':
-                        break
-        finally:
-            if manager is not None:
-                manager.abort()
-            connection.close()
-            threadlocal_manager.pop()
+        with ZODBConnection(name=zodb_name) as root:
+            try:
+                logger.debug("Getting connection root {0!r}".format(root))
+                application_name = registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME)
+                application = root.get(application_name)
+                logger.debug("Loading application {0!r} named {1}".format(application, application_name))
+                if application is not None:
+                    # set local registry
+                    sm = application.getSiteManager()
+                    set_local_registry(sm)
+                    logger.debug("Setting local registry {0!r}".format(sm))
+                    # find document
+                    intids = get_utility(IIntIds)
+                    document = intids.queryObject(document_id)
+                    if document is None:
+                        logger.warning("Can't find requested document {0}!".format(document_id))
+                        return
+                    # index document
+                    logger.debug("Starting indexing for {0!r}".format(document))
+                    manager = ITransactionManager(document)
+                    for attempt in manager.attempts():
+                        with attempt as t:
+                            self.update_index(es_client, document)
+                        if t.status == 'Committed':
+                            break
+            finally:
+                if manager is not None:
+                    manager.abort()
+                threadlocal_manager.pop()
 
     def update_index(self, client, document):
         """Update index"""