flash
This commit is contained in:
1 parent
a16285c1d2
commit
61e8f7b490
2 files changed
+56
-29
No files matched your search
@@ -83,39 +83,48 @@ async def run_browser_task(task_id: str, task: str, max_steps: int, model: str):
|
||||
import subprocess
|
||||
import glob
|
||||
import urllib.request
|
||||
import sys
|
||||
|
||||
# Точный путь из контейнера
|
||||
CHROME_PATH = "/root/.cache/ms-playwright/chromium-1208/chrome-linux64/chrome"
|
||||
print(f"[{task_id}] Starting Chrome with Remote Debugging on port {REMOTE_DEBUGGING_PORT}...")
|
||||
|
||||
# Ищем chrome
|
||||
# Список возможных путей к Chrome
|
||||
chrome_exe = None
|
||||
if os.path.exists(CHROME_PATH):
|
||||
chrome_exe = CHROME_PATH
|
||||
else:
|
||||
# Fallback - ищем в кэше
|
||||
for pattern in [
|
||||
"/root/.cache/ms-playwright/chromium-*/chrome-linux64/chrome",
|
||||
"/ms-playwright/*/chrome-linux/chrome",
|
||||
]:
|
||||
matches = glob.glob(pattern)
|
||||
if matches:
|
||||
chrome_exe = matches[0]
|
||||
break
|
||||
possible_paths = [
|
||||
"/root/.cache/ms-playwright/chromium-1208/chrome-linux64/chrome",
|
||||
"/root/.cache/ms-playwright/chromium-*/chrome-linux64/chrome",
|
||||
"/ms-playwright/*/chrome-linux/chrome",
|
||||
"/usr/bin/google-chrome",
|
||||
"/usr/bin/chromium",
|
||||
"/usr/bin/chromium-browser",
|
||||
]
|
||||
|
||||
for path_pattern in possible_paths:
|
||||
matches = glob.glob(path_pattern)
|
||||
if matches:
|
||||
chrome_exe = matches[0]
|
||||
break
|
||||
|
||||
if not chrome_exe:
|
||||
raise RuntimeError(f"Chrome not found. Path: {CHROME_PATH}")
|
||||
# Попробуем найти через shutil
|
||||
import shutil
|
||||
chrome_exe = shutil.which("google-chrome") or shutil.which("chromium") or shutil.which("chromium-browser")
|
||||
|
||||
if not chrome_exe:
|
||||
raise RuntimeError(f"Chrome not found. Please ensure playwright browsers are installed.")
|
||||
|
||||
print(f"[{task_id}] Using Chrome executable: {chrome_exe}")
|
||||
|
||||
# Создаем уникальную директорию для профиля (чтобы избежать блокировки)
|
||||
import uuid
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
user_data_dir = f"/app/browser-data-{unique_id}"
|
||||
user_data_dir = f"/tmp/browser-data-{unique_id}"
|
||||
os.makedirs(user_data_dir, exist_ok=True)
|
||||
|
||||
# Запускаем Chrome с remote debugging
|
||||
chrome_cmd = [
|
||||
chrome_exe,
|
||||
f"--remote-debugging-port={REMOTE_DEBUGGING_PORT}",
|
||||
f"--remote-debugging-address=0.0.0.0.0",
|
||||
f"--remote-debugging-address=0.0.0.0",
|
||||
f"--user-data-dir={user_data_dir}",
|
||||
"--no-first-run",
|
||||
"--no-default-browser-check",
|
||||
@@ -127,40 +136,54 @@ async def run_browser_task(task_id: str, task: str, max_steps: int, model: str):
|
||||
"about:blank"
|
||||
]
|
||||
|
||||
print(f"[{task_id}] Executing command: {' '.join(chrome_cmd)}")
|
||||
|
||||
# Запускаем процесс
|
||||
process = subprocess.Popen(
|
||||
chrome_cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE
|
||||
stderr=subprocess.PIPE,
|
||||
text=True
|
||||
)
|
||||
|
||||
# Ждем пока Chrome запустится и CDP будет готов
|
||||
cdp_ready = False
|
||||
cdp_url = f"http://127.0.0.1:{REMOTE_DEBUGGING_PORT}"
|
||||
|
||||
for i in range(30): # Ждем до 30 секунд
|
||||
await asyncio.sleep(1)
|
||||
try:
|
||||
response = urllib.request.urlopen(
|
||||
f"http://localhost:{REMOTE_DEBUGGING_PORT}/json/version",
|
||||
timeout=2
|
||||
|
||||
# Проверяем, не упал ли процесс
|
||||
if process.poll() is not None:
|
||||
stdout, stderr = process.communicate()
|
||||
raise RuntimeError(
|
||||
f"Chrome process exited unexpectedly with code {process.returncode}.\n"
|
||||
f"STDOUT: {stdout}\n"
|
||||
f"STDERR: {stderr}"
|
||||
)
|
||||
|
||||
try:
|
||||
response = urllib.request.urlopen(f"{cdp_url}/json/version", timeout=2)
|
||||
if response.status == 200:
|
||||
cdp_ready = True
|
||||
print(f"[{task_id}] CDP is ready at {cdp_url}")
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
if i % 5 == 0:
|
||||
print(f"[{task_id}] Waiting for CDP at {cdp_url}... (attempt {i})")
|
||||
|
||||
if not cdp_ready:
|
||||
# Получаем логи Chrome
|
||||
stdout, stderr = process.communicate(timeout=5)
|
||||
raise RuntimeError(
|
||||
f"Chrome started but CDP not ready after 30s.\n"
|
||||
f"STDOUT: {stdout.decode()}\n"
|
||||
f"STDERR: {stderr.decode()}"
|
||||
f"Chrome started but CDP not ready after 30s at {cdp_url}.\n"
|
||||
f"STDOUT: {stdout}\n"
|
||||
f"STDERR: {stderr}"
|
||||
)
|
||||
|
||||
# Подключаемся через CDP
|
||||
cdp_url = f"http://localhost:{REMOTE_DEBUGGING_PORT}"
|
||||
browser = Browser(cdp_url=cdp_url)
|
||||
from browser_use import BrowserConfig
|
||||
browser = Browser(config=BrowserConfig(cdp_url=cdp_url))
|
||||
else:
|
||||
# Обычный запуск
|
||||
browser_profile = BrowserProfile(
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
Traceback (most recent call last):
|
||||
File "/home/nafer/work/browser-use/app.py", line 8, in <module>
|
||||
from fastapi import FastAPI, HTTPException
|
||||
ModuleNotFoundError: No module named 'fastapi'
|
||||
Reference in new issue
Block a user