Skip to content

Commit

Permalink
Merge pull request #3025 from sasirven/feature/manage-schedules-with-…
Browse files Browse the repository at this point in the history
…cron

Feature - Manage schedules with cron (1/3)
  • Loading branch information
elegantmoose authored Sep 25, 2024
2 parents 5521615 + 27b47d1 commit 7889b79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/objects/c_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Meta:
unknown = ma.EXCLUDE

id = ma.fields.String()
schedule = ma.fields.Time(required=True)
schedule = ma.fields.String(required=True)
task = ma.fields.Nested(OperationSchema())

@ma.post_load
Expand Down
18 changes: 15 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 All @@ -13,6 +14,7 @@
import jinja2
import yaml
from aiohttp import web
import croniter

from app.objects.c_plugin import Plugin
from app.service.interfaces.i_app_svc import AppServiceInterface
Expand Down Expand Up @@ -82,9 +84,19 @@ 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).time()
today_utc = datetime.now(timezone.utc).date()
diff = datetime.combine(today_utc, now) - datetime.combine(today_utc, s.schedule)
if not croniter.croniter.is_valid(s.schedule):
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:
self.log.debug('Pulling %s off the scheduler' % s.id)
sop = copy.deepcopy(s.task)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ dnspython==2.4.2
asyncssh==2.14.1
aioftp~=0.20.0
packaging==23.2
croniter~=3.0.3

0 comments on commit 7889b79

Please sign in to comment.