Skip to content

Commit

Permalink
Add conversion from old schedules format to the new one
Browse files Browse the repository at this point in the history
Improve conversion with regex group
  • Loading branch information
sasirven authored and samuel-sirven-bib committed Sep 24, 2024
1 parent 0b7da83 commit 27b47d1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app/service/app_svc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hashlib
import json
import os
import re
import time
from collections import namedtuple
from datetime import datetime, timezone
Expand Down Expand Up @@ -83,11 +84,17 @@ async def run_scheduler(self):
while True:
interval = 60
for s in await self.get_service('data_svc').locate('schedules'):
now = datetime.now(timezone.utc)
if not croniter.croniter.is_valid(s.schedule):
self.log.warning(f"The schedule {s.id} with the format `{s.schedule}` is incompatible with cron!")
continue
match = re.match(r'^(\d{2}):(\d{2}):\d{2}\.\d{6}$', s.schedule)
if match:
hour, minute = match.groups()
s.schedule = f"{minute} {hour} * * *"
self.log.info(f"Converted time schedule {s.id} to cron format: {s.schedule}")
else:
self.log.warning(f"The schedule {s.id} with the format `{s.schedule}` is incompatible with cron!")
continue

now = datetime.now()
cron = croniter.croniter(s.schedule, now)
diff = now - cron.get_prev(datetime)
if interval > diff.total_seconds() > 0:
Expand Down

0 comments on commit 27b47d1

Please sign in to comment.