[repo] ensure we don't restart a task while the repo is shutting down (closes #1942736) stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 22 Sep 2011 10:02:25 +0200
branchstable
changeset 7823 5f145462e041
parent 7822 ce8a4de2ecf1
child 7824 18ce47d4a17f
[repo] ensure we don't restart a task while the repo is shutting down (closes #1942736) we need a safety belt in cw itself again looping task which use 'try / except' or doesn't need to access the db (which will trigger ShuttingDown exception)
server/repository.py
server/utils.py
--- a/server/repository.py	Thu Sep 22 10:02:17 2011 +0200
+++ b/server/repository.py	Thu Sep 22 10:02:25 2011 +0200
@@ -344,7 +344,7 @@
             self.looping_task(cleanup_session_interval, self.clean_sessions)
         assert isinstance(self._looping_tasks, list), 'already started'
         for i, (interval, func, args) in enumerate(self._looping_tasks):
-            self._looping_tasks[i] = task = utils.LoopTask(interval, func, args)
+            self._looping_tasks[i] = task = utils.LoopTask(self, interval, func, args)
             self.info('starting task %s with interval %.2fs', task.name,
                       interval)
             task.start()
--- a/server/utils.py	Thu Sep 22 10:02:17 2011 +0200
+++ b/server/utils.py	Thu Sep 22 10:02:25 2011 +0200
@@ -122,11 +122,12 @@
 
 class LoopTask(object):
     """threaded task restarting itself once executed"""
-    def __init__(self, interval, func, args):
+    def __init__(self, repo, interval, func, args):
         if interval <= 0:
             raise ValueError('Loop task interval must be > 0 '
                              '(current value: %f for %s)' % \
                              (interval, func_name(func)))
+        self.repo = repo
         self.interval = interval
         def auto_restart_func(self=self, func=func, args=args):
             restart = True
@@ -139,7 +140,7 @@
             except BaseException:
                 restart = False
             finally:
-                if restart:
+                if restart and not self.repo.shutting_down:
                     self.start()
         self.func = auto_restart_func
         self.name = func_name(func)