[server] Introduce an `has_scheduler` method on Repository 3.25
authorDenis Laxalde <denis.laxalde@logilab.fr>
Wed, 29 Mar 2017 11:45:19 +0200
branch3.25
changeset 12112 0a54f49314f6
parent 12111 cd069068a5ef
child 12113 669aaed98678
[server] Introduce an `has_scheduler` method on Repository This is to be used by client application to determine if looping tasks may be registered in the current process. By checking this, one will avoid the warning in looping_task method when the repository has no scheduler.
cubicweb/hooks/__init__.py
cubicweb/server/repository.py
doc/changes/3.25.rst
--- a/cubicweb/hooks/__init__.py	Wed Mar 29 11:37:31 2017 +0200
+++ b/cubicweb/hooks/__init__.py	Wed Mar 29 11:45:19 2017 +0200
@@ -29,7 +29,7 @@
     events = ('server_startup',)
 
     def __call__(self):
-        if self.repo._scheduler is None:
+        if not self.repo.has_scheduler():
             return
         # XXX use named args and inner functions to avoid referencing globals
         # which may cause reloading pb
@@ -51,7 +51,7 @@
     events = ('server_startup',)
 
     def __call__(self):
-        if self.repo._scheduler is None:
+        if not self.repo.has_scheduler():
             return
         def update_feeds(repo):
             # take a list to avoid iterating on a dictionary whose size may
@@ -75,7 +75,7 @@
     events = ('server_startup',)
 
     def __call__(self):
-        if self.repo._scheduler is None:
+        if not self.repo.has_scheduler():
             return
         def expire_dataimports(repo=self.repo):
             for uri, source in repo.sources_by_uri.items():
--- a/cubicweb/server/repository.py	Wed Mar 29 11:37:31 2017 +0200
+++ b/cubicweb/server/repository.py	Wed Mar 29 11:45:19 2017 +0200
@@ -383,6 +383,12 @@
                 raise Exception('Is the database initialised ? (cause: %s)' % ex)
         return appschema
 
+    def has_scheduler(self):
+        """Return True if the repository has a scheduler attached and is able
+        to register looping tasks.
+        """
+        return self._scheduler is not None
+
     def run_scheduler(self):
         """Start repository scheduler after preparing the repository for that.
 
@@ -392,7 +398,7 @@
         XXX Other startup related stuffs are done elsewhere. In Repository
         XXX __init__ or in external codes (various server managers).
         """
-        assert self._scheduler is not None, \
+        assert self.has_scheduler(), \
             "This Repository is not intended to be used as a server"
         self.info(
             'starting repository scheduler with tasks: %s',
@@ -407,7 +413,7 @@
         """
         if self.config.repairing:
             return
-        if self._scheduler is None:
+        if not self.has_scheduler():
             self.warning(
                 'looping task %s will not run in this process where repository '
                 'has no scheduler; use "cubicweb-ctl scheduler <appid>" to '
--- a/doc/changes/3.25.rst	Wed Mar 29 11:37:31 2017 +0200
+++ b/doc/changes/3.25.rst	Wed Mar 29 11:45:19 2017 +0200
@@ -52,7 +52,11 @@
   scheduler, all cubicweb-ctl's "start" commands (i.e. ``start``, ``pyramid``,
   ``wsgi``) do not start repository *looping tasks manager* anymore, nor do
   they start the scheduler. Site administrators are thus expected to start
-  this scheduler as a separate process.
+  this scheduler as a separate process. Also, registering looping tasks (i.e.
+  calling ``repo.looping_tasks()``) is a no-op when the repository has no
+  scheduler set; a warning is issued in such cases. Application developers may
+  rely on repository's ``has_scheduler`` method to determine if they should
+  register a looping task or not.
 
 * In ``cubicweb.pyramid``, function ``make_cubicweb_application`` got renamed
   into ``config_from_cwconfig`` (950ce7d9f642).