From 27e284702be932b598f38e635e905993857fa9e1 Mon Sep 17 00:00:00 2001 From: Andrei Date: Mon, 6 Oct 2025 22:55:34 +0000 Subject: [PATCH] feat: Add development server management scripts - Create start-dev.sh to start all services on 0.0.0.0 for external access - Create stop-dev.sh to gracefully stop all dev servers - Aggressive port cleanup to ensure clean startup - Backend on 0.0.0.0:3020 (maternal-api.noru1.ro) - Frontend on 0.0.0.0:3030 (maternal.noru1.ro) - Admin on 0.0.0.0:3335 (pfadmin.noru1.ro) - PID tracking and log files in /tmp - Multiple kill methods to ensure ports are freed --- start-dev.sh | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++ stop-dev.sh | 172 +++++++++++++++++++++++++++++++++++ 2 files changed, 423 insertions(+) create mode 100755 start-dev.sh create mode 100755 stop-dev.sh diff --git a/start-dev.sh b/start-dev.sh new file mode 100755 index 0000000..65d6989 --- /dev/null +++ b/start-dev.sh @@ -0,0 +1,251 @@ +#!/bin/bash + +# ParentFlow Development Servers Start Script +# Starts all development servers on 0.0.0.0 for external access + +set -e + +# Color codes for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +CYAN='\033[0;36m' +NC='\033[0m' # No Color + +# Logging functions +log() { + echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" +} + +error() { + echo -e "${RED}[ERROR]${NC} $1" >&2 + exit 1 +} + +success() { + echo -e "${GREEN}✓${NC} $1" +} + +warning() { + echo -e "${YELLOW}⚠${NC} $1" +} + +# Header +echo "" +echo "============================================" +echo " ParentFlow Development Servers v1.0 " +echo "============================================" +echo "" + +# Function to kill processes on a port - more aggressive +kill_port() { + local PORT=$1 + local SERVICE=$2 + + log "Forcefully clearing port $PORT ($SERVICE)..." + + # Method 1: Using lsof + if command -v lsof &> /dev/null; then + local PIDS=$(lsof -t -i:$PORT 2>/dev/null || true) + if [ -n "$PIDS" ]; then + warning "Found PIDs on port $PORT: $PIDS" + echo $PIDS | xargs -r kill -9 2>/dev/null || true + sleep 1 + fi + fi + + # Method 2: Using fuser as fallback + fuser -k $PORT/tcp 2>/dev/null || true + sleep 1 + + # Method 3: Kill any process with the port in its command + pkill -f ":$PORT" 2>/dev/null || true + pkill -f "PORT=$PORT" 2>/dev/null || true + sleep 1 + + # Method 4: Using netstat/ss to find and kill + if command -v ss &> /dev/null; then + local PIDS=$(ss -tulpn 2>/dev/null | grep ":$PORT " | grep -oP 'pid=\K[0-9]+' || true) + if [ -n "$PIDS" ]; then + echo $PIDS | xargs -r kill -9 2>/dev/null || true + sleep 1 + fi + fi + + # Final aggressive cleanup - kill anything that might be related + pkill -f "npm.*dev.*$PORT" 2>/dev/null || true + pkill -f "node.*$PORT" 2>/dev/null || true + pkill -f "next.*$PORT" 2>/dev/null || true + sleep 1 + + # Verify the port is free + if lsof -i:$PORT > /dev/null 2>&1; then + error "Failed to clear port $PORT after multiple attempts. Please run: sudo lsof -i:$PORT and kill -9 " + else + success "Port $PORT cleared and ready" + fi +} + +# Step 0: Kill ALL Node.js dev processes first +log "${CYAN}Step 0: Cleaning up all development processes...${NC}" + +# Kill all existing dev servers +pkill -f "npm run dev" 2>/dev/null || true +pkill -f "npm run start:dev" 2>/dev/null || true +pkill -f "next dev" 2>/dev/null || true +pkill -f "node.*dist/main" 2>/dev/null || true +sleep 2 + +# Now clear the specific ports +log "${CYAN}Clearing specific ports...${NC}" +kill_port 3020 "Backend API" +kill_port 3030 "Frontend" +kill_port 3335 "Admin Dashboard" + +# Verify Node.js is installed +if ! command -v node &> /dev/null; then + error "Node.js is not installed. Please install Node.js first." +fi + +# Verify npm is installed +if ! command -v npm &> /dev/null; then + error "npm is not installed. Please install npm first." +fi + +# Step 1: Start Backend Development Server +log "${CYAN}Step 1: Starting Backend API on 0.0.0.0:3020...${NC}" +cd /root/maternal-app/maternal-app/maternal-app-backend + +# Check if dependencies are installed +if [ ! -d "node_modules" ]; then + warning "Backend dependencies not found, installing..." + npm install +fi + +# Start backend in background +HOST=0.0.0.0 PORT=3020 API_PORT=3020 npm run start:dev > /tmp/backend-dev.log 2>&1 & +BACKEND_PID=$! +echo $BACKEND_PID > /tmp/backend-dev.pid +success "Backend API started (PID: $BACKEND_PID)" +log "Backend accessible at: http://maternal-api.noru1.ro (0.0.0.0:3020)" + +# Step 2: Start Frontend Development Server +log "${CYAN}Step 2: Starting Frontend on 0.0.0.0:3030...${NC}" +cd /root/maternal-app/maternal-web + +# Check if dependencies are installed +if [ ! -d "node_modules" ]; then + warning "Frontend dependencies not found, installing..." + npm install +fi + +# Create .env.local if it doesn't exist +if [ ! -f ".env.local" ]; then + cat > .env.local << EOF +NEXT_PUBLIC_API_URL=http://maternal-api.noru1.ro/api/v1 +NEXT_PUBLIC_GRAPHQL_URL=http://maternal-api.noru1.ro/graphql +NEXT_PUBLIC_WS_URL=ws://maternal-api.noru1.ro +NEXT_PUBLIC_APP_URL=http://maternal.noru1.ro +NEXT_PUBLIC_APP_NAME=ParentFlow +EOF + log "Created .env.local for frontend" +fi + +# Start frontend in background +HOST=0.0.0.0 PORT=3030 npm run dev > /tmp/frontend-dev.log 2>&1 & +FRONTEND_PID=$! +echo $FRONTEND_PID > /tmp/frontend-dev.pid +success "Frontend started (PID: $FRONTEND_PID)" +log "Frontend accessible at: http://maternal.noru1.ro (0.0.0.0:3030)" + +# Step 3: Start Admin Dashboard Development Server +log "${CYAN}Step 3: Starting Admin Dashboard on 0.0.0.0:3335...${NC}" +cd /root/maternal-app/parentflow-admin + +# Check if dependencies are installed +if [ ! -d "node_modules" ]; then + warning "Admin dependencies not found, installing..." + npm install +fi + +# Create .env.local if it doesn't exist +if [ ! -f ".env.local" ]; then + cat > .env.local << EOF +NEXT_PUBLIC_API_URL=http://maternal-api.noru1.ro/api/v1 +NEXT_PUBLIC_APP_URL=http://pfadmin.noru1.ro +NEXT_PUBLIC_APP_NAME=ParentFlow Admin +EOF + log "Created .env.local for admin dashboard" +fi + +# Start admin dashboard in background +HOST=0.0.0.0 PORT=3335 npm run dev > /tmp/admin-dev.log 2>&1 & +ADMIN_PID=$! +echo $ADMIN_PID > /tmp/admin-dev.pid +success "Admin Dashboard started (PID: $ADMIN_PID)" +log "Admin accessible at: http://pfadmin.noru1.ro (0.0.0.0:3335)" + +# Step 4: Verify all services are running +log "${CYAN}Step 4: Verifying services...${NC}" +sleep 5 + +verify_service() { + local SERVICE=$1 + local PORT=$2 + local PID_FILE=$3 + + if [ -f "$PID_FILE" ]; then + PID=$(cat $PID_FILE) + if kill -0 $PID 2>/dev/null; then + success "$SERVICE is running (PID: $PID)" + return 0 + else + warning "$SERVICE process died" + return 1 + fi + else + warning "$SERVICE PID file not found" + return 1 + fi +} + +ALL_GOOD=true +verify_service "Backend API" 3020 /tmp/backend-dev.pid || ALL_GOOD=false +verify_service "Frontend" 3030 /tmp/frontend-dev.pid || ALL_GOOD=false +verify_service "Admin Dashboard" 3335 /tmp/admin-dev.pid || ALL_GOOD=false + +# Final summary +echo "" +echo "============================================" +if [ "$ALL_GOOD" = true ]; then + echo -e "${GREEN} All Development Servers Started! ${NC}" +else + echo -e "${YELLOW} Some Services Failed to Start ${NC}" +fi +echo "============================================" +echo "" +echo "Access URLs:" +echo " Backend API: http://maternal-api.noru1.ro (0.0.0.0:3020)" +echo " Frontend: http://maternal.noru1.ro (0.0.0.0:3030)" +echo " Admin Dashboard: http://pfadmin.noru1.ro (0.0.0.0:3335)" +echo "" +echo "Log files:" +echo " Backend: tail -f /tmp/backend-dev.log" +echo " Frontend: tail -f /tmp/frontend-dev.log" +echo " Admin: tail -f /tmp/admin-dev.log" +echo "" +echo "To stop all servers: ./stop-dev.sh" +echo "" +echo "PID files stored in:" +echo " /tmp/backend-dev.pid" +echo " /tmp/frontend-dev.pid" +echo " /tmp/admin-dev.pid" +echo "" + +if [ "$ALL_GOOD" = false ]; then + warning "Check log files for errors" + exit 1 +fi + +log "Development servers started successfully!" \ No newline at end of file diff --git a/stop-dev.sh b/stop-dev.sh new file mode 100755 index 0000000..c4fcb65 --- /dev/null +++ b/stop-dev.sh @@ -0,0 +1,172 @@ +#!/bin/bash + +# ParentFlow Development Servers Stop Script +# Stops all development servers and cleans up + +set -e + +# Color codes for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +CYAN='\033[0;36m' +NC='\033[0m' # No Color + +# Logging functions +log() { + echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" +} + +error() { + echo -e "${RED}[ERROR]${NC} $1" >&2 +} + +success() { + echo -e "${GREEN}✓${NC} $1" +} + +warning() { + echo -e "${YELLOW}⚠${NC} $1" +} + +# Header +echo "" +echo "============================================" +echo " Stopping ParentFlow Development Servers " +echo "============================================" +echo "" + +# Function to stop a service by PID file +stop_service() { + local SERVICE=$1 + local PID_FILE=$2 + local PORT=$3 + + log "Stopping $SERVICE..." + + # Try to stop using PID file + if [ -f "$PID_FILE" ]; then + PID=$(cat $PID_FILE) + if kill -0 $PID 2>/dev/null; then + log "Stopping process PID: $PID" + kill -TERM $PID 2>/dev/null || true + sleep 2 + # Force kill if still running + if kill -0 $PID 2>/dev/null; then + warning "Process didn't stop gracefully, force killing..." + kill -9 $PID 2>/dev/null || true + fi + rm -f $PID_FILE + success "$SERVICE stopped" + else + warning "$SERVICE process (PID: $PID) was not running" + rm -f $PID_FILE + fi + else + warning "PID file not found for $SERVICE" + fi + + # Double-check and kill any remaining processes on the port + if lsof -i:$PORT > /dev/null 2>&1; then + warning "Found process still using port $PORT, killing..." + lsof -t -i:$PORT | xargs -r kill -9 2>/dev/null || true + success "Port $PORT cleared" + fi +} + +# Function to kill all Node.js processes matching a pattern +kill_node_pattern() { + local PATTERN=$1 + local SERVICE=$2 + + PIDS=$(ps aux | grep -E "$PATTERN" | grep -v grep | awk '{print $2}' || true) + if [ -n "$PIDS" ]; then + warning "Found $SERVICE processes: $PIDS" + echo $PIDS | xargs -r kill -9 2>/dev/null || true + success "$SERVICE processes killed" + fi +} + +# Step 1: Stop Backend API +log "${CYAN}Step 1: Stopping Backend API...${NC}" +stop_service "Backend API" "/tmp/backend-dev.pid" 3020 +kill_node_pattern "npm.*start:dev.*3020" "Backend" +kill_node_pattern "node.*dist/main.*3020" "Backend" + +# Step 2: Stop Frontend +log "${CYAN}Step 2: Stopping Frontend...${NC}" +stop_service "Frontend" "/tmp/frontend-dev.pid" 3030 +kill_node_pattern "npm.*dev.*3030" "Frontend" +kill_node_pattern "next.*dev.*3030" "Frontend" + +# Step 3: Stop Admin Dashboard +log "${CYAN}Step 3: Stopping Admin Dashboard...${NC}" +stop_service "Admin Dashboard" "/tmp/admin-dev.pid" 3335 +kill_node_pattern "npm.*dev.*3335" "Admin" +kill_node_pattern "next.*dev.*3335" "Admin" + +# Step 4: Clean up log files +log "${CYAN}Step 4: Cleaning up...${NC}" +if [ -f "/tmp/backend-dev.log" ]; then + rm -f /tmp/backend-dev.log + log "Removed backend log file" +fi + +if [ -f "/tmp/frontend-dev.log" ]; then + rm -f /tmp/frontend-dev.log + log "Removed frontend log file" +fi + +if [ -f "/tmp/admin-dev.log" ]; then + rm -f /tmp/admin-dev.log + log "Removed admin log file" +fi + +# Step 5: Final port check +log "${CYAN}Step 5: Verifying ports are free...${NC}" + +check_port() { + local PORT=$1 + local SERVICE=$2 + + if lsof -i:$PORT > /dev/null 2>&1; then + error "Port $PORT is still in use ($SERVICE)" + return 1 + else + success "Port $PORT is free ($SERVICE)" + return 0 + fi +} + +ALL_CLEAR=true +check_port 3020 "Backend API" || ALL_CLEAR=false +check_port 3030 "Frontend" || ALL_CLEAR=false +check_port 3335 "Admin Dashboard" || ALL_CLEAR=false + +# Kill any remaining Next.js or npm dev processes +log "${CYAN}Step 6: Cleaning up any remaining dev processes...${NC}" +pkill -f "next dev" 2>/dev/null || true +pkill -f "npm run dev" 2>/dev/null || true +pkill -f "npm run start:dev" 2>/dev/null || true + +# Final summary +echo "" +echo "============================================" +if [ "$ALL_CLEAR" = true ]; then + echo -e "${GREEN} All Development Servers Stopped! ${NC}" +else + echo -e "${YELLOW} Some Ports Still In Use ${NC}" + echo "" + echo "Manual cleanup commands:" + echo " lsof -i:3020 | grep LISTEN" + echo " lsof -i:3030 | grep LISTEN" + echo " lsof -i:3335 | grep LISTEN" + echo "" + echo "Force kill all Node processes:" + echo " pkill -9 node" +fi +echo "============================================" +echo "" + +log "Development servers stop script completed!" \ No newline at end of file