Added checks in include to handle empty sites
authorThierry Florac <thierry.florac@onf.fr>
Tue, 17 Mar 2015 16:09:59 +0100
changeset 8 c0d0514e86e0
parent 7 570db7b9f0f6
child 9 b83989cde81d
Added checks in include to handle empty sites
src/pyams_scheduler/include.py
--- a/src/pyams_scheduler/include.py	Sat Mar 14 13:29:42 2015 +0100
+++ b/src/pyams_scheduler/include.py	Tue Mar 17 16:09:59 2015 +0100
@@ -22,6 +22,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 zope.interface.interfaces import ComponentLookupError
 
 # import packages
 from pyams_scheduler.process import SchedulerProcess, SchedulerMessageHandler
@@ -57,20 +58,26 @@
         application_name = config.registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY,
                                                         PYAMS_APPLICATION_DEFAULT_NAME)
         application = root.get(application_name)
-        sm = application.getSiteManager()
-        scheduler_util = sm.get(SCHEDULER_NAME)
-        zeo_connection = sm.getUtility(IZEOConnection, name=scheduler_util.zeo_connection)
-        # 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)
+        if application is not None:
+            sm = application.getSiteManager()
+            scheduler_util = sm.get(SCHEDULER_NAME)
+            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
+                process.start()
+                if process.is_alive():
+                    atexit.register(process_exit_func, process=process)