ForcePilot/backend/package/yuxi/channels/adapters/imessage/transport.py

32 lines
901 B
Python
Raw Normal View History

from __future__ import annotations
import asyncio
from collections.abc import Awaitable, Callable
from yuxi.utils.logging_config import logger
DEFAULT_TRANSPORT_TIMEOUT_S = 30.0
DEFAULT_TRANSPORT_POLL_INTERVAL_S = 0.5
async def wait_for_transport_ready(
probe_fn: Callable[[], Awaitable[bool]],
timeout_s: float = DEFAULT_TRANSPORT_TIMEOUT_S,
poll_interval_s: float = DEFAULT_TRANSPORT_POLL_INTERVAL_S,
) -> bool:
deadline = asyncio.get_event_loop().time() + timeout_s
while asyncio.get_event_loop().time() < deadline:
try:
ready = await probe_fn()
if ready:
logger.info("[iMessage/Transport] Transport ready")
return True
except Exception:
pass
await asyncio.sleep(poll_interval_s)
logger.error(f"[iMessage/Transport] Transport not ready after {timeout_s}s")
return False