From 85b4ea9e247babe13f7b92368aabb9ea9f5f41d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl?= Date: Mon, 17 Nov 2025 13:20:37 +0100 Subject: [PATCH] changing scheduler --- presquegratos.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/presquegratos.py b/presquegratos.py index 6c0f373..6f08251 100644 --- a/presquegratos.py +++ b/presquegratos.py @@ -982,25 +982,33 @@ async def run_weekly(): # -------------------- Scheduler -------------------- +def _format_duration(seconds: float) -> str: + seconds = int(seconds) + days, seconds = divmod(seconds, 86400) + hours, seconds = divmod(seconds, 3600) + minutes, seconds = divmod(seconds, 60) + parts = [] + if days: parts.append(f"{days} days") + if hours: parts.append(f"{hours} hours") + if minutes: parts.append(f"{minutes} minutes") + if seconds: parts.append(f"{seconds} seconds") + return ", ".join(parts) if parts else "0 seconds" + async def run_forever_sunday_noon(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") - """Run at next Sunday 12:00 Europe/Brussels, then every 7 days.""" while True: - now = datetime.now(TZ) - # days until Sunday (weekday(): Monday=0..Sunday=6) + now = datetime.now() days = (6 - now.weekday()) % 7 target = (now + timedelta(days=days)).replace(hour=12, minute=0, second=0, microsecond=0) - if target <= now: - target = target + timedelta(days=7) - wait = (target - now).total_seconds() - LOG.info("Next run at %s (in %.0f min)", target.isoformat(), wait/60) - await asyncio.sleep(wait) - try: - await run_weekly() - except Exception as e: - LOG.exception("Weekly run failed: %s", e) - # then sleep 7 days - await asyncio.sleep(7 * 24 * 3600) + 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() # -------------------- Entrypoint -------------------- @@ -1009,6 +1017,7 @@ async def main(): parser = argparse.ArgumentParser() parser.add_argument("--runonce", action="store_true", help="Run now and exit (no scheduler)") args = parser.parse_args() + if args.runonce: await run_weekly() else: