~credativ/openerp-web/6.1-fixes

« back to all changes in this revision

Viewing changes to addons/web/common/web_session.py

  • Committer: Ondřej Kuzník
  • Date: 2013-06-26 11:54:53 UTC
  • Revision ID: ondrej.kuznik@credativ.co.uk-20130626115453-jzhw2tu4lz2x9qct
[IMP] Implement server-wide session expiration

werkzeug documentation states:
"[sessions] module does not implement methods or ways to check if a session is
expired. That should be done by a cronjob and storage specific. For
example to prune unused filesystem sessions one could check the modified
time of the files. It sessions are stored in the database the new()
method should add an expiration timestamp for the session."

This does not implement the modification time though, just reuses the
expiration predicate used in web.common.http...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from osv import osv
 
2
from . import http
 
3
 
 
4
import logging
 
5
import time
 
6
 
 
7
class web_session(osv.osv):
 
8
    _name = "web.session"
 
9
    _logger = logging.getLogger(_name)
 
10
 
 
11
    def expire(self, cr, uid):
 
12
        expire_interval = self.pool.get('ir.config_parameter').get_param(cr, uid, 'session_expiration_interval')
 
13
        expire_interval = int(expire_interval)
 
14
        self._logger.info("Expiring all sessions older than %d seconds", expire_interval)
 
15
        for store in http.STORES:
 
16
            session_store, session_lock = http.STORES[store]
 
17
            self._logger.debug("Expiring session store %s", store)
 
18
            with session_lock:
 
19
                sessions_total = 0
 
20
                sessions_expired = 0
 
21
                for session in (session_store.get(x) for x in session_store.list()):
 
22
                    sessions_total += 1
 
23
                    for key, value in session.iteritems():
 
24
                        if http.session_should_expire(value, expire_interval):
 
25
                            del session[key]
 
26
                    if not session:
 
27
                        sessions_expired += 1
 
28
                        session_store.delete(session)
 
29
                    else:
 
30
                        session_store.save_if_modified(session)
 
31
                self._logger.debug("Session store '%s' expiration finished, expired %d/%d sessions", store, sessions_expired, sessions_total)
 
32
        self._logger.debug("Expiration job finished")