Use ZODBConnection as context manager when starting conversion process
authorThierry Florac <thierry.florac@onf.fr>
Fri, 26 Jan 2018 16:28:00 +0100
changeset 69 7f1f296ef0b8
parent 68 883c3c189f4e
child 70 8e2e54492244
Use ZODBConnection as context manager when starting conversion process
src/pyams_media/process.py
--- a/src/pyams_media/process.py	Sun Jan 14 14:41:45 2018 +0100
+++ b/src/pyams_media/process.py	Fri Jan 26 16:28:00 2018 +0100
@@ -69,49 +69,46 @@
         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 media
-                intids = get_utility(IIntIds)
-                media = intids.queryObject(media_id)
-                if media is None:
-                    logger.warning("Can't find requested media {0}!".format(media_id))
-                    return
-                # extract converter output
-                logger.debug("Starting conversion process for {0!r} to {1}".format(media, media_format))
-                manager = ITransactionManager(media)
-                for width, conversion in converter.convert(media):
-                    conversion_result = conversion.get('output', '')
-                    logger.debug("Finished FFmpeg conversion process to {0}, {1} bytes output"
-                                 .format(media_format, len(conversion_result)))
-                    # add conversion in a transaction attempts loop
-                    if len(conversion_result) > 0:
-                        for attempt in manager.attempts():
-                            with attempt as t:
-                                converted = IMediaConversions(media).add_conversion(conversion_result, media_format,
-                                                                                    converter.format, width)
-                                IAnnotations(converted)[MEDIA_CONVERSION_CMDLINE_KEY] = conversion.get('cmdline')
-                            if t.status == 'Committed':
-                                break
-                    else:
-                        logger.warning("Finished FFmpeg conversion process with **NO OUTPUT** !!!")
-                        logger.warning(conversion.get('errors'))
-        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 media
+                    intids = get_utility(IIntIds)
+                    media = intids.queryObject(media_id)
+                    if media is None:
+                        logger.warning("Can't find requested media {0}!".format(media_id))
+                        return
+                    # extract converter output
+                    logger.debug("Starting conversion process for {0!r} to {1}".format(media, media_format))
+                    manager = ITransactionManager(media)
+                    for width, conversion in converter.convert(media):
+                        conversion_result = conversion.get('output', '')
+                        logger.debug("Finished FFmpeg conversion process to {0}, {1} bytes output"
+                                     .format(media_format, len(conversion_result)))
+                        # add conversion in a transaction attempts loop
+                        if len(conversion_result) > 0:
+                            for attempt in manager.attempts():
+                                with attempt as t:
+                                    converted = IMediaConversions(media).add_conversion(conversion_result, media_format,
+                                                                                        converter.format, width)
+                                    IAnnotations(converted)[MEDIA_CONVERSION_CMDLINE_KEY] = conversion.get('cmdline')
+                                if t.status == 'Committed':
+                                    break
+                        else:
+                            logger.warning("Finished FFmpeg conversion process with **NO OUTPUT** !!!")
+                            logger.warning(conversion.get('errors'))
+            finally:
+                if manager is not None:
+                    manager.abort()
+                threadlocal_manager.pop()
 
 
 class ConversionThread(Thread):