Use local registry on scheduler process startup to get ZEO connection
authorThierry Florac <thierry.florac@onf.fr>
Thu, 23 Jul 2015 17:28:38 +0200
changeset 16 0a23439ac705
parent 15 d035041ef482
child 17 643ce60ce5f7
Use local registry on scheduler process startup to get ZEO connection
src/pyams_scheduler/include.py
--- a/src/pyams_scheduler/include.py	Wed Jul 22 17:17:14 2015 +0200
+++ b/src/pyams_scheduler/include.py	Thu Jul 23 17:28:38 2015 +0200
@@ -26,6 +26,7 @@
 
 # import packages
 from pyams_scheduler.process import SchedulerProcess, SchedulerMessageHandler
+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 zmq.error import ZMQError
@@ -58,28 +59,32 @@
         application = root.get(application_name)
         if application is not None:
             sm = application.getSiteManager()
-            scheduler_util = sm.get(SCHEDULER_NAME)
+            set_local_registry(sm)
             try:
-                zeo_connection_name = scheduler_util.zeo_connection
-            except ComponentLookupError:
-                pass
-            else:
-                zeo_connection = sm.getUtility(IZEOConnection, name=zeo_connection_name)
-                # load tasks
-                for task in scheduler_util.values():
-                    trigger = task.get_trigger(config.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})
-                logger.debug("Starting tasks scheduler {0!r}".format(process))
-                # start process
+                scheduler_util = sm.get(SCHEDULER_NAME)
                 try:
-                    process.start()
-                except ZMQError:
+                    zeo_connection_name = scheduler_util.zeo_connection
+                except ComponentLookupError:
+                    pass
+                else:
+                    zeo_connection = query_utility(IZEOConnection, name=zeo_connection_name)
+                    if zeo_connection is not None:
+                        # load tasks
+                        for task in scheduler_util.values():
+                            trigger = task.get_trigger(config.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})
+                        logger.debug("Starting tasks scheduler {0!r}".format(process))
+                        # start process
+                        process.start()
+                        if process.is_alive():
+                            atexit.register(process_exit_func, process=process)
+            finally:
+                if not process.is_alive():
+                    process.terminate()
                     process.join()
-                else:
-                    if process.is_alive():
-                        atexit.register(process_exit_func, process=process)
+                set_local_registry(None)