99 lines
2.7 KiB
Python
99 lines
2.7 KiB
Python
"""
|
|
APScheduler configuration for background jobs.
|
|
"""
|
|
import logging
|
|
from zoneinfo import ZoneInfo
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
from apscheduler.triggers.cron import CronTrigger
|
|
|
|
KST = ZoneInfo("Asia/Seoul")
|
|
|
|
from jobs.snapshot_job import create_daily_snapshots
|
|
from jobs.collection_job import run_daily_collection, run_financial_collection
|
|
from jobs.kjb_signal_job import run_kjb_signals
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create scheduler instance
|
|
scheduler = BackgroundScheduler()
|
|
|
|
|
|
def configure_jobs():
|
|
"""Configure scheduled jobs."""
|
|
# Daily data collection at 18:00 (after market close, before snapshot)
|
|
scheduler.add_job(
|
|
run_daily_collection,
|
|
trigger=CronTrigger(
|
|
hour=18,
|
|
minute=0,
|
|
day_of_week='mon-fri',
|
|
timezone=KST,
|
|
),
|
|
id='daily_collection',
|
|
name='Collect daily market data',
|
|
replace_existing=True,
|
|
)
|
|
logger.info("Configured daily_collection job at 18:00 KST")
|
|
|
|
# Weekly financial statement collection at 19:00 Monday
|
|
# (after daily collection, financial data updates quarterly)
|
|
scheduler.add_job(
|
|
run_financial_collection,
|
|
trigger=CronTrigger(
|
|
hour=19,
|
|
minute=0,
|
|
day_of_week='mon',
|
|
timezone=KST,
|
|
),
|
|
id='weekly_financial_collection',
|
|
name='Collect financial statements from FnGuide',
|
|
replace_existing=True,
|
|
)
|
|
logger.info("Configured weekly_financial_collection job at 19:00 KST Monday")
|
|
|
|
# Daily snapshot at 18:30 (after data collection completes)
|
|
scheduler.add_job(
|
|
create_daily_snapshots,
|
|
trigger=CronTrigger(
|
|
hour=18,
|
|
minute=30,
|
|
day_of_week='mon-fri',
|
|
timezone=KST,
|
|
),
|
|
id='daily_snapshots',
|
|
name='Create daily portfolio snapshots',
|
|
replace_existing=True,
|
|
)
|
|
logger.info("Configured daily_snapshots job at 18:30 KST")
|
|
|
|
# KJB signal generation at 18:15 (after daily collection, before snapshot)
|
|
scheduler.add_job(
|
|
run_kjb_signals,
|
|
trigger=CronTrigger(
|
|
hour=18,
|
|
minute=15,
|
|
day_of_week='mon-fri',
|
|
timezone=KST,
|
|
),
|
|
id='kjb_daily_signals',
|
|
name='Generate KJB trading signals',
|
|
replace_existing=True,
|
|
)
|
|
logger.info("Configured kjb_daily_signals job at 18:15 KST")
|
|
|
|
|
|
def start_scheduler():
|
|
"""Start the scheduler."""
|
|
if not scheduler.running:
|
|
configure_jobs()
|
|
scheduler.start()
|
|
logger.info("Scheduler started")
|
|
|
|
|
|
def stop_scheduler():
|
|
"""Stop the scheduler."""
|
|
if scheduler.running:
|
|
scheduler.shutdown()
|
|
logger.info("Scheduler stopped")
|