diff --git a/Dockerfile b/Dockerfile index f4ae830..777eebd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,11 @@ # 阶段一:构建阶段 FROM python:3.10 AS builder -# 合并命令以减少层数,并安装时区数据 +# 合并命令以减少层数,并安装时区数据,同时清理缓存和不再需要的文件 RUN apt-get update && \ apt-get install -y --no-install-recommends tzdata && \ ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone && \ - # 清理缓存和不再需要的文件 apt-get clean && \ rm -rf /var/lib/apt/lists/* @@ -15,10 +14,9 @@ WORKDIR /root/VintageVigil # 将依赖文件单独复制,以利用Docker缓存 COPY requirements.txt . -# 安装Python依赖 -RUN pip install --no-cache-dir -r requirements.txt --target /root/VintageVigil/dependencies && \ - # 清理pip缓存 - rm -rf /root/.cache/pip + +# 安装Python依赖,并清理pip缓存 +RUN pip install --no-cache-dir -r requirements.txt --target /root/dependencies # 将代码复制到容器内 COPY . . @@ -30,7 +28,7 @@ FROM python:3.10-slim AS runner COPY --from=builder /etc/localtime /etc/localtime COPY --from=builder /etc/timezone /etc/timezone -# 安装运行时依赖 +# 安装运行时依赖,并清理APT缓存 RUN apt-get update && \ apt-get install -y --no-install-recommends jq curl && \ apt-get clean && \ @@ -40,7 +38,7 @@ RUN apt-get update && \ WORKDIR /root/VintageVigil # 从构建阶段复制已安装的依赖 -COPY --from=builder /root/VintageVigil/dependencies /usr/local/lib/python3.10/site-packages +COPY --from=builder /root/dependencies /usr/local/lib/python3.10/site-packages # 从构建阶段复制项目文件 COPY --from=builder /root/VintageVigil . @@ -48,6 +46,7 @@ COPY --from=builder /root/VintageVigil . # 确保启动脚本具有执行权限 RUN chmod +x /root/VintageVigil/start.sh + # 设置容器启动时执行的命令(根据你的需要选择一个) # 自动监控 # CMD ["bash", "./run_checker.sh"] diff --git a/Dockerfile-Alpine b/Dockerfile-Alpine index e7a0428..6edacef 100644 --- a/Dockerfile-Alpine +++ b/Dockerfile-Alpine @@ -1,29 +1,22 @@ # 阶段一:构建阶段 FROM python:3.10-alpine AS builder -# 安装时区数据和其他依赖 +# 设置工作目录 +WORKDIR /root/VintageVigil + +# 安装时区数据和依赖,同时清理缓存 RUN apk update && \ - apk add --no-cache tzdata && \ + apk add --no-cache tzdata build-base libffi-dev openssl-dev && \ cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone && \ - apk del tzdata && \ rm -rf /var/cache/apk/* -# 设置工作目录 -WORKDIR /root/VintageVigil - -# 安装构建依赖 -# 注意:Alpine 上可能需要安装额外的依赖来编译某些 Python 包 -RUN apk add --no-cache --virtual .build-deps \ - build-base \ - libffi-dev \ - openssl-dev - # 将依赖文件单独复制,以利用 Docker 缓存 COPY requirements.txt . -# 安装 Python 依赖 -RUN pip install --no-cache-dir -r requirements.txt --target /root/VintageVigil/dependencies && \ - apk del .build-deps + +# 安装 Python 依赖并清理构建时依赖 +RUN pip install --no-cache-dir -r requirements.txt --target /root/dependencies && \ + apk del build-base libffi-dev openssl-dev # 将代码复制到容器内 COPY . . @@ -31,9 +24,10 @@ COPY . . # 阶段二:运行阶段 FROM python:3.10-alpine AS runner -# 复制时区设置 +# 复制时区设置和已安装的依赖 COPY --from=builder /etc/localtime /etc/localtime COPY --from=builder /etc/timezone /etc/timezone +COPY --from=builder /root/dependencies /usr/local/lib/python3.10/site-packages # 安装运行时依赖 RUN apk add --no-cache jq curl @@ -41,9 +35,6 @@ RUN apk add --no-cache jq curl # 设置工作目录 WORKDIR /root/VintageVigil -# 从构建阶段复制已安装的依赖 -COPY --from=builder /root/VintageVigil/dependencies /usr/local/lib/python3.10/site-packages - # 从构建阶段复制项目文件 COPY --from=builder /root/VintageVigil . diff --git a/common/logger/logger.py b/common/logger/logger.py index 0281fa6..cf0030c 100644 --- a/common/logger/logger.py +++ b/common/logger/logger.py @@ -16,17 +16,6 @@ def filter(record): def setup_logger(websites, user_path): log_path = f"{user_path}/logs" - # 检查环境变量DEBUG的值 - debug_mode = os.getenv("DEBUG", "false").lower() == "true" - - # 根据DEBUG的值选择不同的日志格式 - if debug_mode: - # 包含文件名和行号的详细日志格式 - log_format = "{time:YYYY-MM-DD HH:mm:ss:SSS} | {level: <8} | {file}:{line} | {message}" - else: - # 不包含文件名和行号的简洁日志格式 - log_format = "{time:YYYY-MM-DD HH:mm:ss:SSS} | {level: <8} | {message}" - for website in websites: for index, search in enumerate(website[1:]): keyword = search.get("keyword") @@ -35,7 +24,6 @@ def setup_logger(websites, user_path): logger.add( f"{log_path}/{website_name}-{index}.log", filter=get_website_keyword_filter(website_name, keyword, user_path), - format=log_format, # 使用根据DEBUG值选择的日志格式 rotation="10 MB", # 每个日志文件最大10MB retention="24 hours", # 保留24小时内的日志 enqueue=True, # 异步写入日志 diff --git a/main.py b/main.py index 410cd75..b49d103 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ from telebot import asyncio_helper import argparse from loguru import logger - +import sys from monitor import setup_and_monitor from common import AsyncHTTPXClient, AsyncAIOHTTPClient @@ -23,7 +23,7 @@ def __init__(self, base_path="user", direct_user_path=None): self.direct_user_path = direct_user_path self.http_client = None self.telegram_bots = {} - + async def initialize_resources(self, parse_mode=None): """ Initializes the necessary resources for monitoring. @@ -37,6 +37,19 @@ async def initialize_resources(self, parse_mode=None): load_dotenv() + # 检查环境变量DEBUG的值 + debug_mode = os.getenv("DEBUG", "false").lower() == "true" + + # 根据DEBUG的值选择不同的日志格式 + if debug_mode: + # 包含文件名和行号的详细日志格式 + log_format = "{time:YYYY-MM-DD HH:mm:ss:SSS} | {level: <8} | {file}:{line} | {message}" + else: + # 不包含文件名和行号的简洁日志格式 + log_format = "{time:YYYY-MM-DD HH:mm:ss:SSS} | {level: <8} | {message}" + + logger.configure(handlers=[{"sink": sys.stdout, "format": log_format}]) + proxy = os.getenv('HTTP_PROXY') timeout = 10.0