changing scheduler
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user