Skip to content

Commit 37cac73

Browse files
authored
Add Redis keepalive background updater on startup (#500)
1 parent cbe5e8a commit 37cac73

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

pyUltroid/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def main():
1818
WasItRestart,
1919
autopilot,
2020
customize,
21+
keep_redis_alive,
2122
plug,
2223
ready,
2324
startup_stuff,
@@ -49,6 +50,7 @@ def main():
4950
LOGS.info("Initialising...")
5051

5152
ultroid_bot.run_in_loop(autopilot())
53+
ultroid_bot.loop.create_task(keep_redis_alive())
5254

5355
pmbot = udB.get_key("PMBOT")
5456
manager = udB.get_key("MANAGER")

pyUltroid/startup/_database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,11 @@ def __init__(
274274
if platform.lower() == "qovery" and not host:
275275
var, hash_, host, password = "", "", "", ""
276276
for vars_ in os.environ:
277-
if vars_.startswith("QOVERY_REDIS_") and vars.endswith("_HOST"):
277+
if vars_.startswith("QOVERY_REDIS_") and vars_.endswith("_HOST"):
278278
var = vars_
279279
if var:
280280
hash_ = var.split("_", maxsplit=2)[1].split("_")[0]
281-
if hash:
281+
if hash_:
282282
kwargs["host"] = os.environ.get(f"QOVERY_REDIS_{hash_}_HOST")
283283
kwargs["port"] = os.environ.get(f"QOVERY_REDIS_{hash_}_PORT")
284284
kwargs["password"] = os.environ.get(f"QOVERY_REDIS_{hash_}_PASSWORD")

pyUltroid/startup/funcs.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import random
1111
import shutil
1212
import time
13+
from datetime import datetime, timezone as dt_timezone
1314
from random import randint
1415

1516
from ..configs import Var
@@ -47,6 +48,8 @@
4748
from ..fns.helper import download_file, inline_mention, updater
4849

4950
db_url = 0
51+
REDIS_KEEPALIVE_KEY = "KEEP_ACTIVE"
52+
REDIS_KEEPALIVE_INTERVAL_SECONDS = 7 * 24 * 60 * 60
5053

5154

5255
async def autoupdate_local_database():
@@ -152,6 +155,33 @@ async def startup_stuff():
152155
time.tzset()
153156

154157

158+
async def keep_redis_alive():
159+
from .. import udB
160+
161+
if udB.name != "Redis":
162+
return
163+
164+
interval = udB.get_key("REDIS_KEEPALIVE_INTERVAL")
165+
try:
166+
interval = int(interval) if interval else REDIS_KEEPALIVE_INTERVAL_SECONDS
167+
except (TypeError, ValueError):
168+
interval = REDIS_KEEPALIVE_INTERVAL_SECONDS
169+
interval = max(interval, 60)
170+
171+
while True:
172+
try:
173+
now = datetime.now(dt_timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
174+
udB.set_key(REDIS_KEEPALIVE_KEY, f"Updated value at {now}")
175+
LOGS.debug(
176+
"Redis keepalive updated key '%s' (next run in %s seconds).",
177+
REDIS_KEEPALIVE_KEY,
178+
interval,
179+
)
180+
except Exception as exc:
181+
LOGS.warning("Redis keepalive update failed: %s", exc)
182+
await asyncio.sleep(interval)
183+
184+
155185
async def autobot():
156186
from .. import udB, ultroid_bot
157187

pyUltroid/version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "2025.05.31"
2-
ultroid_version = "2.1.1"
1+
__version__ = "2026.04.03"
2+
ultroid_version = "2.1.2"

0 commit comments

Comments
 (0)