Add debug logs in session_redis

This commit is contained in:
Guewen Baconnier
2016-12-14 11:25:12 +01:00
parent 997f92aaab
commit 022f9003e8
2 changed files with 31 additions and 7 deletions
+9 -7
View File
@@ -26,15 +26,15 @@ def is_true(strval):
return bool(strtobool(strval or '0'.lower()))
host = os.environ.get('ODOO_SESSION_REDIS_HOST') or 'localhost'
port = int(os.environ.get('ODOO_SESSION_REDIS_PORT') or 6379)
prefix = os.environ.get('ODOO_SESSION_REDIS_PREFIX')
password = os.environ.get('ODOO_SESSION_REDIS_PASSWORD')
expiration = os.environ.get('ODOO_SESSION_REDIS_EXPIRATION')
@lazy_property
def session_store(self):
host = os.environ.get('ODOO_SESSION_REDIS_HOST') or 'localhost'
port = int(os.environ.get('ODOO_SESSION_REDIS_PORT') or 6379)
prefix = os.environ.get('ODOO_SESSION_REDIS_PREFIX')
password = os.environ.get('ODOO_SESSION_REDIS_PASSWORD')
expiration = os.environ.get('ODOO_SESSION_REDIS_EXPIRATION')
_logger.debug("HTTP sessions stored in Redis %s:%s with prefix '%s'",
host, port, prefix or '')
redis_client = redis.Redis(host=host, port=port, password=password)
return RedisSessionStore(redis=redis_client, prefix=prefix,
expiration=expiration,
@@ -60,6 +60,8 @@ def purge_fs_sessions(path):
if is_true(os.environ.get('ODOO_SESSION_REDIS')):
_logger.debug("HTTP sessions stored in Redis %s:%s with prefix '%s'",
host, port, prefix or '')
http.Root.session_store = session_store
http.session_gc = session_gc
# clean the existing sessions on the file system
+22
View File
@@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
import json
import logging
from werkzeug.contrib.sessions import SessionStore
@@ -10,6 +11,8 @@ from werkzeug.contrib.sessions import SessionStore
# openerp.http.session_gc()
DEFAULT_SESSION_TIMEOUT = 60 * 60 * 24 * 7 # 7 days in seconds
_logger = logging.getLogger(__name__)
class RedisSessionStore(SessionStore):
""" SessionStore that saves session to redis """
@@ -35,27 +38,46 @@ class RedisSessionStore(SessionStore):
def save(self, session):
key = self.build_key(session.sid)
if _logger.isEnabledFor(logging.DEBUG):
if session.uid:
user_msg = "user '%s' (id: %s)" % (
session.login, session.uid)
else:
user_msg = "anonymous user"
_logger.debug("saving session with key '%s' and "
"expiration of %s seconds for %s",
key, self.expiration, user_msg)
if self.redis.set(key, json.dumps(dict(session))):
return self.redis.expire(key, self.expiration)
def delete(self, session):
key = self.build_key(session.sid)
_logger.debug('deleting session with key %s', key)
return self.redis.delete(key)
def get(self, sid):
if not self.is_valid_key(sid):
_logger.debug("session with invalid sid '%s' has been asked, "
"returning a new one", sid)
return self.new()
key = self.build_key(sid)
saved = self.redis.get(key)
if not saved:
_logger.debug("session with non-existent key '%s' has been asked, "
"returning a new one", key)
return self.new()
try:
data = json.loads(saved)
except ValueError:
_logger.debug("session for key '%s' has been asked but its json "
"content could not be read, it has been reset", key)
data = {}
return self.session_class(data, sid, False)
def list(self):
keys = self.redis.keys('%s*' % self.prefix)
_logger.debug("a listing redis keys has been called")
return [key[len(self.prefix):] for key in keys]