pluto/core/management/commands/scheduling.py

47 lines
1.4 KiB
Python
Raw Normal View History

2023-03-05 20:09:31 +00:00
import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from django.core.management.base import BaseCommand
2023-03-09 16:44:16 +00:00
from core.clients.aggregators.nordigen import NordigenClient
from core.models import Aggregator
2023-03-05 20:09:31 +00:00
from core.util import logs
log = logs.get_logger("scheduling")
INTERVAL = 5
async def job():
print("Running schedule.")
2023-03-09 16:44:16 +00:00
aggregators = Aggregator.objects.filter(enabled=True, fetch_accounts=True)
for aggregator in aggregators:
if aggregator.service == "nordigen":
instance = await NordigenClient(aggregator)
print("RUNNING GET ALL ACCOUNT INFO")
await instance.get_all_account_info(store=True)
print("FINISHED RUNNING")
else:
raise NotImplementedError(f"No such client library: {aggregator.service}")
aggregator.fetch_accounts = False
aggregator.save()
2023-03-05 20:09:31 +00:00
class Command(BaseCommand):
def handle(self, *args, **options):
"""
Start the scheduling process.
"""
scheduler = AsyncIOScheduler()
log.debug(f"Scheduling checking process job every {INTERVAL} seconds")
scheduler.add_job(job, "interval", seconds=INTERVAL)
scheduler.start()
loop = asyncio.get_event_loop()
try:
loop.run_forever()
except (KeyboardInterrupt, SystemExit):
log.info("Process terminating")
finally:
loop.close()