web/application.py
changeset 9897 fa44db7da2dc
parent 9728 f79ce7b39ee9
parent 9889 9b42e55e0686
child 10001 1245357b3b3e
equal deleted inserted replaced
9892:928732ec00dd 9897:fa44db7da2dc
    33 
    33 
    34 from cubicweb import set_log_methods, cwvreg
    34 from cubicweb import set_log_methods, cwvreg
    35 from cubicweb import (
    35 from cubicweb import (
    36     ValidationError, Unauthorized, Forbidden,
    36     ValidationError, Unauthorized, Forbidden,
    37     AuthenticationError, NoSelectableObject,
    37     AuthenticationError, NoSelectableObject,
    38     BadConnectionId, CW_EVENT_MANAGER)
    38     CW_EVENT_MANAGER)
    39 from cubicweb.repoapi import anonymous_cnx
    39 from cubicweb.repoapi import anonymous_cnx
    40 from cubicweb.web import LOGGER, component, cors
    40 from cubicweb.web import LOGGER, component, cors
    41 from cubicweb.web import (
    41 from cubicweb.web import (
    42     StatusResponse, DirectResponse, Redirect, NotFound, LogOut,
    42     StatusResponse, DirectResponse, Redirect, NotFound, LogOut,
    43     RemoteCallFailed, InvalidSession, RequestError, PublishException)
    43     RemoteCallFailed, InvalidSession, RequestError, PublishException)
    86         self.debug('cleaning http sessions')
    86         self.debug('cleaning http sessions')
    87         session_time = self.session_time
    87         session_time = self.session_time
    88         closed, total = 0, 0
    88         closed, total = 0, 0
    89         for session in self.current_sessions():
    89         for session in self.current_sessions():
    90             total += 1
    90             total += 1
    91             try:
    91             last_usage_time = session.mtime
    92                 last_usage_time = session.cnx.check()
    92             no_use_time = (time() - last_usage_time)
    93             except AttributeError:
    93             if session.anonymous_session:
    94                 last_usage_time = session.mtime
    94                 if no_use_time >= self.cleanup_anon_session_time:
    95             except BadConnectionId:
    95                     self.close_session(session)
       
    96                     closed += 1
       
    97             elif session_time is not None and no_use_time >= session_time:
    96                 self.close_session(session)
    98                 self.close_session(session)
    97                 closed += 1
    99                 closed += 1
    98             else:
       
    99                 no_use_time = (time() - last_usage_time)
       
   100                 if session.anonymous_session:
       
   101                     if no_use_time >= self.cleanup_anon_session_time:
       
   102                         self.close_session(session)
       
   103                         closed += 1
       
   104                 elif session_time is not None and no_use_time >= session_time:
       
   105                     self.close_session(session)
       
   106                     closed += 1
       
   107         return closed, total - closed
   100         return closed, total - closed
   108 
   101 
   109     def current_sessions(self):
   102     def current_sessions(self):
   110         """return currently open sessions"""
   103         """return currently open sessions"""
   111         raise NotImplementedError()
   104         raise NotImplementedError()
   298 
   291 
   299     def log_handle_request(self, req, path):
   292     def log_handle_request(self, req, path):
   300         """wrapper around _publish to log all queries executed for a given
   293         """wrapper around _publish to log all queries executed for a given
   301         accessed path
   294         accessed path
   302         """
   295         """
       
   296         def wrap_set_cnx(func):
       
   297             def wrap_execute(cnx):
       
   298                 orig_execute = cnx.execute
       
   299                 def execute(rql, kwargs=None, build_descr=True):
       
   300                     tstart, cstart = time(), clock()
       
   301                     rset = orig_execute(rql, kwargs, build_descr=build_descr)
       
   302                     cnx.executed_queries.append((rql, kwargs, time() - tstart, clock() - cstart))
       
   303                     return rset
       
   304                 return execute
       
   305             def set_cnx(cnx):
       
   306                 func(cnx)
       
   307                 cnx.execute = wrap_execute(cnx)
       
   308                 cnx.executed_queries = []
       
   309             return set_cnx
       
   310         req.set_cnx = wrap_set_cnx(req.set_cnx)
   303         try:
   311         try:
   304             return self.main_handle_request(req, path)
   312             return self.main_handle_request(req, path)
   305         finally:
   313         finally:
   306             cnx = req.cnx
   314             cnx = req.cnx
   307             if cnx:
   315             if cnx: