[repo] Kill repo._sessions
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 10 Mar 2017 14:04:26 +0100
changeset 12028 08c866d2f11d
parent 12027 c62c80f20a82
child 12029 3d9883a6068f
[repo] Kill repo._sessions and with it the need to have a looping task to clean it up, and with that all the session's timestamping machinery.
cubicweb/devtools/testlib.py
cubicweb/pyramid/core.py
cubicweb/server/repository.py
cubicweb/server/session.py
cubicweb/server/test/unittest_serverctl.py
--- a/cubicweb/devtools/testlib.py	Fri Mar 10 18:24:40 2017 +0100
+++ b/cubicweb/devtools/testlib.py	Fri Mar 10 14:04:26 2017 +0100
@@ -241,7 +241,6 @@
             user.properties
             user.login
             session = Session(user, self._repo)
-            self._repo._sessions[session.sessionid] = session
             user._cw = user.cw_rset.req = session
         with session.new_cnx() as cnx:
             self._repo.hm.call_hooks('session_open', cnx)
--- a/cubicweb/pyramid/core.py	Fri Mar 10 18:24:40 2017 +0100
+++ b/cubicweb/pyramid/core.py	Fri Mar 10 14:04:26 2017 +0100
@@ -300,7 +300,6 @@
     # with session.new_cnx() as cnx:
     #     repo.hm.call_hooks('session_open', cnx)
     #     cnx.commit()
-    # repo._sessions[session.sessionid] = session
     return session
 
 
--- a/cubicweb/server/repository.py	Fri Mar 10 18:24:40 2017 +0100
+++ b/cubicweb/server/repository.py	Fri Mar 10 14:04:26 2017 +0100
@@ -30,7 +30,6 @@
 
 from warnings import warn
 from itertools import chain
-from time import time, localtime, strftime
 from contextlib import contextmanager
 from logging import getLogger
 
@@ -44,7 +43,6 @@
 
 from cubicweb import (CW_MIGRATION_MAP, QueryError,
                       UnknownEid, AuthenticationError, ExecutionError,
-                      BadConnectionId,
                       UniqueTogetherError, ViolatedConstraint)
 from cubicweb import set_log_methods
 from cubicweb import cwvreg, schema, server
@@ -220,8 +218,6 @@
         self._scheduler = scheduler
 
         self.app_instances_bus = NullEventBus()
-        # dictionary of opened sessions
-        self._sessions = {}
 
         # list of functions to be called at regular interval
         # list of running threads
@@ -390,31 +386,15 @@
                 raise Exception('Is the database initialised ? (cause: %s)' % ex)
         return appschema
 
-    def _prepare_startup(self):
-        """Prepare "Repository as a server" for startup.
-
-        * register session clean up task.
-        """
-        if not (self.config.creating or self.config.repairing
-                or self.config.quick_start):
-            # register a task to cleanup expired session
-            if self._scheduler is not None:
-                self.cleanup_session_time = self.config['cleanup-session-time'] or 60 * 60 * 24
-                assert self.cleanup_session_time > 0
-                cleanup_session_interval = min(60 * 60, self.cleanup_session_time / 3)
-                self.looping_task(cleanup_session_interval, self.clean_sessions)
-
     def run_scheduler(self):
         """Start repository scheduler after preparing the repository for that.
 
         * trigger server startup hook,
-        * register session clean up task,
         * start the scheduler *and block*.
 
         XXX Other startup related stuffs are done elsewhere. In Repository
         XXX __init__ or in external codes (various server managers).
         """
-        self._prepare_startup()
         assert self._scheduler is not None, \
             "This Repository is not intended to be used as a server"
         self.info(
@@ -675,7 +655,6 @@
         session = Session(user, self)
         user._cw = user.cw_rset.req = session
         user.cw_clear_relation_cache()
-        self._sessions[session.sessionid] = session
         self.info('opened session %s for user %s', session.sessionid, login)
         with session.new_cnx() as cnx:
             self.hm.call_hooks('session_open', cnx)
@@ -690,20 +669,6 @@
 
     # session handling ########################################################
 
-    def clean_sessions(self):
-        """close sessions not used since an amount of time specified in the
-        configuration
-        """
-        mintime = time() - self.cleanup_session_time
-        self.debug('cleaning session unused since %s',
-                   strftime('%H:%M:%S', localtime(mintime)))
-        nbclosed = 0
-        for session in list(self._sessions.values()):
-            if session.timestamp < mintime:
-                session.close()
-                nbclosed += 1
-        return nbclosed
-
     @contextmanager
     def internal_cnx(self):
         """Context manager returning a Connection using internal user which have
--- a/cubicweb/server/session.py	Fri Mar 10 18:24:40 2017 +0100
+++ b/cubicweb/server/session.py	Fri Mar 10 14:04:26 2017 +0100
@@ -21,7 +21,6 @@
 
 import functools
 import sys
-from time import time
 from uuid import uuid4
 from warnings import warn
 from contextlib import contextmanager
@@ -257,9 +256,6 @@
         self.vreg = self.repo.vreg
         self._execute = self.repo.querier.execute
 
-        # other session utility
-        self._session_timestamp = session._timestamp
-
         # internal (root) session
         self.is_internal_session = isinstance(session.user, InternalManager)
 
@@ -756,10 +752,8 @@
 
         See :meth:`cubicweb.dbapi.Cursor.execute` documentation.
         """
-        self._session_timestamp.touch()
         rset = self._execute(self, rql, kwargs, build_descr)
         rset.req = self
-        self._session_timestamp.touch()
         return rset
 
     @_open_only
@@ -786,7 +780,6 @@
                 cnxset.rollback()
                 self.debug('rollback for transaction %s done', self.connectionid)
         finally:
-            self._session_timestamp.touch()
             self.clear()
 
     @_open_only
@@ -878,7 +871,6 @@
                 self.debug('postcommit transaction %s done', self.connectionid)
                 return self.transaction_uuid(set=False)
         finally:
-            self._session_timestamp.touch()
             self.clear()
 
     # resource accessors ######################################################
@@ -928,18 +920,6 @@
     return property(**args)
 
 
-class Timestamp(object):
-
-    def __init__(self):
-        self.value = time()
-
-    def touch(self):
-        self.value = time()
-
-    def __float__(self):
-        return float(self.value)
-
-
 class Session(object):
     """Repository user session
 
@@ -953,7 +933,6 @@
         self.sessionid = _id or make_uid(unormalize(user.login))
         self.user = user  # XXX repoapi: deprecated and store only a login.
         self.repo = repo
-        self._timestamp = Timestamp()
         self.data = {}
 
     def __unicode__(self):
@@ -961,10 +940,6 @@
             unicode(self.user.login), self.sessionid, id(self))
 
     @property
-    def timestamp(self):
-        return float(self._timestamp)
-
-    @property
     @deprecated('[3.19] session.id is deprecated, use session.sessionid')
     def id(self):
         return self.sessionid
@@ -987,18 +962,6 @@
                  stacklevel=2)
         return self.repo.get_option_value(option)
 
-    def _touch(self):
-        """update latest session usage timestamp and reset mode to read"""
-        self._timestamp.touch()
-
-    local_perm_cache = cnx_attr('local_perm_cache')
-
-    @local_perm_cache.setter
-    def local_perm_cache(self, value):
-        # base class assign an empty dict:-(
-        assert value == {}
-        pass
-
     # deprecated ###############################################################
 
     @property
--- a/cubicweb/server/test/unittest_serverctl.py	Fri Mar 10 18:24:40 2017 +0100
+++ b/cubicweb/server/test/unittest_serverctl.py	Fri Mar 10 14:04:26 2017 +0100
@@ -42,7 +42,7 @@
         # make sure repository scheduler started
         scheduler_start_message = (
             'INFO:cubicweb.repository:starting repository scheduler with '
-            'tasks: update_feeds, clean_sessions, expire_dataimports'
+            'tasks: update_feeds, expire_dataimports'
         )
         self.assertIn(scheduler_start_message, log_cm.output)
         # and that scheduler's run method got called