pluto/core/management/commands/scheduling.py

67 lines
2.2 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
2023-03-11 17:43:10 +00:00
from core.clients.platforms.agora import AgoraClient
from core.models import INTERVAL_CHOICES, Aggregator, Platform
2023-03-05 20:09:31 +00:00
from core.util import logs
log = logs.get_logger("scheduling")
2023-03-11 17:43:10 +00:00
INTERVAL_AGGREGATOR = 5
2023-03-05 20:09:31 +00:00
2023-03-11 17:43:10 +00:00
INTERVALS_PLATFORM = [x[0] for x in INTERVAL_CHOICES]
2023-03-05 20:09:31 +00:00
2023-03-11 17:43:10 +00:00
async def aggregator_job():
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)
await instance.get_all_account_info(store=True)
else:
raise NotImplementedError(f"No such client library: {aggregator.service}")
aggregator.fetch_accounts = False
aggregator.save()
2023-03-05 20:09:31 +00:00
2023-03-11 17:43:10 +00:00
async def platform_job(interval):
if interval == 0:
return
platforms = Platform.objects.filter(enabled=True, cheat_interval_seconds=interval)
for platform in platforms:
if platform.service == "agora":
if platform.cheat is True:
instance = await AgoraClient(platform)
await instance.cheat()
else:
raise NotImplementedError(f"No such client library: {platform.service}")
2023-03-05 20:09:31 +00:00
class Command(BaseCommand):
def handle(self, *args, **options):
"""
Start the scheduling process.
"""
scheduler = AsyncIOScheduler()
2023-03-11 17:43:10 +00:00
log.debug(f"Scheduling {INTERVAL_AGGREGATOR} second aggregator job")
scheduler.add_job(aggregator_job, "interval", seconds=INTERVAL_AGGREGATOR)
for interval in INTERVALS_PLATFORM:
if interval == 0:
continue
log.debug(f"Scheduling {interval} second platform job")
scheduler.add_job(
platform_job, "interval", seconds=interval, args=[interval]
)
2023-03-05 20:09:31 +00:00
scheduler.start()
loop = asyncio.get_event_loop()
try:
loop.run_forever()
except (KeyboardInterrupt, SystemExit):
log.info("Process terminating")
finally:
loop.close()