Spaces:
Running
Running
nacho commited on
Commit ·
2d9d4ce
1
Parent(s): 3ee386a
fix: browser health check + single error count per failure
Browse files- account_manager.py +18 -6
- main.py +4 -2
account_manager.py
CHANGED
|
@@ -103,19 +103,31 @@ class AccountManager:
|
|
| 103 |
account.last_error = ""
|
| 104 |
account.is_muted = account.browser.is_muted()
|
| 105 |
account.muted_until = account.browser.muted_until()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return account.browser
|
| 107 |
except Exception as e:
|
| 108 |
logger.error("Error creating browser for %s: %s", account.email, e)
|
| 109 |
-
await self.mark_error(account, str(e))
|
| 110 |
await self.close_browser(account)
|
| 111 |
raise
|
| 112 |
|
| 113 |
async def get_or_create_browser_with_retry(self, account: Account, headless: bool = True) -> DeepSeekBrowser:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
async def close_browser(self, account: Account):
|
| 121 |
if account.browser:
|
|
|
|
| 103 |
account.last_error = ""
|
| 104 |
account.is_muted = account.browser.is_muted()
|
| 105 |
account.muted_until = account.browser.muted_until()
|
| 106 |
+
else:
|
| 107 |
+
try:
|
| 108 |
+
await account.browser.page.evaluate("1+1", timeout=3000)
|
| 109 |
+
except Exception:
|
| 110 |
+
logger.warning("Browser for %s seems dead, recreating...", account.email)
|
| 111 |
+
await self.close_browser(account)
|
| 112 |
+
raise RuntimeError("Browser dead, need recreate")
|
| 113 |
return account.browser
|
| 114 |
except Exception as e:
|
| 115 |
logger.error("Error creating browser for %s: %s", account.email, e)
|
|
|
|
| 116 |
await self.close_browser(account)
|
| 117 |
raise
|
| 118 |
|
| 119 |
async def get_or_create_browser_with_retry(self, account: Account, headless: bool = True) -> DeepSeekBrowser:
|
| 120 |
+
last_error = ""
|
| 121 |
+
for attempt in range(2):
|
| 122 |
+
try:
|
| 123 |
+
return await self.get_or_create_browser(account, headless)
|
| 124 |
+
except Exception as e:
|
| 125 |
+
last_error = str(e)
|
| 126 |
+
if attempt == 0:
|
| 127 |
+
logger.info("Retrying browser for %s...", account.email)
|
| 128 |
+
await asyncio.sleep(2)
|
| 129 |
+
await self.mark_error(account, last_error)
|
| 130 |
+
raise RuntimeError(f"Failed to create browser for {account.email}: {last_error}")
|
| 131 |
|
| 132 |
async def close_browser(self, account: Account):
|
| 133 |
if account.browser:
|
main.py
CHANGED
|
@@ -308,7 +308,8 @@ async def chat_completions(
|
|
| 308 |
}
|
| 309 |
|
| 310 |
except Exception as e:
|
| 311 |
-
|
|
|
|
| 312 |
logger.error("Chat completion error for model=%s: %s", request.model, e)
|
| 313 |
raise HTTPException(status_code=503, detail=str(e))
|
| 314 |
|
|
@@ -573,7 +574,8 @@ async def admin_chat(request: Request, admin_key: str = Header(...)):
|
|
| 573 |
"usage": {"prompt_tokens": prompt_tokens, "completion_tokens": completion_tokens, "total_tokens": prompt_tokens + completion_tokens},
|
| 574 |
}
|
| 575 |
except Exception as e:
|
| 576 |
-
|
|
|
|
| 577 |
logger.error("Admin chat error: %s", e)
|
| 578 |
raise HTTPException(status_code=503, detail=str(e))
|
| 579 |
|
|
|
|
| 308 |
}
|
| 309 |
|
| 310 |
except Exception as e:
|
| 311 |
+
if account.browser is not None:
|
| 312 |
+
await manager.mark_error(account, str(e))
|
| 313 |
logger.error("Chat completion error for model=%s: %s", request.model, e)
|
| 314 |
raise HTTPException(status_code=503, detail=str(e))
|
| 315 |
|
|
|
|
| 574 |
"usage": {"prompt_tokens": prompt_tokens, "completion_tokens": completion_tokens, "total_tokens": prompt_tokens + completion_tokens},
|
| 575 |
}
|
| 576 |
except Exception as e:
|
| 577 |
+
if account.browser is not None:
|
| 578 |
+
await manager.mark_error(account, str(e))
|
| 579 |
logger.error("Admin chat error: %s", e)
|
| 580 |
raise HTTPException(status_code=503, detail=str(e))
|
| 581 |
|