"""Standalone scheduler process entry point.

Run with:
    python -m src.services.scheduler_runner

This module intentionally does NOT import the FastAPI app — it only starts the
SchedulerService so it can run in its own container without duplicating work in
the main web-worker containers (which set SCHEDULER_ENABLED=false).
"""
import asyncio
import signal
from loguru import logger

from src.database.client import DatabaseClient
from src.services.scheduler_service import init_scheduler


async def main():
    db = DatabaseClient()
    scheduler = init_scheduler(db, check_interval=60)

    stop_event = asyncio.Event()

    def handle_signal():
        logger.info("Scheduler received shutdown signal — stopping…")
        stop_event.set()

    loop = asyncio.get_running_loop()
    for sig in (signal.SIGINT, signal.SIGTERM):
        loop.add_signal_handler(sig, handle_signal)

    await scheduler.start()
    logger.info("Scheduler started (dedicated process)")

    await stop_event.wait()

    await scheduler.stop()
    logger.info("Scheduler stopped")


if __name__ == "__main__":
    asyncio.run(main())
