This commit is contained in:
@@ -565,4 +565,10 @@ async def detect_pagination_service(decoded_url):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Perform the operation
|
# Perform the operation
|
||||||
return await safe_browser_operation(decoded_url, pagination_operation)
|
result = await safe_browser_operation(decoded_url, pagination_operation)
|
||||||
|
|
||||||
|
# Check if the result is an error from safe_browser_operation
|
||||||
|
if isinstance(result, dict) and result.get("status") == "error":
|
||||||
|
return result
|
||||||
|
|
||||||
|
return result
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from pyppeteer import launch
|
from pyppeteer import launch
|
||||||
from app.config import CUSTOM_USER_AGENT
|
from app.config import CUSTOM_USER_AGENT
|
||||||
|
import asyncio
|
||||||
|
|
||||||
async def wait_for_network_idle(page):
|
async def wait_for_network_idle(page):
|
||||||
"""Wait until no network requests are in flight"""
|
"""Wait until no network requests are in flight"""
|
||||||
@@ -8,6 +9,7 @@ async def wait_for_network_idle(page):
|
|||||||
async def safe_browser_operation(url, operation_func):
|
async def safe_browser_operation(url, operation_func):
|
||||||
"""Safely perform browser operations with proper cleanup"""
|
"""Safely perform browser operations with proper cleanup"""
|
||||||
browser = None
|
browser = None
|
||||||
|
page = None
|
||||||
try:
|
try:
|
||||||
browser = await launch(
|
browser = await launch(
|
||||||
headless=True,
|
headless=True,
|
||||||
@@ -28,12 +30,24 @@ async def safe_browser_operation(url, operation_func):
|
|||||||
# Call the operation function that uses the page
|
# Call the operation function that uses the page
|
||||||
result = await operation_func(page)
|
result = await operation_func(page)
|
||||||
|
|
||||||
# Explicitly close the page
|
|
||||||
await page.close()
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error during browser operation: {e}")
|
||||||
|
# Return error result instead of re-raising to allow graceful handling
|
||||||
|
return {
|
||||||
|
"status": "error",
|
||||||
|
"error": str(e),
|
||||||
|
"url": url
|
||||||
|
}
|
||||||
finally:
|
finally:
|
||||||
|
# Ensure page is closed properly
|
||||||
|
if page:
|
||||||
|
try:
|
||||||
|
await page.close()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error closing page: {e}")
|
||||||
|
|
||||||
# Ensure browser is closed properly
|
# Ensure browser is closed properly
|
||||||
if browser:
|
if browser:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user