Files
usa/scripts/run-crawler-docker-standalone.sh

56 lines
1.8 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 生产:仅用 Docker 跑爬虫API 在宿主机(如 PM2时使用。
# 保证爬虫与 API 使用同一 SQLite 文件(数据对齐)。
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="${PROJECT_ROOT:-$(cd "$SCRIPT_DIR/.." && pwd)}"
DB_FILE="${DB_FILE:-$PROJECT_ROOT/server/data.db}"
API_BASE="${API_BASE:-http://host.docker.internal:3001}"
CRAWLER_IMAGE="${CRAWLER_IMAGE:-usa-dashboard-crawler:latest}"
CONTAINER_NAME="${CONTAINER_NAME:-usa-crawler}"
# 可选:从 .env 加载 DASHSCOPE_API_KEY 等
if [ -f "$PROJECT_ROOT/.env" ]; then
set -a
# shellcheck source=../.env
. "$PROJECT_ROOT/.env"
set +a
fi
# 宿主机 DB 必须存在API 已初始化或先 seed
if [ ! -f "$DB_FILE" ]; then
echo "ERROR: DB file not found: $DB_FILE"
echo " Create it first: DB_PATH=$DB_FILE node server/seed.js"
exit 1
fi
# Linux 下 Docker 默认无 host.docker.internal需显式添加
DOCKER_EXTRA=()
if [ "$(uname -s)" = "Linux" ]; then
DOCKER_EXTRA+=(--add-host=host.docker.internal:host-gateway)
fi
# 若已存在同名容器则先删
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
echo "==> Starting crawler container (standalone)"
echo " DB: $DB_FILE -> /data/data.db"
echo " API_BASE: $API_BASE"
echo " Image: $CRAWLER_IMAGE"
docker run -d \
--name "$CONTAINER_NAME" \
--restart unless-stopped \
-p 8000:8000 \
-v "$DB_FILE:/data/data.db" \
-e DB_PATH=/data/data.db \
-e API_BASE="$API_BASE" \
-e GDELT_DISABLED=1 \
-e RSS_INTERVAL_SEC=60 \
${DASHSCOPE_API_KEY:+ -e DASHSCOPE_API_KEY="$DASHSCOPE_API_KEY"} \
"${DOCKER_EXTRA[@]}" \
"$CRAWLER_IMAGE"
echo " Container: $CONTAINER_NAME"
echo " Logs: docker logs -f $CONTAINER_NAME"
echo " Status: curl -s http://localhost:8000/crawler/status | jq ."