Auto-install Playwright Chromium at server startup for cloud deployments
This commit is contained in:
+26
-1
@@ -6,12 +6,37 @@ import os
|
|||||||
import httpx
|
import httpx
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from pydantic import BaseModel, HttpUrl, Field
|
from pydantic import BaseModel, HttpUrl, Field
|
||||||
from typing import Optional, Dict, List, Literal, Any, Union
|
from typing import Optional, Dict, List, Literal, Any, Union
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from fastmcp.server.middleware import Middleware, MiddlewareContext
|
from fastmcp.server.middleware import Middleware, MiddlewareContext
|
||||||
|
|
||||||
|
# --- Install Playwright Chromium at startup (for cloud deployments) ---
|
||||||
|
def _install_playwright_chromium():
|
||||||
|
"""Install Playwright Chromium browser if not already installed."""
|
||||||
|
try:
|
||||||
|
# Check if running in a cloud environment (no display)
|
||||||
|
result = subprocess.run(
|
||||||
|
[sys.executable, "-m", "playwright", "install", "chromium"],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=300 # 5 minute timeout
|
||||||
|
)
|
||||||
|
if result.returncode == 0:
|
||||||
|
print("Playwright Chromium installed successfully")
|
||||||
|
else:
|
||||||
|
print(f"Playwright Chromium installation warning: {result.stderr}")
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
print("Warning: Playwright Chromium installation timed out")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Warning: Could not install Playwright Chromium: {e}")
|
||||||
|
|
||||||
|
# Run installation at module load time
|
||||||
|
_install_playwright_chromium()
|
||||||
|
|
||||||
# Optional tiktoken import for token counting
|
# Optional tiktoken import for token counting
|
||||||
try:
|
try:
|
||||||
import tiktoken
|
import tiktoken
|
||||||
|
|||||||
Reference in New Issue
Block a user