32 lines
977 B
Python
32 lines
977 B
Python
import logging
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
from apscheduler.triggers.cron import CronTrigger
|
|
|
|
logger = logging.getLogger(__name__)
|
|
scheduler = AsyncIOScheduler(timezone="Asia/Shanghai")
|
|
|
|
|
|
async def daily_pipeline_job():
|
|
from .database import AsyncSessionLocal
|
|
from .ai.processor import run_daily_pipeline
|
|
async with AsyncSessionLocal() as db:
|
|
try:
|
|
await run_daily_pipeline(db)
|
|
except Exception as e:
|
|
logger.error(f"Daily pipeline failed: {e}", exc_info=True)
|
|
|
|
|
|
def start_scheduler():
|
|
scheduler.add_job(daily_pipeline_job, CronTrigger(hour=6, minute=0), id="daily_pipeline", replace_existing=True)
|
|
scheduler.start()
|
|
logger.info("Scheduler started — daily pipeline runs at 06:00 Asia/Shanghai")
|
|
|
|
|
|
def shutdown_scheduler():
|
|
scheduler.shutdown(wait=False)
|
|
|
|
|
|
async def trigger_now():
|
|
"""Manually trigger the pipeline (called from admin API)."""
|
|
await daily_pipeline_job()
|