# myproject/scheduler.py
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
import logging
from subscription_app.tasks import generate_billing

logger = logging.getLogger(__name__)

# 初始化调度器
scheduler = BackgroundScheduler()

def start():
    if not scheduler.running:
        # 添加任务（例如每隔 5 分钟运行一次任务）
        scheduler.add_job(
            generate_billing,
            trigger=CronTrigger(second='*/5'),  # 每 5 分钟执行一次
            id='generate_billing task',  # 给任务一个唯一 ID
            replace_existing=True,
        )
        logger.info("Scheduler started")
        scheduler.start()

def stop():
    logger.info("Scheduler stopped")
    scheduler.shutdown()


