增强转图浏览器启动健壮性并修正日志来源标识
变更项:1) html_to_image 改为候选浏览器逐个尝试启动,避免单一路径失败导致整体异常。2) Linux 增加系统浏览器路径探测(google-chrome/chromium)并保留 Playwright 缓存路径作为候选。3) 修正启动日志来源标识,区分 system 与 playwright-cache,避免误判。4) 所有候选失败时自动回退到 bundled 浏览器,提高转图成功率。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
|
||||
import psutil
|
||||
from playwright.async_api import async_playwright
|
||||
@@ -478,8 +479,9 @@ def check_chromium_installed(path):
|
||||
|
||||
async def html_to_image(html_content, output_image):
|
||||
async with async_playwright() as p:
|
||||
browser_path = None
|
||||
browser_candidates = []
|
||||
if os.name == 'nt':
|
||||
# Windows 优先尝试常见系统安装路径。
|
||||
possible_chrome_paths = [
|
||||
r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe",
|
||||
r"C:\Users\Liu-OPEN\AppData\Local\Google\Chrome\Application\chrome.exe",
|
||||
@@ -488,24 +490,41 @@ async def html_to_image(html_content, output_image):
|
||||
]
|
||||
for path in possible_chrome_paths:
|
||||
if check_chromium_installed(path):
|
||||
browser_path = path
|
||||
break
|
||||
browser_candidates.append(("system", path))
|
||||
else:
|
||||
import glob
|
||||
# Linux 先尝试系统可执行文件,再尝试 Playwright 缓存浏览器。
|
||||
for bin_name in ("google-chrome", "google-chrome-stable", "chromium", "chromium-browser"):
|
||||
bin_path = shutil.which(bin_name)
|
||||
if bin_path and check_chromium_installed(bin_path):
|
||||
browser_candidates.append(("system", bin_path))
|
||||
user_home = os.path.expanduser("~")
|
||||
glob_pattern = os.path.join(user_home, ".cache", "ms-playwright", "chromium-*", "chrome-linux", "chrome")
|
||||
chrome_paths = glob.glob(glob_pattern)
|
||||
for path in sorted(chrome_paths, reverse=True):
|
||||
if check_chromium_installed(path):
|
||||
browser_path = path
|
||||
break
|
||||
browser_candidates.append(("playwright-cache", path))
|
||||
|
||||
launch_args = ["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage", "--disable-gpu"]
|
||||
browser = None
|
||||
launch_errors = []
|
||||
|
||||
if browser_path:
|
||||
logger.debug(f"Launch chromium with system chrome: {browser_path}")
|
||||
browser = await p.chromium.launch(executable_path=browser_path, args=launch_args)
|
||||
else:
|
||||
# 优先按候选路径逐个尝试,失败自动降级,不让单一路径问题导致整体失败。
|
||||
for source, browser_path in browser_candidates:
|
||||
try:
|
||||
logger.debug(f"Launch chromium with {source}: {browser_path}")
|
||||
browser = await p.chromium.launch(
|
||||
executable_path=browser_path,
|
||||
args=launch_args,
|
||||
timeout=20000,
|
||||
)
|
||||
break
|
||||
except Exception as e:
|
||||
launch_errors.append(f"{source}:{browser_path} -> {e}")
|
||||
logger.warning(f"Launch chromium failed with {source}: {browser_path}, error={e}")
|
||||
|
||||
# 如果候选都失败,回退到 Playwright bundled 浏览器。
|
||||
if not browser:
|
||||
logger.debug("Launch chromium with bundled browser")
|
||||
browser = await p.chromium.launch(args=launch_args)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user