Use IApplicationCreatedEvent to start medias converter process
authorThierry Florac <thierry.florac@onf.fr>
Thu, 21 Apr 2016 17:01:09 +0200
changeset 32 27d0b4049921
parent 31 5d4da6163895
child 33 22b2669350ec
Use IApplicationCreatedEvent to start medias converter process
src/pyams_media/include.py
--- a/src/pyams_media/include.py	Thu Apr 21 17:00:31 2016 +0200
+++ b/src/pyams_media/include.py	Thu Apr 21 17:01:09 2016 +0200
@@ -23,12 +23,15 @@
 # import interfaces
 from pyams_media.interfaces import CONVERTER_HANDLER_KEY, CONVERTER_NAME
 from pyams_utils.interfaces import PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME
+from pyramid.interfaces import IApplicationCreated
 
 # import packages
 from pyams_media.process import MediaConversionProcess, MediaConversionMessageHandler
 from pyams_utils.registry import set_local_registry
 from pyams_utils.zodb import get_connection_from_settings
 from pyams_zmq.process import process_exit_func
+from pyramid.events import subscriber
+from zope.component.globalregistry import getGlobalSiteManager
 
 
 def include_package(config):
@@ -45,11 +48,17 @@
     else:
         config.scan()
 
+
+@subscriber(IApplicationCreated)
+def handle_new_application(event):
+    """Start converter process when application created"""
+
     # check for upgrade mode
     if sys.argv[0].endswith('pyams_upgrade'):
         return
 
-    settings = config.registry.settings
+    registry = getGlobalSiteManager()
+    settings = registry.settings
     start_handler = settings.get(CONVERTER_HANDLER_KEY, False)
     if start_handler:
         # get database connection
@@ -66,7 +75,7 @@
                 conversion_util = sm.get(CONVERTER_NAME)
                 if conversion_util is not None:
                     # create medias converter process
-                    process = MediaConversionProcess(start_handler, MediaConversionMessageHandler, config.registry)
+                    process = MediaConversionProcess(start_handler, MediaConversionMessageHandler, registry)
                     logger.debug('Starting medias conversion process {0!r}...'.format(process))
                     process.start()
                     if process.is_alive():