from __future__ import annotations

import asyncio
import logging

from backend.features.operations.service import OperationsService

logger = logging.getLogger(__name__)


async def run_operations_scheduler(
    service: OperationsService, stop: asyncio.Event
) -> None:
    while not stop.is_set():
        try:
            await asyncio.to_thread(service.tick)
        except Exception:
            logger.exception(
                "Operations scheduler tick failed",
                extra={"event": "operations.scheduler.failed"},
            )
        try:
            await asyncio.wait_for(stop.wait(), timeout=5)
        except TimeoutError:
            pass
