merge 3.25 heads 3.25
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 30 Mar 2017 10:37:55 +0200
branch3.25
changeset 12123 1cf37b28aa23
parent 12122 92aef8c6e7c8 (current diff)
parent 12114 40446d4e1ee2 (diff)
child 12124 392d6d599286
merge 3.25 heads
cubicweb/hooks/logstats.py
--- a/cubicweb/hooks/__init__.py	Wed Mar 29 14:56:04 2017 +0200
+++ b/cubicweb/hooks/__init__.py	Thu Mar 30 10:37:55 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/hooks/logstats.py	Wed Mar 29 14:56:04 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-# copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
-# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
-#
-# This file is part of CubicWeb.
-#
-# CubicWeb is free software: you can redistribute it and/or modify it under the
-# terms of the GNU Lesser General Public License as published by the Free
-# Software Foundation, either version 2.1 of the License, or (at your option)
-# any later version.
-#
-# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
-# details.
-#
-# You should have received a copy of the GNU Lesser General Public License along
-# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-
-"""looping task for dumping instance's stats in a file
-"""
-
-
-
-from datetime import datetime
-import json
-
-from cubicweb.server import hook
-
-class LogStatsStartHook(hook.Hook):
-    """register task to regularly dump instance's stats in a file
-
-    data are stored as one json entry per row
-    """
-    __regid__ = 'cubicweb.hook.logstats.start'
-    events = ('server_startup',)
-
-    def __call__(self):
-        interval = self.repo.config.get('logstat-interval', 0)
-        if interval <= 0:
-            return            
-
-        def dump_stats(repo):
-            statsfile = repo.config.get('logstat-file')
-            with repo.internal_cnx() as cnx:
-                stats = cnx.call_service('repo_stats')
-                gcstats = cnx.call_service('repo_gc_stats', nmax=5)
-                
-            allstats = {'resources': stats,
-                        'memory': gcstats,
-                        'timestamp': datetime.utcnow().isoformat(),
-                       }
-            try:
-                with open(statsfile, 'ab') as ofile:
-                    json.dump(allstats, ofile)
-                    ofile.write('\n')
-            except IOError:
-                repo.warning('Cannot open stats file for writing: %s', statsfile)
-                    
-        self.repo.looping_task(interval, dump_stats, self.repo)
--- a/cubicweb/server/repository.py	Wed Mar 29 14:56:04 2017 +0200
+++ b/cubicweb/server/repository.py	Thu Mar 30 10:37:55 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',
@@ -405,8 +411,14 @@
         looping tasks can only be registered during repository initialization,
         once done this method will fail.
         """
-        assert self._scheduler is not None, \
-            "This Repository is not intended to be used as a server"
+        if self.config.repairing:
+            return
+        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 '
+                'have it running', func)
+            return
         event = utils.schedule_periodic_task(
             self._scheduler, interval, func, *args)
         self.info('scheduled periodic task %s (interval: %.2fs)',
--- a/doc/changes/3.25.rst	Wed Mar 29 14:56:04 2017 +0200
+++ b/doc/changes/3.25.rst	Thu Mar 30 10:37:55 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).
@@ -70,3 +74,7 @@
   (4516c3956d46).
 
 * The `next_tabindex` method of request class has been removed (011730a4af73).
+
+* The `cubicweb.hook.logstats.start` hook was dropped because it's looping
+  task would not be run in a web instance (see first point about repository
+  scheduler).