[repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
--- a/server/__init__.py Wed Jun 29 15:50:26 2011 +0200
+++ b/server/__init__.py Wed Jun 29 16:02:24 2011 +0200
@@ -36,6 +36,12 @@
from cubicweb import CW_SOFTWARE_ROOT
+class ShuttingDown(BaseException):
+ """raised when trying to access some resources while the repository is
+ shutting down. Inherit from BaseException so that `except Exception` won't
+ catch it.
+ """
+
# server-side debugging #########################################################
# server debugging flags. They may be combined using binary operators.
--- a/server/repository.py Wed Jun 29 15:50:26 2011 +0200
+++ b/server/repository.py Wed Jun 29 16:02:24 2011 +0200
@@ -55,7 +55,7 @@
BadConnectionId, Unauthorized, ValidationError,
RepositoryError, UniqueTogetherError, typed_eid, onevent)
from cubicweb import cwvreg, schema, server
-from cubicweb.server import utils, hook, pool, querier, sources
+from cubicweb.server import ShuttingDown, utils, hook, pool, querier, sources
from cubicweb.server.session import Session, InternalSession, InternalManager, \
security_enabled
from cubicweb.server.ssplanner import EditedEntity
@@ -940,7 +940,7 @@
checkshuttingdown=True):
"""return the user associated to the given session identifier"""
if checkshuttingdown and self.shutting_down:
- raise Exception('Repository is shutting down')
+ raise ShuttingDown('Repository is shutting down')
try:
session = self._sessions[sessionid]
except KeyError:
--- a/server/session.py Wed Jun 29 15:50:26 2011 +0200
+++ b/server/session.py Wed Jun 29 16:02:24 2011 +0200
@@ -38,6 +38,7 @@
from cubicweb.dbapi import ConnectionProperties
from cubicweb.utils import make_uid, RepeatList
from cubicweb.rqlrewrite import RQLRewriter
+from cubicweb.server import ShuttingDown
from cubicweb.server.edition import EditedEntity
@@ -1144,7 +1145,7 @@
"""connections pool, set according to transaction mode for each query"""
if self.repo.shutting_down:
self.reset_pool(True)
- raise Exception('repository is shutting down')
+ raise ShuttingDown('repository is shutting down')
return getattr(self._threaddata, 'pool', None)
--- a/server/utils.py Wed Jun 29 15:50:26 2011 +0200
+++ b/server/utils.py Wed Jun 29 16:02:24 2011 +0200
@@ -128,14 +128,18 @@
(interval, func_name(func)))
self.interval = interval
def auto_restart_func(self=self, func=func, args=args):
+ restart = True
try:
func(*args)
- except:
+ except Exception:
logger = logging.getLogger('cubicweb.repository')
logger.exception('Unhandled exception in LoopTask %s', self.name)
raise
+ except BaseException:
+ restart = False
finally:
- self.start()
+ if restart:
+ self.start()
self.func = auto_restart_func
self.name = func_name(func)