cleanup
Build and Push Docker Images / build-and-push (push) Successful in 2m32s

This commit is contained in:
2025-07-12 16:26:22 +02:00
parent 5f5941703c
commit 2f724d0c1f
9 changed files with 306 additions and 288 deletions
@@ -1,6 +1,11 @@
from playwright.async_api import async_playwright
from app.config import CUSTOM_USER_AGENT
from app.config import CUSTOM_USER_AGENT, BROWSER_INSTANCE_TIMEOUT_MINUTES
import asyncio
import time
# Global tracking for browser instances created by this module
page_creation_times = {}
active_pages = set()
async def wait_for_network_idle(page):
"""Wait until no network requests are in flight"""
@@ -32,6 +37,10 @@ async def safe_browser_operation(url, operation_func):
page = await context.new_page()
page.set_default_timeout(30000)
# Track page creation time for force cleanup
page_creation_times[page] = time.time()
active_pages.add(page)
# Call the operation function that uses the page
result = await operation_func(page)
@@ -46,9 +55,14 @@ async def safe_browser_operation(url, operation_func):
"url": url
}
finally:
# Ensure page is closed properly
# Cleanup page tracking
if page:
try:
# Remove from tracking
if page in active_pages:
active_pages.remove(page)
if page in page_creation_times:
del page_creation_times[page]
await page.close()
except Exception as e:
print(f"Error closing page: {e}")
@@ -72,4 +86,33 @@ async def safe_browser_operation(url, operation_func):
try:
await playwright.stop()
except Exception as e:
print(f"Error closing playwright: {e}")
print(f"Error closing playwright: {e}")
async def force_cleanup_old_pages():
"""Force cleanup old page instances created by this module"""
print(f"Checking for old page instances in browser_utils (timeout: {BROWSER_INSTANCE_TIMEOUT_MINUTES} minutes)...")
current_time = time.time()
timeout_seconds = BROWSER_INSTANCE_TIMEOUT_MINUTES * 60
cleaned_pages = 0
# Clean up old pages
pages_to_cleanup = []
for page in list(active_pages):
creation_time = page_creation_times.get(page, 0)
if current_time - creation_time > timeout_seconds:
pages_to_cleanup.append(page)
print(f"Marking page for cleanup (age: {(current_time - creation_time)/60:.1f} minutes)")
for page in pages_to_cleanup:
try:
if page in active_pages:
active_pages.remove(page)
if page in page_creation_times:
del page_creation_times[page]
await page.close()
cleaned_pages += 1
except Exception as e:
print(f"Error cleaning up old page: {e}")
print(f"Force cleanup completed: {cleaned_pages} pages cleaned")