Skip to content

Commit

Permalink
revert to use a file for lastcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Mar 22, 2024
1 parent f54f6cd commit e13091f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion feedsbot/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
16 changes: 12 additions & 4 deletions feedsbot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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")
Expand Down

0 comments on commit e13091f

Please sign in to comment.