# HG changeset patch # User Thierry Florac # Date 1516981571 -3600 # Node ID 929e3470cfeb4f73fa8595aa5c2dda3b3221c1f3 # Parent 8d54446fe1720cbe5c105fb16deef52de4aea0c1 Use ZODBConnection as context manager when starting indexer process diff -r 8d54446fe172 -r 929e3470cfeb 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"""