Merge pull request #177 from ap-wtioit/11.0-fix_session_redis

11.0 session redis preserve sessions
This commit is contained in:
Guewen Baconnier
2020-05-11 09:38:57 +02:00
committed by GitHub
co-authored by GitHub
5 changed files with 31 additions and 9 deletions
+10 -2
View File
@@ -18,6 +18,10 @@ The storage of sessions in Redis is activated using environment variables.
the sessions (default is 7 days) the sessions (default is 7 days)
* ``ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS`` is the time in seconds before expiration of * ``ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS`` is the time in seconds before expiration of
the anonymous sessions (default is 3 hours) the anonymous sessions (default is 3 hours)
* ``ODOO_SESSION_REDIS_COPY_EXISTING_FS_SESSIONS`` when ``1`` or ``true`` copies the existing odoo sessions from
the filesystem to redis when starting
* ``ODOO_SESSION_REDIS_PURGE_EXISTING_FS_SESSIONS`` when ``1`` or ``true`` deletes the existing odoo sessions from
the filesystem to redis when starting
The keys are set to ``session:<session id>``. The keys are set to ``session:<session id>``.
@@ -32,8 +36,12 @@ Limitations
* The server has to be restarted in order for the sessions to be stored in * The server has to be restarted in order for the sessions to be stored in
Redis. Redis.
* All the users will have to login again as their previous session will be
dropped.
* The addon monkey-patch ``odoo.http.Root.session_store`` with a custom * The addon monkey-patch ``odoo.http.Root.session_store`` with a custom
method when the Redis mode is active, so incompatibilities with other addons method when the Redis mode is active, so incompatibilities with other addons
is possible if they do the same. is possible if they do the same.
Preseve Sessions
----------------
In order to preserve sessions when switching to redis run odoo with
``ODOO_SESSION_REDIS_COPY_EXISTING_FS_SESSIONS=true odoo --load session_redis --stop-after-init``
-2
View File
@@ -1,4 +1,2 @@
# -*- coding: utf-8 -*-
from . import http from . import http
from . import session from . import session
+1 -1
View File
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Camptocamp SA # Copyright 2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
@@ -16,4 +15,5 @@
'website': 'http://www.camptocamp.com', 'website': 'http://www.camptocamp.com',
'data': [], 'data': [],
'installable': True, 'installable': True,
'post_load': False,
} }
+18 -1
View File
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Camptocamp SA # Copyright 2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
@@ -77,6 +76,20 @@ def purge_fs_sessions(path):
pass pass
def copy_fs_sessions(path):
from odoo.http import OpenERPSession
from werkzeug.contrib.sessions import FilesystemSessionStore
werkzeug_session_store = FilesystemSessionStore(path, session_class=OpenERPSession)
session_store = http.Root().session_store
filename_prefix_len = len('werkzeug_')
filename_suffix_len = len('.sess')
for fname in os.listdir(path):
session_file = fname[filename_prefix_len:filename_suffix_len * -1]
session = werkzeug_session_store.get(session_file)
session_store.save(session)
if is_true(os.environ.get('ODOO_SESSION_REDIS')): if is_true(os.environ.get('ODOO_SESSION_REDIS')):
if sentinel_host: if sentinel_host:
_logger.debug("HTTP sessions stored in Redis with prefix '%s'. " _logger.debug("HTTP sessions stored in Redis with prefix '%s'. "
@@ -88,5 +101,9 @@ if is_true(os.environ.get('ODOO_SESSION_REDIS')):
http.Root.session_store = session_store http.Root.session_store = session_store
http.session_gc = session_gc http.session_gc = session_gc
if is_true(os.environ.get('ODOO_SESSION_REDIS_COPY_EXISTING_FS_SESSIONS')):
copy_fs_sessions(odoo.tools.config.session_dir)
if is_true(os.environ.get('ODOO_SESSION_REDIS_PURGE_EXISTING_FS_SESSIONS')):
# clean the existing sessions on the file system # clean the existing sessions on the file system
purge_fs_sessions(odoo.tools.config.session_dir) purge_fs_sessions(odoo.tools.config.session_dir)
-1
View File
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Camptocamp SA # Copyright 2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)