修改部署文件
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1m22s

This commit is contained in:
Song367 2025-07-29 18:34:11 +08:00
parent 345533ed6e
commit d06e94ad11
3 changed files with 10 additions and 104 deletions

View File

@ -1,113 +1,23 @@
# 使用官方Node.js运行时作为基础镜像
FROM node:18-alpine
# 安装系统依赖和监控工具
RUN apk add --no-cache \
bash \
procps \
curl \
&& npm install -g yarn nodemon
# 全局安装yarn
RUN npm install -g yarn
# 设置工作目录
WORKDIR /app
# 复制package.json和yarn.lock(如果存在)
# 复制package.json和yarn.lock
COPY package.json yarn.lock* ./
# 安装项目依赖
RUN yarn install --frozen-lockfile
RUN yarn install
# 复制项目文件
COPY . .
# 创建videos目录如果不存在
RUN mkdir -p videos
# 创建CPU监控和重启脚本
RUN cat > /app/monitor.sh << 'EOF'
#!/bin/bash
# CPU阈值设置百分比
CPU_THRESHOLD=80
# 检查间隔(秒)
CHECK_INTERVAL=30
# 日志文件
LOG_FILE="/app/monitor.log"
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
get_cpu_usage() {
# 获取Node.js进程的CPU使用率
local pid=$(pgrep -f "node.*server.js" | head -1)
if [ -n "$pid" ]; then
ps -p $pid -o %cpu --no-headers | awk '{print int($1)}'
else
echo "0"
fi
}
restart_app() {
log_message "CPU使用率过高重启应用..."
pkill -f "node.*server.js"
sleep 5
cd /app
yarn dev &
log_message "应用已重启"
}
log_message "开始CPU监控阈值: ${CPU_THRESHOLD}%"
while true; do
cpu_usage=$(get_cpu_usage)
log_message "当前CPU使用率: ${cpu_usage}%"
if [ "$cpu_usage" -gt "$CPU_THRESHOLD" ]; then
log_message "警告: CPU使用率 ${cpu_usage}% 超过阈值 ${CPU_THRESHOLD}%"
restart_app
fi
sleep $CHECK_INTERVAL
done
EOF
# 给监控脚本执行权限
RUN chmod +x /app/monitor.sh
# 创建启动脚本
RUN cat > /app/start.sh << 'EOF'
#!/bin/bash
# 启动应用
echo "启动WebRTC应用..."
cd /app
yarn dev &
# 等待应用启动
sleep 10
# 启动CPU监控
echo "启动CPU监控..."
/app/monitor.sh &
# 保持容器运行
wait
EOF
# 给启动脚本执行权限
RUN chmod +x /app/start.sh
# 暴露端口默认3000支持环境变量配置
# 暴露端口
EXPOSE 3000
# 设置环境变量
ENV NODE_ENV=production
ENV PORT=3000
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:$PORT || exit 1
# 启动应用和监控
CMD ["/app/start.sh"]
# 启动项目
CMD ["yarn", "dev"]

View File

@ -4,13 +4,9 @@ services:
webrtc-app:
build: .
ports:
- "3000:3000" # 映射到主机的3000端口外网可访问
environment:
- NODE_ENV=production
- PORT=3000
- "3000:3000"
volumes:
- ./videos:/app/videos # 挂载视频文件目录
- ./logs:/app/logs # 挂载日志目录
- ./videos:/app/videos
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000"]

View File

@ -280,7 +280,7 @@ io.on('connection', (socket) => {
// 启动服务器
const PORT = process.env.PORT || 3000;
server.listen(PORT, async () => {
server.listen(PORT, '0.0.0.0', async () => {
console.log(`服务器运行在端口 ${PORT}`);
await initializeServer();
});