fixing the loop
This commit is contained in:
@@ -996,17 +996,29 @@ def _format_duration(seconds: float) -> str:
|
||||
|
||||
async def run_forever_sunday_noon():
|
||||
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
|
||||
|
||||
while True:
|
||||
now = datetime.now()
|
||||
days = (6 - now.weekday()) % 7
|
||||
target = (now + timedelta(days=days)).replace(hour=12, minute=0, second=0, microsecond=0)
|
||||
days_ahead = 6 - now.weekday()
|
||||
if days_ahead < 0:
|
||||
days_ahead += 7
|
||||
|
||||
target = (now + timedelta(days=days_ahead)).replace(
|
||||
hour=12, minute=0, second=0, microsecond=0
|
||||
)
|
||||
|
||||
# If it's already past this Sunday's noon, schedule for next week
|
||||
if target <= now:
|
||||
target += timedelta(days=7)
|
||||
|
||||
sleep_seconds = (target - now).total_seconds()
|
||||
|
||||
while sleep_seconds > 0:
|
||||
LOG.info("Waiting for %s for next scan", _format_duration(sleep_seconds))
|
||||
await asyncio.sleep(min(sleep_seconds, 5 * 60))
|
||||
now = datetime.now()
|
||||
sleep_seconds = (target - now).total_seconds()
|
||||
|
||||
|
||||
LOG.info("Going to run the weekly task")
|
||||
await run_weekly()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user