From 38035377399885f4a46d38f4ecfd409fcacc100b Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 23 Aug 2023 18:35:20 +0700 Subject: [PATCH] trigger bgsave on importent redis set_message --- tubearchivist/home/src/download/yt_dlp_base.py | 4 ++-- tubearchivist/home/src/ta/config.py | 6 +++--- tubearchivist/home/src/ta/ta_redis.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/tubearchivist/home/src/download/yt_dlp_base.py b/tubearchivist/home/src/download/yt_dlp_base.py index 526dbd4..11478dd 100644 --- a/tubearchivist/home/src/download/yt_dlp_base.py +++ b/tubearchivist/home/src/download/yt_dlp_base.py @@ -112,9 +112,9 @@ class CookieHandler: def set_cookie(self, cookie): """set cookie str and activate in cofig""" - RedisArchivist().set_message("cookie", cookie) + RedisArchivist().set_message("cookie", cookie, save=True) path = ".downloads.cookie_import" - RedisArchivist().set_message("config", True, path=path) + RedisArchivist().set_message("config", True, path=path, save=True) self.config["downloads"]["cookie_import"] = True print("cookie: activated and stored in Redis") diff --git a/tubearchivist/home/src/ta/config.py b/tubearchivist/home/src/ta/config.py index d1cccce..4946bee 100644 --- a/tubearchivist/home/src/ta/config.py +++ b/tubearchivist/home/src/ta/config.py @@ -100,7 +100,7 @@ class AppConfig: self.config[config_dict][config_value] = to_write updated.append((config_value, to_write)) - RedisArchivist().set_message("config", self.config) + RedisArchivist().set_message("config", self.config, save=True) return updated @staticmethod @@ -112,7 +112,7 @@ class AppConfig: message = {"status": value} redis_key = f"{user_id}:{key}" - RedisArchivist().set_message(redis_key, message) + RedisArchivist().set_message(redis_key, message, save=True) def get_colors(self): """overwrite config if user has set custom values""" @@ -225,7 +225,7 @@ class ScheduleBuilder: to_write = value redis_config["scheduler"][key] = to_write - RedisArchivist().set_message("config", redis_config) + RedisArchivist().set_message("config", redis_config, save=True) mess_dict = { "status": self.MSG, "level": "info", diff --git a/tubearchivist/home/src/ta/ta_redis.py b/tubearchivist/home/src/ta/ta_redis.py index 46b1b5c..77de528 100644 --- a/tubearchivist/home/src/ta/ta_redis.py +++ b/tubearchivist/home/src/ta/ta_redis.py @@ -41,6 +41,7 @@ class RedisArchivist(RedisBase): message: dict, path: str = ".", expire: bool | int = False, + save: bool = False, ) -> None: """write new message to redis""" self.conn.execute_command( @@ -54,6 +55,16 @@ class RedisArchivist(RedisBase): secs = expire self.conn.execute_command("EXPIRE", self.NAME_SPACE + key, secs) + if save: + self.bg_save() + + def bg_save(self) -> None: + """save to aof""" + try: + self.conn.bgsave() + except redis.exceptions.ResponseError: + pass + def get_message(self, key: str) -> dict: """get message dict from redis""" reply = self.conn.execute_command("JSON.GET", self.NAME_SPACE + key)