36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
from typing import TYPE_CHECKING
|
||
|
|
|
||
|
|
from yuxi.utils.logging_config import logger
|
||
|
|
|
||
|
|
if TYPE_CHECKING:
|
||
|
|
from yuxi.channels.manager import ChannelManager
|
||
|
|
from yuxi.channels.services.runtime_state import RuntimeState
|
||
|
|
|
||
|
|
|
||
|
|
class MaintenanceRunner:
|
||
|
|
def __init__(self, channel_manager: ChannelManager, state: RuntimeState):
|
||
|
|
self._manager = channel_manager
|
||
|
|
self._state = state
|
||
|
|
|
||
|
|
async def run(self, interval: float = 600) -> None:
|
||
|
|
logger.info("MaintenanceRunner started")
|
||
|
|
while True:
|
||
|
|
await asyncio.sleep(interval)
|
||
|
|
try:
|
||
|
|
await self._run_maintenance()
|
||
|
|
except asyncio.CancelledError:
|
||
|
|
break
|
||
|
|
except Exception:
|
||
|
|
logger.exception("MaintenanceRunner error")
|
||
|
|
|
||
|
|
async def _run_maintenance(self) -> None:
|
||
|
|
for channel_id, adapter in self._manager._adapters.items():
|
||
|
|
try:
|
||
|
|
await adapter._refresh_token_if_needed()
|
||
|
|
except Exception:
|
||
|
|
logger.debug(f"Token refresh skip for {channel_id}")
|
||
|
|
logger.debug("Maintenance cycle complete")
|