mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-23 18:04:34 +00:00
Merge pull request #482 from camptocamp/18.0-mig-session_redis_fix
[MIG] session_redis: Migration to 18.0
This commit is contained in:
@@ -14,6 +14,5 @@
|
|||||||
"python": ["redis"],
|
"python": ["redis"],
|
||||||
},
|
},
|
||||||
"website": "https://github.com/camptocamp/odoo-cloud-platform",
|
"website": "https://github.com/camptocamp/odoo-cloud-platform",
|
||||||
"data": [],
|
"installable": True,
|
||||||
"installable": False,
|
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-11
@@ -25,21 +25,21 @@ def is_true(strval):
|
|||||||
return bool(strtobool(strval or "0".lower()))
|
return bool(strtobool(strval or "0".lower()))
|
||||||
|
|
||||||
|
|
||||||
sentinel_host = os.environ.get("ODOO_SESSION_REDIS_SENTINEL_HOST")
|
sentinel_host = os.getenv("ODOO_SESSION_REDIS_SENTINEL_HOST")
|
||||||
sentinel_master_name = os.environ.get("ODOO_SESSION_REDIS_SENTINEL_MASTER_NAME")
|
sentinel_master_name = os.getenv("ODOO_SESSION_REDIS_SENTINEL_MASTER_NAME")
|
||||||
if sentinel_host and not sentinel_master_name:
|
if sentinel_host and not sentinel_master_name:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
"ODOO_SESSION_REDIS_SENTINEL_MASTER_NAME must be defined "
|
"ODOO_SESSION_REDIS_SENTINEL_MASTER_NAME must be defined "
|
||||||
"when using session_redis"
|
"when using session_redis"
|
||||||
)
|
)
|
||||||
sentinel_port = int(os.environ.get("ODOO_SESSION_REDIS_SENTINEL_PORT", 26379))
|
sentinel_port = int(os.getenv("ODOO_SESSION_REDIS_SENTINEL_PORT", 26379))
|
||||||
host = os.environ.get("ODOO_SESSION_REDIS_HOST", "localhost")
|
host = os.getenv("ODOO_SESSION_REDIS_HOST", "localhost")
|
||||||
port = int(os.environ.get("ODOO_SESSION_REDIS_PORT", 6379))
|
port = int(os.getenv("ODOO_SESSION_REDIS_PORT", 6379))
|
||||||
prefix = os.environ.get("ODOO_SESSION_REDIS_PREFIX")
|
prefix = os.getenv("ODOO_SESSION_REDIS_PREFIX")
|
||||||
url = os.environ.get("ODOO_SESSION_REDIS_URL")
|
url = os.getenv("ODOO_SESSION_REDIS_URL")
|
||||||
password = os.environ.get("ODOO_SESSION_REDIS_PASSWORD")
|
password = os.getenv("ODOO_SESSION_REDIS_PASSWORD")
|
||||||
expiration = os.environ.get("ODOO_SESSION_REDIS_EXPIRATION")
|
expiration = os.getenv("ODOO_SESSION_REDIS_EXPIRATION")
|
||||||
anon_expiration = os.environ.get("ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS")
|
anon_expiration = os.getenv("ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS")
|
||||||
|
|
||||||
|
|
||||||
@lazy_property
|
@lazy_property
|
||||||
@@ -61,6 +61,10 @@ def session_store(self):
|
|||||||
|
|
||||||
|
|
||||||
def purge_fs_sessions(path):
|
def purge_fs_sessions(path):
|
||||||
|
if not os.path.isdir(path):
|
||||||
|
_logger.warning(f"Session directory '{path}' does not exist.")
|
||||||
|
return
|
||||||
|
|
||||||
for fname in os.listdir(path):
|
for fname in os.listdir(path):
|
||||||
path = os.path.join(path, fname)
|
path = os.path.join(path, fname)
|
||||||
try:
|
try:
|
||||||
@@ -69,7 +73,7 @@ def purge_fs_sessions(path):
|
|||||||
_logger.warning("OS Error during purge of redis sessions.")
|
_logger.warning("OS Error during purge of redis sessions.")
|
||||||
|
|
||||||
|
|
||||||
if is_true(os.environ.get("ODOO_SESSION_REDIS")):
|
if is_true(os.getenv("ODOO_SESSION_REDIS")):
|
||||||
if sentinel_host:
|
if sentinel_host:
|
||||||
_logger.debug(
|
_logger.debug(
|
||||||
"HTTP sessions stored in Redis with prefix '%s'. "
|
"HTTP sessions stored in Redis with prefix '%s'. "
|
||||||
|
|||||||
@@ -60,10 +60,8 @@ class RedisSessionStore(SessionStore):
|
|||||||
else:
|
else:
|
||||||
user_msg = "anonymous user"
|
user_msg = "anonymous user"
|
||||||
_logger.debug(
|
_logger.debug(
|
||||||
"saving session with key '%s' and " "expiration of %s seconds for %s",
|
f"saving session with key '{key}' and "
|
||||||
key,
|
f"expiration of {expiration} seconds for {user_msg}"
|
||||||
expiration,
|
|
||||||
user_msg,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
data = json.dumps(dict(session), cls=json_encoding.SessionEncoder).encode(
|
data = json.dumps(dict(session), cls=json_encoding.SessionEncoder).encode(
|
||||||
@@ -74,14 +72,14 @@ class RedisSessionStore(SessionStore):
|
|||||||
|
|
||||||
def delete(self, session):
|
def delete(self, session):
|
||||||
key = self.build_key(session.sid)
|
key = self.build_key(session.sid)
|
||||||
_logger.debug("deleting session with key %s", key)
|
_logger.debug(f"deleting session with key {key}")
|
||||||
return self.redis.delete(key)
|
return self.redis.delete(key)
|
||||||
|
|
||||||
def get(self, sid):
|
def get(self, sid):
|
||||||
if not self.is_valid_key(sid):
|
if not self.is_valid_key(sid):
|
||||||
_logger.debug(
|
_logger.debug(
|
||||||
"session with invalid sid '%s' has been asked, " "returning a new one",
|
f"session with invalid sid '{sid}' has been asked, "
|
||||||
sid,
|
"returning a new one"
|
||||||
)
|
)
|
||||||
return self.new()
|
return self.new()
|
||||||
|
|
||||||
@@ -89,18 +87,16 @@ class RedisSessionStore(SessionStore):
|
|||||||
saved = self.redis.get(key)
|
saved = self.redis.get(key)
|
||||||
if not saved:
|
if not saved:
|
||||||
_logger.debug(
|
_logger.debug(
|
||||||
"session with non-existent key '%s' has been asked, "
|
f"session with non-existent key '{key}' has been asked, "
|
||||||
"returning a new one",
|
"returning a new one"
|
||||||
key,
|
|
||||||
)
|
)
|
||||||
return self.new()
|
return self.new()
|
||||||
try:
|
try:
|
||||||
data = json.loads(saved.decode("utf-8"), cls=json_encoding.SessionDecoder)
|
data = json.loads(saved.decode("utf-8"), cls=json_encoding.SessionDecoder)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_logger.debug(
|
_logger.debug(
|
||||||
"session for key '%s' has been asked but its json "
|
f"session for key '{key}' has been asked but its json "
|
||||||
"content could not be read, it has been reset",
|
"content could not be read, it has been reset"
|
||||||
key,
|
|
||||||
)
|
)
|
||||||
data = {}
|
data = {}
|
||||||
return self.session_class(data, sid, False)
|
return self.session_class(data, sid, False)
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ _MAP = {
|
|||||||
|
|
||||||
|
|
||||||
def strtobool(value):
|
def strtobool(value):
|
||||||
try:
|
result = _MAP.get(str(value).strip().lower())
|
||||||
return _MAP[str(value).lower()]
|
|
||||||
except KeyError as error:
|
if result is None:
|
||||||
raise ValueError(f'"{value}" is not a valid bool value') from error
|
raise ValueError(f"Invalid boolean value: {repr(value)}")
|
||||||
|
|
||||||
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user