fixing the loop
This commit is contained in:
@@ -996,17 +996,29 @@ def _format_duration(seconds: float) -> str:
|
|||||||
|
|
||||||
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")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
days = (6 - now.weekday()) % 7
|
days_ahead = 6 - now.weekday()
|
||||||
target = (now + timedelta(days=days)).replace(hour=12, minute=0, second=0, microsecond=0)
|
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()
|
sleep_seconds = (target - now).total_seconds()
|
||||||
|
|
||||||
while sleep_seconds > 0:
|
while sleep_seconds > 0:
|
||||||
LOG.info("Waiting for %s for next scan", _format_duration(sleep_seconds))
|
LOG.info("Waiting for %s for next scan", _format_duration(sleep_seconds))
|
||||||
await asyncio.sleep(min(sleep_seconds, 5 * 60))
|
await asyncio.sleep(min(sleep_seconds, 5 * 60))
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
sleep_seconds = (target - now).total_seconds()
|
sleep_seconds = (target - now).total_seconds()
|
||||||
|
|
||||||
LOG.info("Going to run the weekly task")
|
LOG.info("Going to run the weekly task")
|
||||||
await run_weekly()
|
await run_weekly()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user