From 7c700c7b76dceabdb814d18b2cfce696d480b46e Mon Sep 17 00:00:00 2001 From: Andreas Perhab Date: Tue, 7 Apr 2020 11:26:33 +0200 Subject: [PATCH 1/3] [FIX] session_redis: add required option 'post_load' for server wide addon --- session_redis/__manifest__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/session_redis/__manifest__.py b/session_redis/__manifest__.py index 2498a86..5daa4d4 100644 --- a/session_redis/__manifest__.py +++ b/session_redis/__manifest__.py @@ -16,4 +16,5 @@ 'website': 'http://www.camptocamp.com', 'data': [], 'installable': True, + 'post_load': False, } From 469daae4358ed5f70626e7d9a65c54114a95f162 Mon Sep 17 00:00:00 2001 From: Andreas Perhab Date: Tue, 7 Apr 2020 12:19:41 +0200 Subject: [PATCH 2/3] [IMP] session_redis: add option to copy sessions from filesystem when starting --- session_redis/README.rst | 12 ++++++++++-- session_redis/http.py | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/session_redis/README.rst b/session_redis/README.rst index 949f8b4..e4e32ec 100644 --- a/session_redis/README.rst +++ b/session_redis/README.rst @@ -18,6 +18,10 @@ The storage of sessions in Redis is activated using environment variables. the sessions (default is 7 days) * ``ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS`` is the time in seconds before expiration of 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:``. @@ -32,8 +36,12 @@ Limitations * The server has to be restarted in order for the sessions to be stored in 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 method when the Redis mode is active, so incompatibilities with other addons 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`` diff --git a/session_redis/http.py b/session_redis/http.py index a4b5c17..57eed19 100644 --- a/session_redis/http.py +++ b/session_redis/http.py @@ -77,6 +77,20 @@ def purge_fs_sessions(path): pass +def copy_fs_sessions(path): + from odoo.http import OpenERPSession + import werkzeug.contrib.sessions + werkzeug_session_store = werkzeug.contrib.sessions.FilesystemSessionStore(path, session_class=OpenERPSession) + session_store = http.Root().session_store + filename_prefix='werkzeug_' + filename_suffix = '.sess' + + for fname in os.listdir(path): + path = os.path.join(path, fname) + session = werkzeug_session_store.get(fname[len(filename_prefix):len(filename_suffix) * -1]) + session_store.save(session) + + if is_true(os.environ.get('ODOO_SESSION_REDIS')): if sentinel_host: _logger.debug("HTTP sessions stored in Redis with prefix '%s'. " @@ -88,5 +102,9 @@ if is_true(os.environ.get('ODOO_SESSION_REDIS')): http.Root.session_store = session_store http.session_gc = session_gc - # clean the existing sessions on the file system - purge_fs_sessions(odoo.tools.config.session_dir) + + 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 + purge_fs_sessions(odoo.tools.config.session_dir) From 5983397e685d645844c6969ac189f056b99567e4 Mon Sep 17 00:00:00 2001 From: Andreas Perhab Date: Wed, 15 Apr 2020 10:30:44 +0200 Subject: [PATCH 3/3] [FIX] session_redis: fix flake8 errors --- session_redis/__init__.py | 2 -- session_redis/__manifest__.py | 1 - session_redis/http.py | 13 ++++++------- session_redis/session.py | 1 - 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/session_redis/__init__.py b/session_redis/__init__.py index af491fc..a64bcb4 100644 --- a/session_redis/__init__.py +++ b/session_redis/__init__.py @@ -1,4 +1,2 @@ -# -*- coding: utf-8 -*- - from . import http from . import session diff --git a/session_redis/__manifest__.py b/session_redis/__manifest__.py index 5daa4d4..89c7f78 100644 --- a/session_redis/__manifest__.py +++ b/session_redis/__manifest__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2016 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) diff --git a/session_redis/http.py b/session_redis/http.py index 57eed19..77c6417 100644 --- a/session_redis/http.py +++ b/session_redis/http.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2016 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) @@ -79,15 +78,15 @@ def purge_fs_sessions(path): def copy_fs_sessions(path): from odoo.http import OpenERPSession - import werkzeug.contrib.sessions - werkzeug_session_store = werkzeug.contrib.sessions.FilesystemSessionStore(path, session_class=OpenERPSession) + from werkzeug.contrib.sessions import FilesystemSessionStore + werkzeug_session_store = FilesystemSessionStore(path, session_class=OpenERPSession) session_store = http.Root().session_store - filename_prefix='werkzeug_' - filename_suffix = '.sess' + filename_prefix_len = len('werkzeug_') + filename_suffix_len = len('.sess') for fname in os.listdir(path): - path = os.path.join(path, fname) - session = werkzeug_session_store.get(fname[len(filename_prefix):len(filename_suffix) * -1]) + session_file = fname[filename_prefix_len:filename_suffix_len * -1] + session = werkzeug_session_store.get(session_file) session_store.save(session) diff --git a/session_redis/session.py b/session_redis/session.py index 00e4433..de54509 100644 --- a/session_redis/session.py +++ b/session_redis/session.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2016 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)