From e13091fdc8c8d4308c57087179e5501d060cff4d Mon Sep 17 00:00:00 2001 From: adbenitez Date: Fri, 22 Mar 2024 03:28:50 -0400 Subject: [PATCH] revert to use a file for lastcheck --- feedsbot/hooks.py | 2 +- feedsbot/util.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/feedsbot/hooks.py b/feedsbot/hooks.py index 4764b8a..d3dd0d3 100644 --- a/feedsbot/hooks.py +++ b/feedsbot/hooks.py @@ -78,7 +78,7 @@ def on_start(bot: Bot, args: Namespace) -> None: init(f"sqlite:///{config_dir / 'sqlite.db'}") Thread( target=check_feeds, - args=(bot, args.interval, args.parallel), + args=(bot, args.interval, args.parallel, config_dir), daemon=True, ).start() diff --git a/feedsbot/util.py b/feedsbot/util.py index 38a9c7c..5bbc0f5 100644 --- a/feedsbot/util.py +++ b/feedsbot/util.py @@ -6,6 +6,7 @@ import re import time from multiprocessing.pool import ThreadPool +from pathlib import Path from tempfile import NamedTemporaryFile from typing import Optional @@ -28,9 +29,15 @@ www.request = functools.partial(www.request, timeout=15) # type: ignore -def check_feeds(bot: Bot, interval: int, pool_size: int) -> None: - lastcheck_key = "ui.feedsbot.lastcheck" - lastcheck = float(bot.rpc.get_config(lastcheck_key) or 0) +def check_feeds(bot: Bot, interval: int, pool_size: int, app_dir: Path) -> None: + lastcheck_path = app_dir / "lastcheck.txt" + lastcheck = 0.0 + if lastcheck_path.exists(): + with lastcheck_path.open(encoding="utf-8") as lastcheck_file: + try: + lastcheck = float(lastcheck_file.read()) + except (ValueError, TypeError): + pass took = max(time.time() - lastcheck, 0) with ThreadPool(pool_size) as pool: @@ -41,7 +48,8 @@ def check_feeds(bot: Bot, interval: int, pool_size: int) -> None: time.sleep(delay) bot.logger.info("[WORKER] Starting to check feeds") lastcheck = time.time() - bot.rpc.set_config(lastcheck_key, str(lastcheck)) + with lastcheck_path.open("w", encoding="utf-8") as lastcheck_file: + lastcheck_file.write(str(lastcheck)) with session_scope() as session: feeds = session.execute(select(Feed)).scalars().all() bot.logger.info(f"[WORKER] There are {len(feeds)} feeds to check")