CronTrigger had no explicit timezone, defaulting to system timezone (UTC in Docker containers), causing jobs to run at KST 03:00/03:30 instead of the intended 18:00/18:30. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
67 lines
1.7 KiB
Python
67 lines
1.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
|
|
|
|
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")
|
|
|
|
# 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")
|
|
|
|
|
|
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")
|