1
0
Fork 0

chore: minor cleanup, healthcheck

This commit is contained in:
Arthur K. 2026-03-02 20:44:34 +03:00
parent 2611d1bb6d
commit 42282ce8cb
Signed by: wzray
GPG key ID: B97F30FDC4636357
7 changed files with 42 additions and 8 deletions

18
src/healthcheck.py Normal file
View file

@ -0,0 +1,18 @@
import os
import urllib.request
def main() -> int:
port = os.environ.get("PORT", "80")
url = f"http://127.0.0.1:{port}/health"
try:
with urllib.request.urlopen(url, timeout=2) as resp:
body = resp.read().decode("utf-8").strip()
return 0 if resp.status == 200 and body == "ok" else 1
except Exception:
return 1
if __name__ == "__main__":
exit(main())

View file

@ -71,7 +71,7 @@ class Provider(ABC):
"""Usage percent when provider may switch active account/token."""
return None
def should_prepare_standby(self, usage_percent: int) -> bool:
def should_prepare_standby(self) -> bool:
"""Whether standby preparation should be triggered for current usage."""
return False

View file

@ -122,14 +122,14 @@ class ChatGPTProvider(Provider):
return True
return await self._create_next_account_under_lock()
def should_prepare_standby(self, usage_percent: int) -> bool:
return usage_percent >= self.prepare_threshold and bool(load_next_tokens())
def should_prepare_standby(self) -> bool:
return bool(load_next_tokens())
async def ensure_standby_account(
self,
usage_percent: int,
) -> None:
if self.should_prepare_standby(usage_percent):
if usage_percent >= self.prepare_threshold:
await self.ensure_next_account()
async def maybe_switch_active_account(self, usage_percent: int) -> bool:

View file

@ -47,7 +47,7 @@ def should_trigger_standby_prepare(provider_name: str, usage_percent: int) -> bo
provider = PROVIDERS.get(provider_name)
if not provider:
return False
return provider.should_prepare_standby(usage_percent)
return provider.should_prepare_standby()
async def ensure_provider_token_ready(provider_name: str):
@ -86,7 +86,7 @@ async def ensure_standby_task(provider_name: str, usage_percent: int, reason: st
if not provider:
return
if not provider.should_prepare_standby(usage_percent):
if not provider.should_prepare_standby():
return
try:
@ -188,6 +188,11 @@ async def token_handler(request: web.Request) -> web.Response:
)
async def health_handler(request: web.Request) -> web.Response:
del request
return web.Response(text="ok")
async def on_startup(app: web.Application):
del app
for provider_name in PROVIDERS:
@ -208,6 +213,7 @@ def create_app() -> web.Application:
app = web.Application(middlewares=[request_log_middleware])
app.on_startup.append(on_startup)
app.on_cleanup.append(on_cleanup)
app.router.add_get("/health", health_handler)
app.router.add_get("/{provider}/token", token_handler)
app.router.add_get("/token", token_handler)
return app