File size: 6,581 Bytes
01d5a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash

# Source the logging utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/utils/logging.sh"

# Load configuration from .env file
load_env() {
    if [ -f .env ]; then
        # Only load necessary environment variables
        export LOCAL_APP_PORT=$(grep '^LOCAL_APP_PORT=' .env | cut -d '=' -f2)
        export LOCAL_FRONTEND_PORT=$(grep '^LOCAL_FRONTEND_PORT=' .env | cut -d '=' -f2)
    else
        # Use default port if .env not found
        export LOCAL_APP_PORT=8002
        log_info "Using default port: ${LOCAL_APP_PORT}"
    fi
}

# Main function to stop services
stop_services() {
    log_step "Stopping services..."
    
    # Load environment variables
    load_env
    
    # Create run directory if it doesn't exist
    mkdir -p run
    
    # Stop backend service
    if [ -f run/.backend.pid ]; then
        BACKEND_PID=$(cat run/.backend.pid)
        if ps -p $BACKEND_PID > /dev/null; then
            kill $BACKEND_PID
            log_success "Backend service stopped (PID: $BACKEND_PID)"
        else
            log_info "Backend process not running, checking port ${LOCAL_APP_PORT}..."
            PORT_PID=$(lsof -ti:${LOCAL_APP_PORT} 2>/dev/null)
            if [ ! -z "$PORT_PID" ]; then
                kill -9 $PORT_PID
                log_success "Backend service forcefully stopped (Port PID: $PORT_PID)"
            else
                log_info "No backend service found running on port ${LOCAL_APP_PORT}"
            fi
        fi
        rm -f run/.backend.pid
    else
        log_info "No backend PID file found, checking port ${LOCAL_APP_PORT}..."
        PORT_PID=$(lsof -ti:${LOCAL_APP_PORT} 2>/dev/null)
        if [ ! -z "$PORT_PID" ]; then
            kill -9 $PORT_PID
            log_success "Backend service forcefully stopped (Port PID: $PORT_PID)"
        else
            log_info "No backend service found running on port ${LOCAL_APP_PORT}"
        fi
    fi
    
    # Double-check if port 8002 is still in use
    PORT_PID=$(lsof -ti:${LOCAL_APP_PORT} 2>/dev/null)
    if [ ! -z "$PORT_PID" ]; then
        log_warning "Port ${LOCAL_APP_PORT} is still in use, forcefully terminating process..."
        kill -9 $PORT_PID
        sleep 0.5
        
        # Check again
        PORT_PID=$(lsof -ti:${LOCAL_APP_PORT} 2>/dev/null)
        if [ ! -z "$PORT_PID" ]; then
            log_error "Failed to release port ${LOCAL_APP_PORT}, process (PID: $PORT_PID) is still running"
        else
            log_success "Successfully released port ${LOCAL_APP_PORT}"
        fi
    fi
    
    # Stop llama-server process
    log_info "Checking for llama-server processes..."
    LLAMA_PIDS=$(pgrep -f "llama-server")
    if [ ! -z "$LLAMA_PIDS" ]; then
        echo "$LLAMA_PIDS" | while read pid; do
            log_info "Stopping llama-server process (PID: $pid)..."
            kill $pid 2>/dev/null
            sleep 0.5
            # Check if process is still running
            if ps -p $pid > /dev/null 2>&1; then
                log_warning "Process still running, using force kill..."
                kill -9 $pid 2>/dev/null
            fi
        done
        log_success "llama-server processes stopped"
    else
        log_info "No llama-server processes found"
    fi
    
    # Check if port 8080 is still in use (common port for llama-server)
    log_info "Checking if port 8080 is in use..."
    PORT_PID=$(lsof -ti:8080 2>/dev/null)
    if [ ! -z "$PORT_PID" ]; then
        log_info "Stopping process using port 8080 (PID: $PORT_PID)..."
        kill -9 $PORT_PID
        log_success "Process using port 8080 forcefully terminated"
    else
        log_info "Port 8080 is not in use"
    fi
    
    # Stop frontend service
    if [ -f run/.frontend.pid ]; then
        FRONTEND_PID=$(cat run/.frontend.pid)
        if ps -p $FRONTEND_PID > /dev/null; then
            log_info "Stopping frontend process (PID: $FRONTEND_PID)..."
            # use pkill to terminate process tree
            pkill -P $FRONTEND_PID 2>/dev/null
            kill $FRONTEND_PID 2>/dev/null
            sleep 1
            
            # check if process is still running
            if ps -p $FRONTEND_PID > /dev/null; then
                log_warning "Frontend process still running, using force kill..."
                kill -9 $FRONTEND_PID 2>/dev/null
            fi
            
            log_success "Frontend process stopped (PID: $FRONTEND_PID)"
        else
            log_info "Frontend process not running"
        fi
        rm -f run/.frontend.pid
    fi
    
    # check and terminate all possible frontend related processes
    log_info "Checking for any remaining Next.js processes..."
    
    # find and terminate all Next.js related processes
    NEXT_PIDS=$(pgrep -f "next dev|next-server")
    if [ ! -z "$NEXT_PIDS" ]; then
        echo "$NEXT_PIDS" | while read pid; do
            log_info "Stopping Next.js process (PID: $pid)..."
            kill $pid 2>/dev/null
            sleep 0.5
            # check if process is still running
            if ps -p $pid > /dev/null 2>&1; then
                log_warning "Process still running, using force kill..."
                kill -9 $pid 2>/dev/null
            fi
        done
        log_success "Next.js processes stopped"
    fi
    
    # find and terminate all frontend related npm processes
    NPM_PIDS=$(pgrep -f "npm run dev")
    if [ ! -z "$NPM_PIDS" ]; then
        echo "$NPM_PIDS" | while read pid; do
            log_info "Stopping npm process (PID: $pid)..."
            kill $pid 2>/dev/null
            sleep 0.5
            # check if process is still running
            if ps -p $pid > /dev/null 2>&1; then
                log_warning "Process still running, using force kill..."
                kill -9 $pid 2>/dev/null
            fi
        done
        log_success "npm processes stopped"
    fi
    
    # check if frontend port is still in use
    if [ ! -z "${LOCAL_FRONTEND_PORT}" ]; then
        log_info "Checking if port ${LOCAL_FRONTEND_PORT} is still in use..."
        PORT_PID=$(lsof -ti:${LOCAL_FRONTEND_PORT} 2>/dev/null)
        if [ ! -z "$PORT_PID" ]; then
            log_info "Stopping process using port ${LOCAL_FRONTEND_PORT} (PID: $PORT_PID)..."
            kill -9 $PORT_PID
            log_success "Process using port ${LOCAL_FRONTEND_PORT} forcefully terminated"
        else
            log_info "Port ${LOCAL_FRONTEND_PORT} is not in use"
        fi
    fi
    
    log_success "All services stopped successfully"
}

# Execute stop services
stop_services