Skip to content

Commit

Permalink
优化镜像打包以及日志打印方式
Browse files Browse the repository at this point in the history
  • Loading branch information
Samiya committed Mar 7, 2024
1 parent 9f11a80 commit e9beff2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 42 deletions.
15 changes: 7 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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/*

Expand All @@ -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 . .
Expand All @@ -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 && \
Expand All @@ -40,14 +38,15 @@ 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 .

# 确保启动脚本具有执行权限
RUN chmod +x /root/VintageVigil/start.sh


# 设置容器启动时执行的命令(根据你的需要选择一个)
# 自动监控
# CMD ["bash", "./run_checker.sh"]
Expand Down
31 changes: 11 additions & 20 deletions Dockerfile-Alpine
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
# 阶段一:构建阶段
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 . .

# 阶段二:运行阶段
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

# 设置工作目录
WORKDIR /root/VintageVigil

# 从构建阶段复制已安装的依赖
COPY --from=builder /root/VintageVigil/dependencies /usr/local/lib/python3.10/site-packages

# 从构建阶段复制项目文件
COPY --from=builder /root/VintageVigil .

Expand Down
12 changes: 0 additions & 12 deletions common/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<green>{time:YYYY-MM-DD HH:mm:ss:SSS}</green> | <level>{level: <8}</level> | <cyan>{file}</cyan>:<cyan>{line}</cyan> | <level>{message}</level>"
else:
# 不包含文件名和行号的简洁日志格式
log_format = "<green>{time:YYYY-MM-DD HH:mm:ss:SSS}</green> | <level>{level: <8}</level> | <level>{message}</level>"

for website in websites:
for index, search in enumerate(website[1:]):
keyword = search.get("keyword")
Expand All @@ -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, # 异步写入日志
Expand Down
17 changes: 15 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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 = "<green>{time:YYYY-MM-DD HH:mm:ss:SSS}</green> | <level>{level: <8}</level> | <cyan>{file}</cyan>:<cyan>{line}</cyan> | <level>{message}</level>"
else:
# 不包含文件名和行号的简洁日志格式
log_format = "<green>{time:YYYY-MM-DD HH:mm:ss:SSS}</green> | <level>{level: <8}</level> | <level>{message}</level>"

logger.configure(handlers=[{"sink": sys.stdout, "format": log_format}])

proxy = os.getenv('HTTP_PROXY')

timeout = 10.0
Expand Down

0 comments on commit e9beff2

Please sign in to comment.