2023-01-14 17:24:54 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
2023-01-14 16:36:22 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2023-01-14 17:24:54 +00:00
|
|
|
# from core.db.storage import db
|
|
|
|
# from core.models import NotificationRule
|
2023-01-14 16:36:22 +00:00
|
|
|
from core.util import logs
|
|
|
|
|
|
|
|
log = logs.get_logger("scheduling")
|
|
|
|
|
|
|
|
# INTERVAL_CHOICES = (
|
|
|
|
# (0, "On demand"),
|
|
|
|
# (60, "Every minute"),
|
|
|
|
# (900, "Every 15 minutes"),
|
|
|
|
# (1800, "Every 30 minutes"),
|
|
|
|
# (3600, "Every hour"),
|
|
|
|
# (14400, "Every 4 hours"),
|
|
|
|
# (86400, "Every day"),
|
|
|
|
# )
|
|
|
|
|
|
|
|
INTERVALS = [60, 900, 1800, 3600, 14400, 86400]
|
|
|
|
|
2023-01-14 17:24:54 +00:00
|
|
|
|
|
|
|
async def job(interval_seconds):
|
|
|
|
"""
|
|
|
|
Run all schedules matching the given interval.
|
|
|
|
:param interval_seconds: The interval to run.
|
|
|
|
"""
|
2023-01-14 16:36:22 +00:00
|
|
|
print("Running schedule", interval_seconds)
|
2023-01-14 17:24:54 +00:00
|
|
|
# matching_rules = NotificationRule.objects.filter(
|
|
|
|
# enabled=True, interval=interval_seconds
|
|
|
|
# )
|
|
|
|
|
2023-01-14 16:36:22 +00:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
def handle(self, *args, **options):
|
2023-01-14 17:24:54 +00:00
|
|
|
"""
|
|
|
|
Start the scheduling process.
|
|
|
|
"""
|
|
|
|
scheduler = AsyncIOScheduler()
|
2023-01-14 16:36:22 +00:00
|
|
|
for interval in INTERVALS:
|
2023-01-14 17:24:54 +00:00
|
|
|
log.debug(f"Scheduling {interval} second job")
|
|
|
|
scheduler.add_job(job, "interval", seconds=interval, args=[interval])
|
|
|
|
scheduler.start()
|
2023-01-14 16:36:22 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
2023-01-14 17:24:54 +00:00
|
|
|
try:
|
|
|
|
loop.run_forever()
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
log.info("Process terminating")
|
|
|
|
finally:
|
|
|
|
loop.close()
|