neptune/core/management/commands/scheduling.py

39 lines
1.0 KiB
Python

from django.core.management.base import BaseCommand
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")
# 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]
def run_schedule(interval_seconds):
print("Running schedule", interval_seconds)
matching_rules = NotificationRule.objects.filter(
enabled=True, interval=interval_seconds
)
class Command(BaseCommand):
def handle(self, *args, **options):
for interval in INTERVALS:
schedule.every(interval).seconds.do(run_schedule, interval_seconds=interval)
loop = asyncio.get_event_loop()
while True:
loop.run_until_complete(schedule.run_pending())
sleep(10)