File size: 1,911 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
#!/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)
    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 restart backend services
restart_backend() {
    local force=false
    
    # Parse arguments
    while [[ "$#" -gt 0 ]]; do
        case $1 in
            --force) force=true ;;
            *) log_error "Unknown parameter: $1"; return 1 ;;
        esac
        shift
    done
    
    log_step "Restarting backend services..."
    
    # Load environment variables
    if ! load_env; then
        return 1
    fi
    
    # If --force parameter is used, clear data folder
    if [ "$force" = true ]; then
        log_warning "Force restart mode: This will clear the data folder, all data will be deleted!"
        read -p "Are you sure you want to continue? (y/n) " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            log_info "Clearing data folder..."
            rm -rf "${LOCAL_BASE_DIR}/data"
            log_success "Data folder cleared, database will be reinitialized"
        else
            log_info "Operation cancelled"
            return 0
        fi
    fi
    
    # 1. Stop backend service
    BACKEND_PID=$(lsof -ti:8002)
    if [ ! -z "$BACKEND_PID" ]; then
        kill $BACKEND_PID
        log_success "Backend service stopped"
    fi
    rm -f run/.backend.pid backend.log
    
    # 2. Wait for port release
    sleep 2
    
    # 3. Start backend service
    ./scripts/start.sh --backend-only
}

# Execute restart service
restart_backend "$@"