Use ApplicationCreated event to start scheduler sub-process
authorThierry Florac <thierry.florac@onf.fr>
Thu, 21 Apr 2016 17:29:20 +0200
changeset 25 028b2cfeba3f
parent 24 e61cccfea162
child 26 b8bfdb200416
Use ApplicationCreated event to start scheduler sub-process
src/pyams_scheduler/include.py
--- a/src/pyams_scheduler/include.py	Thu Oct 08 11:32:37 2015 +0200
+++ b/src/pyams_scheduler/include.py	Thu Apr 21 17:29:20 2016 +0200
@@ -24,6 +24,7 @@
 from pyams_scheduler.interfaces import SCHEDULER_HANDLER_KEY, SCHEDULER_NAME
 from pyams_utils.interfaces import PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME
 from pyams_utils.interfaces.zeo import IZEOConnection
+from pyramid.interfaces import IApplicationCreated
 from zope.interface.interfaces import ComponentLookupError
 
 # import packages
@@ -31,6 +32,8 @@
 from pyams_utils.registry import set_local_registry, query_utility
 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):
@@ -47,11 +50,17 @@
     else:
         config.scan()
 
+
+@subscriber(IApplicationCreated)
+def handle_new_application(event):
+    """Start scheduler process when application is 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(SCHEDULER_HANDLER_KEY, False)
     if start_handler:
         # get database connection
@@ -74,16 +83,16 @@
                     zeo_connection = query_utility(IZEOConnection, name=zeo_connection_name)
                     if zeo_connection is not None:
                         # create scheduler process
-                        process = SchedulerProcess(start_handler, SchedulerMessageHandler, config.registry)
+                        process = SchedulerProcess(start_handler, SchedulerMessageHandler, registry)
                         # load tasks
                         for task in scheduler_util.values():
-                            trigger = task.get_trigger(config.registry)
+                            trigger = task.get_trigger(registry)
                             logger.debug("Adding scheduler job for task '{0.name}'".format(task))
                             process.scheduler.add_job(task, trigger,
                                                       id=str(task.internal_id),
                                                       name=task.name,
                                                       kwargs={'zeo_settings': zeo_connection.get_settings(),
-                                                              'registry': config.registry})
+                                                              'registry': registry})
                         # start process
                         logger.debug("Starting tasks scheduler {0!r}...".format(process))
                         process.start()