From ccd66dfa11202ba0eb316e0d285c3f60e2eac552 Mon Sep 17 00:00:00 2001 From: Vaica <94612053+zqlit@users.noreply.github.com> Date: Wed, 24 Jun 2026 13:52:16 +0800 Subject: [PATCH] 1352 --- write-server/diagnose-bot.sh | 109 ++++++++++++++++++++++++++++ write-server/src/instrumentation.ts | 14 +++- 2 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 write-server/diagnose-bot.sh diff --git a/write-server/diagnose-bot.sh b/write-server/diagnose-bot.sh new file mode 100644 index 00000000..f89b2ea7 --- /dev/null +++ b/write-server/diagnose-bot.sh @@ -0,0 +1,109 @@ +#!/bin/bash +# 诊断 Bot 启动问题 + +echo "" +echo "===================================" +echo " Bot 启动诊断" +echo "===================================" +echo "" + +# 1. 检查 instrumentation 文件 +echo "1. 检查 instrumentation 文件..." +if [ -f "src/instrumentation.ts" ]; then + echo "✓ src/instrumentation.ts 存在" +elif [ -f "src/instrumentation.js" ]; then + echo "✓ src/instrumentation.js 存在" +elif [ -f "instrumentation.ts" ]; then + echo "✓ instrumentation.ts 存在(根目录)" +elif [ -f "instrumentation.js" ]; then + echo "✓ instrumentation.js 存在(根目录)" +else + echo "✗ 未找到 instrumentation 文件!" + echo " 应该在: src/instrumentation.ts 或 instrumentation.ts" + exit 1 +fi + +# 2. 检查 register 函数导出 +echo "" +echo "2. 检查 register 函数..." +if grep -q "export async function register" src/instrumentation.ts 2>/dev/null || \ + grep -q "export async function register" src/instrumentation.js 2>/dev/null || \ + grep -q "export async function register" instrumentation.ts 2>/dev/null || \ + grep -q "export async function register" instrumentation.js 2>/dev/null; then + echo "✓ register 函数已导出" +else + echo "✗ 未找到 export async function register() 导出" +fi + +# 3. 检查 next.config.ts +echo "" +echo "3. 检查 next.config.ts..." +if grep -q "instrumentationHook" next.config.ts 2>/dev/null; then + echo "⚠ 发现 instrumentationHook 配置(Next.js 16 不需要,应该移除)" +else + echo "✓ 配置文件正常" +fi + +# 4. 检查 bot 入口文件 +echo "" +echo "4. 检查 bot 启动文件..." +if [ -f "src/lib/bot/index.ts" ]; then + echo "✓ src/lib/bot/index.ts 存在" + if grep -q "export async function startBot" src/lib/bot/index.ts; then + echo "✓ startBot 函数已导出" + else + echo "✗ 未找到 startBot 导出" + fi +else + echo "✗ src/lib/bot/index.ts 不存在" +fi + +# 5. 检查环境变量 +echo "" +echo "5. 检查环境变量..." +if [ -f ".env" ]; then + if grep -q "TG_BOT_TOKEN=" .env && ! grep -q "TG_BOT_TOKEN=$" .env; then + echo "✓ TG_BOT_TOKEN 已配置" + else + echo "⚠ TG_BOT_TOKEN 未配置或为空" + fi + + if grep -q "TG_ALLOWED_CHAT_IDS=" .env && ! grep -q "TG_ALLOWED_CHAT_IDS=$" .env; then + echo "✓ TG_ALLOWED_CHAT_IDS 已配置" + else + echo "⚠ TG_ALLOWED_CHAT_IDS 未配置或为空" + fi + + if grep -q "ARTALK_SERVER=" .env && ! grep -q "ARTALK_SERVER=$" .env; then + echo "✓ ARTALK_SERVER 已配置" + else + echo "⚠ ARTALK_SERVER 未配置或为空" + fi +else + echo "⚠ .env 文件不存在" +fi + +# 6. 检查容器状态 +echo "" +echo "6. 检查容器状态..." +if command -v docker &> /dev/null; then + if docker compose ps 2>/dev/null | grep -q "Up"; then + echo "✓ 容器正在运行" + + echo "" + echo "7. 检查容器日志..." + echo "--- 最近的日志 ---" + docker compose logs --tail 30 2>&1 | grep -E "\[instrumentation\]|\[bot\]|\[comment-notify\]|error" || echo "未找到相关日志" + + else + echo "✗ 容器未运行" + fi +else + echo "⚠ docker 命令不可用" +fi + +echo "" +echo "===================================" +echo " 诊断完成" +echo "===================================" +echo "" diff --git a/write-server/src/instrumentation.ts b/write-server/src/instrumentation.ts index ded2b0a1..2f6c5bb0 100644 --- a/write-server/src/instrumentation.ts +++ b/write-server/src/instrumentation.ts @@ -1,8 +1,14 @@ export async function register() { - if (process.env.NEXT_RUNTIME === "nodejs") { + // Next.js instrumentation hook - 在服务器启动时执行 + console.log("[instrumentation] register() called, NEXT_RUNTIME:", process.env.NEXT_RUNTIME); + + // 只在 Node.js 环境运行(Docker 部署,非 Edge Runtime) + try { const { startBot } = await import("@/lib/bot"); - startBot().catch((e) => { - console.error("[instrumentation] bot startup failed:", e); - }); + console.log("[instrumentation] starting bot..."); + await startBot(); + console.log("[instrumentation] bot started successfully"); + } catch (e) { + console.error("[instrumentation] bot startup failed:", e); } }