From 9a379ca116966e66073e8420d54920ec1440dfce Mon Sep 17 00:00:00 2001 From: zhinianboke <115088296+zhinianboke@users.noreply.github.com> Date: Thu, 14 Aug 2025 08:28:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 23 +++++- Dockerfile-cn | 23 +++++- Start.py | 14 ++++ install-playwright-deps.sh | 127 ++++++++++++++++++++++++++++++++++ playwright_checker.py | 70 +++++++++++++++++++ utils/order_detail_fetcher.py | 6 ++ 6 files changed, 257 insertions(+), 6 deletions(-) create mode 100644 install-playwright-deps.sh create mode 100644 playwright_checker.py diff --git a/Dockerfile b/Dockerfile index 80ff707..d64e63e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,6 +18,8 @@ ENV PYTHONDONTWRITEBYTECODE=1 ENV TZ=Asia/Shanghai ENV DOCKER_ENV=true ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright +ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=0 +ENV DEBIAN_FRONTEND=noninteractive # 安装系统依赖(包括Playwright浏览器依赖) RUN apt-get update && \ @@ -48,7 +50,6 @@ RUN apt-get update && \ libasound2 \ libatspi2.0-0 \ libgtk-3-0 \ - libgdk-pixbuf2.0-0 \ libxcursor1 \ libxi6 \ libxrender1 \ @@ -61,6 +62,10 @@ RUN apt-get update && \ libx11-xcb1 \ libxfixes3 \ xdg-utils \ + # 尝试安装 gdk-pixbuf 包(兼容不同版本) + && (apt-get install -y --no-install-recommends libgdk-pixbuf-2.0-0 || \ + apt-get install -y --no-install-recommends libgdk-pixbuf2.0-0 || \ + echo "Warning: Could not install gdk-pixbuf package") \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/* \ @@ -78,12 +83,24 @@ COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt +# 复制自定义依赖安装脚本 +COPY install-playwright-deps.sh /tmp/install-playwright-deps.sh +RUN chmod +x /tmp/install-playwright-deps.sh + # 复制项目文件 COPY . . +# 复制 Playwright 检查脚本 +COPY playwright_checker.py /app/playwright_checker.py + # 安装Playwright浏览器(必须在复制项目文件之后) -RUN playwright install chromium && \ - playwright install-deps chromium +RUN playwright install chromium + +# 使用自定义脚本安装 Playwright 依赖 +RUN /tmp/install-playwright-deps.sh || echo "Warning: Some Playwright dependencies could not be installed, but this may not affect functionality" + +# 清理安装脚本 +RUN rm -f /tmp/install-playwright-deps.sh # 创建必要的目录并设置权限 RUN mkdir -p /app/logs /app/data /app/backups /app/static/uploads/images && \ diff --git a/Dockerfile-cn b/Dockerfile-cn index 69d7193..6b42d75 100644 --- a/Dockerfile-cn +++ b/Dockerfile-cn @@ -18,6 +18,8 @@ ENV PYTHONDONTWRITEBYTECODE=1 ENV TZ=Asia/Shanghai ENV DOCKER_ENV=true ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright +ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=0 +ENV DEBIAN_FRONTEND=noninteractive #更换中科大源 RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources @@ -51,7 +53,6 @@ RUN apt-get update && \ libasound2 \ libatspi2.0-0 \ libgtk-3-0 \ - libgdk-pixbuf2.0-0 \ libxcursor1 \ libxi6 \ libxrender1 \ @@ -64,6 +65,10 @@ RUN apt-get update && \ libx11-xcb1 \ libxfixes3 \ xdg-utils \ + # 尝试安装 gdk-pixbuf 包(兼容不同版本) + && (apt-get install -y --no-install-recommends libgdk-pixbuf-2.0-0 || \ + apt-get install -y --no-install-recommends libgdk-pixbuf2.0-0 || \ + echo "Warning: Could not install gdk-pixbuf package") \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/* \ @@ -81,12 +86,24 @@ COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple&& \ pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple +# 复制自定义依赖安装脚本 +COPY install-playwright-deps.sh /tmp/install-playwright-deps.sh +RUN chmod +x /tmp/install-playwright-deps.sh + # 复制项目文件 COPY . . +# 复制 Playwright 检查脚本 +COPY playwright_checker.py /app/playwright_checker.py + # 安装Playwright浏览器(必须在复制项目文件之后) -RUN playwright install chromium && \ - playwright install-deps chromium +RUN playwright install chromium + +# 使用自定义脚本安装 Playwright 依赖 +RUN /tmp/install-playwright-deps.sh || echo "Warning: Some Playwright dependencies could not be installed, but this may not affect functionality" + +# 清理安装脚本 +RUN rm -f /tmp/install-playwright-deps.sh # 创建必要的目录并设置权限 RUN mkdir -p /app/logs /app/data /app/backups /app/static/uploads/images && \ diff --git a/Start.py b/Start.py index fb29ce4..2b68b4e 100644 --- a/Start.py +++ b/Start.py @@ -76,6 +76,20 @@ async def main(): setup_file_logging() logger.info("文件日志收集器已启动,开始收集实时日志") + # 检查 Playwright 可用性 + print("检查 Playwright 可用性...") + try: + from playwright_checker import check_playwright_sync + playwright_available = check_playwright_sync() + if playwright_available: + logger.info("✅ Playwright 功能正常,订单详情获取和多数量发货功能可用") + else: + logger.warning("⚠️ Playwright 功能异常,订单详情获取功能将不可用") + logger.warning("系统将以降级模式运行,基础功能不受影响") + except Exception as e: + logger.error(f"Playwright 检查失败: {e}") + logger.warning("系统将以降级模式运行,基础功能不受影响") + loop = asyncio.get_running_loop() # 创建 CookieManager 并在全局暴露 diff --git a/install-playwright-deps.sh b/install-playwright-deps.sh new file mode 100644 index 0000000..7efce94 --- /dev/null +++ b/install-playwright-deps.sh @@ -0,0 +1,127 @@ +#!/bin/bash + +# 自定义 Playwright 依赖安装脚本 +# 用于处理新版 Debian 中的包兼容性问题 + +# 不要在错误时立即退出,我们要尝试安装尽可能多的包 +set +e + +echo "开始安装 Playwright 依赖..." + +# 更新包列表 +apt-get update + +# 记录安装状态 +INSTALL_SUCCESS=0 +INSTALL_FAILED=0 + +# 定义核心依赖包列表 +CORE_PACKAGES=( + "libnss3" + "libnspr4" + "libatk-bridge2.0-0" + "libdrm2" + "libxkbcommon0" + "libxcomposite1" + "libxdamage1" + "libxrandr2" + "libgbm1" + "libxss1" + "libasound2" + "libatspi2.0-0" + "libgtk-3-0" + "libxcursor1" + "libxi6" + "libxrender1" + "libxext6" + "libx11-6" + "libxft2" + "libxinerama1" + "libxtst6" + "libappindicator3-1" + "libx11-xcb1" + "libxfixes3" + "xdg-utils" +) + +# 安装核心依赖包 +echo "安装核心依赖包..." +for package in "${CORE_PACKAGES[@]}"; do + if apt-get install -y --no-install-recommends "$package"; then + echo "✅ 成功安装: $package" + ((INSTALL_SUCCESS++)) + else + echo "❌ 安装失败: $package" + ((INSTALL_FAILED++)) + fi +done + +# 尝试安装 gdk-pixbuf 包(兼容不同版本) +echo "安装 gdk-pixbuf 包..." +if apt-get install -y --no-install-recommends libgdk-pixbuf-2.0-0; then + echo "✅ 成功安装: libgdk-pixbuf-2.0-0" + ((INSTALL_SUCCESS++)) +elif apt-get install -y --no-install-recommends libgdk-pixbuf2.0-0; then + echo "✅ 成功安装: libgdk-pixbuf2.0-0" + ((INSTALL_SUCCESS++)) +else + echo "❌ 安装失败: gdk-pixbuf packages" + ((INSTALL_FAILED++)) +fi + +# 定义字体包列表 +FONT_PACKAGES=( + "fonts-unifont" + "fonts-ubuntu" + "fonts-noto" + "fonts-noto-cjk" + "fonts-noto-color-emoji" +) + +# 安装字体包 +echo "安装字体包..." +for package in "${FONT_PACKAGES[@]}"; do + if apt-get install -y --no-install-recommends "$package"; then + echo "✅ 成功安装: $package" + ((INSTALL_SUCCESS++)) + else + echo "❌ 安装失败: $package" + ((INSTALL_FAILED++)) + fi +done + +# 清理 +apt-get clean +rm -rf /var/lib/apt/lists/* +rm -rf /tmp/* +rm -rf /var/tmp/* + +# 输出安装结果 +echo "==================================" +echo "Playwright 依赖安装完成" +echo "成功安装: $INSTALL_SUCCESS 个包" +echo "安装失败: $INSTALL_FAILED 个包" +echo "==================================" + +# 检查关键依赖是否安装成功 +CRITICAL_PACKAGES=("libnss3" "libnspr4" "libgtk-3-0" "libgbm1") +CRITICAL_MISSING=0 + +echo "检查关键依赖..." +for package in "${CRITICAL_PACKAGES[@]}"; do + if dpkg -l | grep -q "^ii.*$package"; then + echo "✅ 关键依赖已安装: $package" + else + echo "❌ 关键依赖缺失: $package" + ((CRITICAL_MISSING++)) + fi +done + +if [ $CRITICAL_MISSING -eq 0 ]; then + echo "🎉 所有关键依赖都已成功安装,Playwright 应该能正常工作" + exit 0 +else + echo "⚠️ 有 $CRITICAL_MISSING 个关键依赖缺失,Playwright 可能无法正常工作" + echo "但系统的其他功能不会受到影响" + exit 0 # 不要让构建失败 +fi diff --git a/playwright_checker.py b/playwright_checker.py new file mode 100644 index 0000000..1f05d42 --- /dev/null +++ b/playwright_checker.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Playwright 可用性检查工具 +在系统启动时检查 Playwright 是否正常工作 +""" + +import asyncio +import sys +import os +from loguru import logger + +async def check_playwright_availability(): + """检查 Playwright 是否可用""" + try: + from playwright.async_api import async_playwright + + logger.info("正在检查 Playwright 可用性...") + + async with async_playwright() as p: + # 尝试启动浏览器 + browser = await p.chromium.launch(headless=True) + + # 创建页面 + page = await browser.new_page() + + # 访问一个简单的页面 + await page.goto("data:text/html,

Test

") + + # 获取页面标题 + title = await page.title() + + # 关闭浏览器 + await browser.close() + + logger.info("✅ Playwright 检查通过,功能正常") + return True + + except ImportError as e: + logger.error(f"❌ Playwright 模块未安装: {e}") + return False + except Exception as e: + logger.error(f"❌ Playwright 检查失败: {e}") + return False + +def check_playwright_sync(): + """同步版本的 Playwright 检查""" + try: + # 在 Docker 环境中设置事件循环策略 + if sys.platform.startswith('linux') or os.getenv('DOCKER_ENV'): + try: + asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy()) + except Exception as e: + logger.warning(f"设置事件循环策略失败: {e}") + + # 运行异步检查 + return asyncio.run(check_playwright_availability()) + except Exception as e: + logger.error(f"❌ Playwright 同步检查失败: {e}") + return False + +if __name__ == "__main__": + # 命令行测试 + success = check_playwright_sync() + if success: + print("Playwright 可用性检查: 通过") + sys.exit(0) + else: + print("Playwright 可用性检查: 失败") + sys.exit(1) diff --git a/utils/order_detail_fetcher.py b/utils/order_detail_fetcher.py index dc56fff..e3688ff 100644 --- a/utils/order_detail_fetcher.py +++ b/utils/order_detail_fetcher.py @@ -665,6 +665,12 @@ async def fetch_order_detail_simple(order_id: str, cookie_string: str = None, he - timestamp: 获取时间戳 失败时返回None """ + # 检查 Playwright 是否可用 + try: + from playwright.async_api import async_playwright + except ImportError: + logger.error("Playwright 未安装,无法获取订单详情") + return None # 先检查数据库中是否有有效数据 try: from db_manager import db_manager