Implement running scheduled tasks
This commit is contained in:
parent
2a1e6b3292
commit
435d9b5571
|
@ -153,7 +153,11 @@ class NotificationRuleData(object):
|
|||
window_unit = window[-1]
|
||||
if window_unit not in SECONDS_PER_UNIT:
|
||||
raise RuleParseError(
|
||||
f"Window unit must be one of {', '.join(SECONDS_PER_UNIT.keys())}, not '{window_unit}'",
|
||||
(
|
||||
"Window unit must be one of "
|
||||
f"{', '.join(SECONDS_PER_UNIT.keys())},"
|
||||
f" not '{window_unit}'"
|
||||
),
|
||||
"window",
|
||||
)
|
||||
window_seconds = window_number * SECONDS_PER_UNIT[window_unit]
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import asyncio
|
||||
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
# from core.db.storage import db
|
||||
# from core.models import NotificationRule
|
||||
from core.util import logs
|
||||
from core.models import NotificationRule
|
||||
from core.db.storage import db
|
||||
import aioschedule as schedule
|
||||
import asyncio
|
||||
from time import sleep
|
||||
|
||||
log = logs.get_logger("scheduling")
|
||||
|
||||
|
@ -21,19 +21,32 @@ log = logs.get_logger("scheduling")
|
|||
|
||||
INTERVALS = [60, 900, 1800, 3600, 14400, 86400]
|
||||
|
||||
def run_schedule(interval_seconds):
|
||||
|
||||
async def job(interval_seconds):
|
||||
"""
|
||||
Run all schedules matching the given interval.
|
||||
:param interval_seconds: The interval to run.
|
||||
"""
|
||||
print("Running schedule", interval_seconds)
|
||||
matching_rules = NotificationRule.objects.filter(
|
||||
enabled=True, interval=interval_seconds
|
||||
)
|
||||
# matching_rules = NotificationRule.objects.filter(
|
||||
# enabled=True, interval=interval_seconds
|
||||
# )
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
"""
|
||||
Start the scheduling process.
|
||||
"""
|
||||
scheduler = AsyncIOScheduler()
|
||||
for interval in INTERVALS:
|
||||
schedule.every(interval).seconds.do(run_schedule, interval_seconds=interval)
|
||||
|
||||
log.debug(f"Scheduling {interval} second job")
|
||||
scheduler.add_job(job, "interval", seconds=interval, args=[interval])
|
||||
scheduler.start()
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
while True:
|
||||
loop.run_until_complete(schedule.run_pending())
|
||||
sleep(10)
|
||||
try:
|
||||
loop.run_forever()
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
log.info("Process terminating")
|
||||
finally:
|
||||
loop.close()
|
||||
|
|
|
@ -19,4 +19,4 @@ django-debug-toolbar
|
|||
django-debug-toolbar-template-profiler
|
||||
orjson
|
||||
msgpack
|
||||
aioschedule
|
||||
apscheduler
|
||||
|
|
Loading…
Reference in New Issue