Skip to content

Commit

Permalink
Simplify checking for updates
Browse files Browse the repository at this point in the history
Checking for updates now happens in a single loop.
Now it shouldn't be possible to start checking for next update before previous one is finished.
  • Loading branch information
Electronic-Mango committed Jun 16, 2024
1 parent 31a9f8f commit a6e769e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main/bot/update_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
is handled in separate modules.
"""

from asyncio import sleep
from datetime import datetime
from random import randrange
from time import struct_time
Expand Down Expand Up @@ -42,15 +43,19 @@ async def _delayed_check_for_all_updates(context: ContextTypes.DEFAULT_TYPE) ->
logger.info("Quiet hour, skipping checking for updates")
return
logger.info("Starting checking for all updates")
delay = 0
for feed_data in get_all_stored_data():
context.job_queue.run_once(callback=_check_for_updates, when=delay, data=feed_data)
delay += LOOKUP_FEED_DELAY
delay += randrange(max(LOOKUP_FEED_DELAY_RANDOMNESS, 1)) # randrange(1) always returns 0
await _check_for_updates(context, *feed_data)
await sleep(LOOKUP_FEED_DELAY + randrange(max(LOOKUP_FEED_DELAY_RANDOMNESS, 1)))


async def _check_for_updates(context: ContextTypes.DEFAULT_TYPE) -> None:
chat_id, feed_type, feed_name, id, date = context.job.data
async def _check_for_updates(
context: ContextTypes.DEFAULT_TYPE,
chat_id: int,
feed_type: str,
feed_name: str,
id: str,
date: struct_time,
) -> None:
logger.info(f"[{chat_id}] Checking for updates for [{feed_name}] [{feed_type}]")
feed = get_parsed_feed(feed_type, feed_name)
if feed_is_valid(feed):
Expand Down

0 comments on commit a6e769e

Please sign in to comment.