feat: add dev-daemon target for background development mode

Add a new make dev-daemon target that allows running DeerFlow services
in background mode without keeping the terminal connection.

Following the pattern of PR #1042, the implementation uses a dedicated
shell script (scripts/start-daemon.sh) for better maintainability.

- Create scripts/start-daemon.sh for daemon mode startup
- Add dev-daemon target to Makefile
- Each service writes logs to separate files (langgraph, gateway, frontend, nginx)
- Services can be stopped with make stop
- Use nohup for proper daemon process detachment
- Add cleanup on failure when services fail to start
- Use more specific pkill pattern to avoid killing unrelated nginx processes
This commit is contained in:
zhh
2026-03-10 09:14:37 +08:00
parent 33f086b612
commit e4f6c3ff02
2 changed files with 137 additions and 1 deletions
+131
View File
@@ -0,0 +1,131 @@
#!/usr/bin/env bash
#
# start-daemon.sh - Start all DeerFlow development services in daemon mode
#
# This script starts DeerFlow services in the background without keeping
# the terminal connection. Logs are written to separate files.
#
# Must be run from the repo root directory.
set -e
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
# ── Stop existing services ────────────────────────────────────────────────────
echo "Stopping existing services if any..."
pkill -f "langgraph dev" 2>/dev/null || true
pkill -f "uvicorn src.gateway.app:app" 2>/dev/null || true
pkill -f "next dev" 2>/dev/null || true
nginx -c "$REPO_ROOT/docker/nginx/nginx.local.conf" -p "$REPO_ROOT" -s quit 2>/dev/null || true
sleep 1
pkill -9 -f nginx.local.conf 2>/dev/null || true
./scripts/cleanup-containers.sh deer-flow-sandbox 2>/dev/null || true
sleep 1
# ── Banner ────────────────────────────────────────────────────────────────────
echo ""
echo "=========================================="
echo " Starting DeerFlow in Daemon Mode"
echo "=========================================="
echo ""
# ── Config check ─────────────────────────────────────────────────────────────
if ! { \
[ -n "$DEER_FLOW_CONFIG_PATH" ] && [ -f "$DEER_FLOW_CONFIG_PATH" ] || \
[ -f backend/config.yaml ] || \
[ -f config.yaml ]; \
}; then
echo "✗ No DeerFlow config file found."
echo " Checked these locations:"
echo " - $DEER_FLOW_CONFIG_PATH (when DEER_FLOW_CONFIG_PATH is set)"
echo " - backend/config.yaml"
echo " - ./config.yaml"
echo ""
echo " Run 'make config' from the repo root to generate ./config.yaml, then set required model API keys in .env or your config file."
exit 1
fi
# ── Cleanup on failure ───────────────────────────────────────────────────────
cleanup_on_failure() {
echo "Failed to start services, cleaning up..."
pkill -f "langgraph dev" 2>/dev/null || true
pkill -f "uvicorn src.gateway.app:app" 2>/dev/null || true
pkill -f "next dev" 2>/dev/null || true
nginx -c "$REPO_ROOT/docker/nginx/nginx.local.conf" -p "$REPO_ROOT" -s quit 2>/dev/null || true
sleep 1
pkill -9 -f nginx.local.conf 2>/dev/null || true
echo "✓ Cleanup complete"
}
# ── Start services ────────────────────────────────────────────────────────────
mkdir -p logs
echo "Starting LangGraph server..."
nohup sh -c 'cd backend && NO_COLOR=1 uv run langgraph dev --no-browser --allow-blocking --no-reload > ../logs/langgraph.log 2>&1' &
sleep 3
if ! lsof -i :2024 -sTCP:LISTEN -t >/dev/null 2>&1; then
echo "✗ LangGraph failed to start. Last log output:"
tail -60 logs/langgraph.log
cleanup_on_failure
exit 1
fi
echo "✓ LangGraph server started on localhost:2024"
echo "Starting Gateway API..."
nohup sh -c 'cd backend && uv run uvicorn src.gateway.app:app --host 0.0.0.0 --port 8001 > ../logs/gateway.log 2>&1' &
sleep 3
if ! lsof -i :8001 -sTCP:LISTEN -t >/dev/null 2>&1; then
echo "✗ Gateway API failed to start. Last log output:"
tail -60 logs/gateway.log
cleanup_on_failure
exit 1
fi
echo "✓ Gateway API started on localhost:8001"
echo "Starting Frontend..."
nohup sh -c 'cd frontend && pnpm run dev > ../logs/frontend.log 2>&1' &
sleep 3
if ! lsof -i :3000 -sTCP:LISTEN -t >/dev/null 2>&1; then
echo "✗ Frontend failed to start. Last log output:"
tail -60 logs/frontend.log
cleanup_on_failure
exit 1
fi
echo "✓ Frontend started on localhost:3000"
echo "Starting Nginx reverse proxy..."
nohup sh -c 'nginx -g "daemon off;" -c '"$REPO_ROOT"'/docker/nginx/nginx.local.conf -p '"$REPO_ROOT"' > logs/nginx.log 2>&1' &
sleep 2
if ! lsof -i :2026 -sTCP:LISTEN -t >/dev/null 2>&1; then
echo "✗ Nginx failed to start. Last log output:"
tail -60 logs/nginx.log
cleanup_on_failure
exit 1
fi
echo "✓ Nginx started on localhost:2026"
# ── Ready ─────────────────────────────────────────────────────────────────────
echo ""
echo "=========================================="
echo " DeerFlow is running in daemon mode!"
echo "=========================================="
echo ""
echo " 🌐 Application: http://localhost:2026"
echo " 📡 API Gateway: http://localhost:2026/api/*"
echo " 🤖 LangGraph: http://localhost:2026/api/langgraph/*"
echo ""
echo " 📋 Logs:"
echo " - LangGraph: logs/langgraph.log"
echo " - Gateway: logs/gateway.log"
echo " - Frontend: logs/frontend.log"
echo " - Nginx: logs/nginx.log"
echo ""
echo " 🛑 Stop daemon: make stop"
echo ""