|
|
|
@ -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()
|
|
|
|
|