Begin work on scheduling management command

This commit is contained in:
2023-02-17 07:20:19 +00:00
parent ffdbcecc8d
commit da67177a18
9 changed files with 126 additions and 1 deletions

View File

View File

View File

@@ -0,0 +1,47 @@
import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from asgiref.sync import sync_to_async
from django.core.management.base import BaseCommand
from core.models import Strategy
from core.util import logs
from core.trading import active_management
log = logs.get_logger("scheduling")
INTERVAL = 5
async def job():
"""
Run all schedules matching the given interval.
:param interval_seconds: The interval to run.
"""
strategies = await sync_to_async(list)(
Strategy.objects.filter(enabled=True, active_management_enabled=True)
)
log.debug(f"Found {len(strategies)} strategies")
for strategy in strategies:
log.debug(f"Running strategy {strategy.name}")
ams = active_management.ActiveManagement(strategy)
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()