import asyncio

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from django.core.management.base import BaseCommand

from core.util import logs

log = logs.get_logger("scheduling")

INTERVALS = [5, 60, 900, 1800, 3600, 14400, 86400]


async def job(interval_seconds):
    """
    Run all schedules matching the given interval.
    :param interval_seconds: The interval to run.
    """


class Command(BaseCommand):
    def handle(self, *args, **options):
        """
        Start the scheduling process.
        """
        scheduler = AsyncIOScheduler()
        for interval in INTERVALS:
            log.debug(f"Scheduling {interval} second job")
            scheduler.add_job(job, "interval", seconds=interval, args=[interval])

        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        scheduler._eventloop = loop
        scheduler.start()
        try:
            loop.run_forever()
        except (KeyboardInterrupt, SystemExit):
            log.info("Process terminating")
        finally:
            loop.close()
