changing scheduler
This commit is contained in:
@@ -982,25 +982,33 @@ async def run_weekly():
|
|||||||
|
|
||||||
# -------------------- Scheduler --------------------
|
# -------------------- 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():
|
async def run_forever_sunday_noon():
|
||||||
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
|
||||||
"""Run at next Sunday 12:00 Europe/Brussels, then every 7 days."""
|
|
||||||
while True:
|
while True:
|
||||||
now = datetime.now(TZ)
|
now = datetime.now()
|
||||||
# days until Sunday (weekday(): Monday=0..Sunday=6)
|
|
||||||
days = (6 - now.weekday()) % 7
|
days = (6 - now.weekday()) % 7
|
||||||
target = (now + timedelta(days=days)).replace(hour=12, minute=0, second=0, microsecond=0)
|
target = (now + timedelta(days=days)).replace(hour=12, minute=0, second=0, microsecond=0)
|
||||||
if target <= now:
|
sleep_seconds = (target - now).total_seconds()
|
||||||
target = target + timedelta(days=7)
|
while sleep_seconds > 0:
|
||||||
wait = (target - now).total_seconds()
|
LOG.info("Waiting for %s for next scan", _format_duration(sleep_seconds))
|
||||||
LOG.info("Next run at %s (in %.0f min)", target.isoformat(), wait/60)
|
await asyncio.sleep(min(sleep_seconds, 5 * 60))
|
||||||
await asyncio.sleep(wait)
|
now = datetime.now()
|
||||||
try:
|
sleep_seconds = (target - now).total_seconds()
|
||||||
|
|
||||||
|
LOG.info("Going to run the weekly task")
|
||||||
await run_weekly()
|
await run_weekly()
|
||||||
except Exception as e:
|
|
||||||
LOG.exception("Weekly run failed: %s", e)
|
|
||||||
# then sleep 7 days
|
|
||||||
await asyncio.sleep(7 * 24 * 3600)
|
|
||||||
|
|
||||||
# -------------------- Entrypoint --------------------
|
# -------------------- Entrypoint --------------------
|
||||||
|
|
||||||
@@ -1009,6 +1017,7 @@ async def main():
|
|||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--runonce", action="store_true", help="Run now and exit (no scheduler)")
|
parser.add_argument("--runonce", action="store_true", help="Run now and exit (no scheduler)")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.runonce:
|
if args.runonce:
|
||||||
await run_weekly()
|
await run_weekly()
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user