refactor browser instance checks to improve stability and resource management
Build and Push Docker Images / build-and-push (push) Successful in 18s
Build and Push Docker Images / build-and-push (push) Successful in 18s
This commit is contained in:
@@ -25,7 +25,7 @@ browser = None
|
||||
async def get_browser():
|
||||
"""Get or create a browser instance"""
|
||||
global browser
|
||||
if browser is None or not browser.isConnected():
|
||||
if browser is None or not hasattr(browser, 'process') or browser.process is None:
|
||||
logger.info("Launching new browser instance")
|
||||
browser = await launch(
|
||||
headless=True,
|
||||
@@ -72,7 +72,7 @@ async def cleanup_resources(page=None, close_browser=False):
|
||||
if page:
|
||||
await page.close()
|
||||
|
||||
if close_browser and browser and browser.isConnected():
|
||||
if close_browser and browser and hasattr(browser, 'process') and browser.process is not None:
|
||||
logger.info("Closing browser instance")
|
||||
await browser.close()
|
||||
browser = None
|
||||
@@ -102,7 +102,7 @@ async def memory_status(x_api_key: Optional[str] = Header(None)):
|
||||
|
||||
return {
|
||||
"memory_usage_mb": memory_info.rss / 1024 / 1024,
|
||||
"browser_active": browser is not None and browser.isConnected() if browser else False
|
||||
"browser_active": browser is not None and hasattr(browser, 'process') and browser.process is not None
|
||||
}
|
||||
|
||||
@app.post("/cleanup")
|
||||
|
||||
Reference in New Issue
Block a user