Background watchdog to monitor quota exceeded events.
()
| 23 | |
| 24 | |
| 25 | async def quota_watchdog(): |
| 26 | """Background watchdog to monitor quota exceeded events.""" |
| 27 | # Use state's logger if available |
| 28 | logger = getattr(state, "logger", logging.getLogger("AIStudioProxyServer")) |
| 29 | logger.info("👀 Quota Watchdog Started") |
| 30 | while True: |
| 31 | try: |
| 32 | await GlobalState.QUOTA_EXCEEDED_EVENT.wait() |
| 33 | logger.critical( |
| 34 | "🚨 Watchdog detected Quota Exceeded! Initiating Rotation..." |
| 35 | ) |
| 36 | |
| 37 | if not GlobalState.AUTH_ROTATION_LOCK.is_set(): |
| 38 | logger.info("Watchdog: Rotation already in progress. Waiting...") |
| 39 | await asyncio.sleep(1) |
| 40 | continue |
| 41 | |
| 42 | GlobalState.start_recovery() |
| 43 | try: |
| 44 | current_model_id = state.current_ai_studio_model_id |
| 45 | success = await perform_auth_rotation( |
| 46 | target_model_id=current_model_id or "" |
| 47 | ) |
| 48 | if success: |
| 49 | logger.info("Watchdog: Rotation successful.") |
| 50 | else: |
| 51 | logger.error("Watchdog: Rotation failed.") |
| 52 | finally: |
| 53 | GlobalState.finish_recovery() |
| 54 | |
| 55 | if GlobalState.IS_QUOTA_EXCEEDED: |
| 56 | logger.warning("Watchdog: Quota flag still set. Forcing reset.") |
| 57 | GlobalState.reset_quota_status() |
| 58 | |
| 59 | except asyncio.CancelledError: |
| 60 | logger.info("Watchdog: Task cancelled.") |
| 61 | break |
| 62 | except Exception as e: |
| 63 | logger.error(f"Watchdog Error: {e}", exc_info=True) |
| 64 | await asyncio.sleep(5) |
| 65 | |
| 66 | |
| 67 | # Register quota_watchdog in state for easier access and to avoid circular import issues |
nothing calls this directly
no test coverage detected