#!/bin/bash # scripts/vibe-kanban/vibe-dev-server.sh # Simplified script for Vibe Kanban - starts API and Workers only # Assumes database setup is already done by vibe-setup.sh # Detect worktree root WORKTREE_ROOT="$(pwd)" # Check if we're in a nested structure (Vibe Kanban worktree) if [ -d "$WORKTREE_ROOT/managing-apps" ] && [ -d "$WORKTREE_ROOT/managing-apps/src/Managing.Api" ]; then WORKTREE_PROJECT_ROOT="$WORKTREE_ROOT/managing-apps" elif [ -d "$WORKTREE_ROOT/src/Managing.Api" ]; then WORKTREE_PROJECT_ROOT="$WORKTREE_ROOT" else echo "❌ Cannot find project structure in worktree" echo " Current directory: $WORKTREE_ROOT" exit 1 fi echo "📁 Worktree project root: $WORKTREE_PROJECT_ROOT" # TASK_ID file to ensure consistency (same as vibe-setup.sh) TASK_ID_FILE="$WORKTREE_PROJECT_ROOT/.vibe-task-id" # Load setup configuration if available SETUP_CONFIG_FILE="$WORKTREE_PROJECT_ROOT/.vibe-setup.env" if [ -f "$SETUP_CONFIG_FILE" ]; then echo "📋 Loading setup configuration from: $SETUP_CONFIG_FILE" source "$SETUP_CONFIG_FILE" echo " Task ID: $TASK_ID" echo " Task Slot: ${TASK_SLOT:-not set}" echo " Port offset: $PORT_OFFSET" echo " API Port: $API_PORT" else echo "⚠️ Setup configuration not found: $SETUP_CONFIG_FILE" echo "💡 Run scripts/vibe-kanban/vibe-setup.sh first to set up the database" # Try to get TASK_ID from stored file (ensures consistency) if [ -f "$TASK_ID_FILE" ]; then TASK_ID=$(cat "$TASK_ID_FILE" 2>/dev/null | tr -d '[:space:]') if [ -n "$TASK_ID" ]; then echo "📋 Using stored TASK_ID: $TASK_ID" fi fi # Try command line argument if [ -z "$TASK_ID" ]; then TASK_ID=${1:-""} fi # Try environment variables if [ -z "$TASK_ID" ]; then if [ -n "$VIBE_TASK_ID" ]; then TASK_ID="$VIBE_TASK_ID" elif [ -n "$VIBE_TASK_NAME" ]; then TASK_ID="$VIBE_TASK_NAME" fi fi PORT_OFFSET=${2:-0} if [ -z "$TASK_ID" ]; then echo "❌ TASK_ID is required" echo "💡 Usage: $0 [PORT_OFFSET]" echo "💡 Or run scripts/vibe-kanban/vibe-setup.sh first to create setup configuration" exit 1 fi API_PORT=$((5000 + PORT_OFFSET)) # Extract TASK_SLOT from TASK_ID if not in config if [ -z "$TASK_SLOT" ]; then TASK_SLOT=$(echo "$TASK_ID" | grep -oE '[0-9]+' | head -1) if [ -z "$TASK_SLOT" ] || [ "$TASK_SLOT" = "0" ]; then TASK_SLOT=$((PORT_OFFSET / 10 + 1)) fi fi echo " Using Task ID: $TASK_ID" echo " Using Task Slot: $TASK_SLOT" echo " Using Port offset: $PORT_OFFSET" fi # Find main repository MAIN_REPO_PATHS=( "/Users/oda/Desktop/Projects/managing-apps" "$(git -C "$WORKTREE_PROJECT_ROOT" rev-parse --show-toplevel 2>/dev/null || echo '')" "$(dirname "$WORKTREE_ROOT" 2>/dev/null)/managing-apps" "${MAIN_REPO:-}" ) MAIN_REPO="" for path in "${MAIN_REPO_PATHS[@]}"; do if [ -n "$path" ] && [ -d "$path" ] && [ -d "$path/scripts" ] && [ -f "$path/scripts/start-api-and-workers.sh" ]; then MAIN_REPO="$path" break fi done if [ -z "$MAIN_REPO" ]; then echo "❌ Cannot find main repository with scripts" exit 1 fi echo "📁 Main repository: $MAIN_REPO" echo "🚀 Starting API and Workers..." echo " Task ID: $TASK_ID" echo " Port offset: $PORT_OFFSET" # Verify database is ready if [ -n "$POSTGRES_PORT" ]; then echo "🔍 Verifying database is ready on port $POSTGRES_PORT..." if ! PGPASSWORD=postgres psql -h localhost -p $POSTGRES_PORT -U postgres -d postgres -c '\q' 2>/dev/null; then echo "❌ Database is not ready on port $POSTGRES_PORT" echo "💡 Run scripts/vibe-kanban/vibe-setup.sh first to set up the database" exit 1 fi echo "✅ Database is ready" fi # Export worktree path so main repo scripts know where to run dotnet from export VIBE_WORKTREE_ROOT="$WORKTREE_PROJECT_ROOT" # Start API and Workers only (database setup is already done) bash "$MAIN_REPO/scripts/start-api-and-workers.sh" "$TASK_ID" "$PORT_OFFSET" # Determine log file paths (logs are created in the worktree root) PID_DIR="$WORKTREE_PROJECT_ROOT/.task-pids" API_LOG="$PID_DIR/api-${TASK_ID}.log" WORKERS_LOG="$PID_DIR/workers-${TASK_ID}.log" # Wait for log files to be created echo "" echo "⏳ Waiting for services to start and log files to be created..." for i in {1..30}; do if [ -f "$API_LOG" ] && [ -f "$WORKERS_LOG" ]; then echo "✅ Log files found" break fi if [ $i -eq 30 ]; then echo "⚠️ Log files not found after 30 seconds" echo " API log: $API_LOG" echo " Workers log: $WORKERS_LOG" echo " Continuing anyway..." fi sleep 1 done # Show logs with labels echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "📋 Showing API and Workers logs (Press Ctrl+C to stop)" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" # Check if log files exist if [ ! -f "$API_LOG" ] && [ ! -f "$WORKERS_LOG" ]; then echo "❌ No log files found" echo " API log: $API_LOG" echo " Workers log: $WORKERS_LOG" echo "💡 Services may still be starting. You can manually view logs with:" echo " tail -f $API_LOG" echo " tail -f $WORKERS_LOG" exit 1 fi # Use multitail if available for better viewing, otherwise use simple tail if command -v multitail >/dev/null 2>&1; then echo "📋 Using multitail for split-screen log viewing..." if [ -f "$API_LOG" ] && [ -f "$WORKERS_LOG" ]; then multitail -s 2 \ -ci green -T "API" "$API_LOG" \ -ci yellow -T "Workers" "$WORKERS_LOG" elif [ -f "$API_LOG" ]; then tail -f "$API_LOG" elif [ -f "$WORKERS_LOG" ]; then tail -f "$WORKERS_LOG" fi else # Simple approach: tail both files with prefixes # Use a subshell to manage background processes ( # Start tailing API logs in background (filter out NuGet build warnings) if [ -f "$API_LOG" ]; then tail -f "$API_LOG" 2>/dev/null | grep -vE "(warning NU|\.csproj : warning)" | while IFS= read -r line; do echo "[API] $line" done & TAIL_API_PID=$! fi # Start tailing Workers logs in background (filter out NuGet build warnings) if [ -f "$WORKERS_LOG" ]; then tail -f "$WORKERS_LOG" 2>/dev/null | grep -vE "(warning NU|\.csproj : warning)" | while IFS= read -r line; do echo "[WORKERS] $line" done & TAIL_WORKERS_PID=$! fi # Clean up background processes on exit cleanup() { [ -n "$TAIL_API_PID" ] && kill "$TAIL_API_PID" 2>/dev/null [ -n "$TAIL_WORKERS_PID" ] && kill "$TAIL_WORKERS_PID" 2>/dev/null exit } trap cleanup INT TERM EXIT # Wait for background processes wait ) fi