-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.sh
120 lines (110 loc) · 2.82 KB
/
startup.sh
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
#!/bin/bash
set -euo pipefail
# Environment setup
source .env
# Utility functions
log_info() {
echo "$(date +%Y-%m-%d_%H:%M:%S) - INFO: $*"
}
log_error() {
echo "$(date +%Y-%m-%d_%H:%M:%S) - ERROR: $*" >&2
}
cleanup() {
log_info "Cleaning up..."
# Remove PID files
rm -f /tmp/mvp_backend.pid /tmp/mvp_database.pid
# Stop services
# ... (Implement specific stop commands based on technology stack)
}
check_dependencies() {
log_info "Checking for required dependencies..."
# ... (Implement dependency checks based on project needs)
# Example:
# if ! command -v python3 >/dev/null 2>&1; then
# log_error "Python 3 is required. Please install it."
# exit 1
# fi
}
# Health checks
check_port() {
local port="$1"
if nc -z 127.0.0.1 "$port" >/dev/null 2>&1; then
log_info "Port $port is available."
return 0
else
log_error "Port $port is not available. Is another service running on it?"
return 1
fi
}
wait_for_service() {
local port="$1"
local timeout="$2"
local start=$(date +%s)
while true; do
if check_port "$port"; then
log_info "Service is ready on port $port."
return 0
fi
if (( $(date +%s) - $start >= $timeout )); then
log_error "Timeout waiting for service on port $port."
return 1
fi
sleep 1
done
}
verify_service() {
local port="$1"
# ... (Implement service health check logic based on technology stack)
# Example:
# curl -s http://localhost:"$port"/health | grep -q "OK"
}
# Service management
start_database() {
log_info "Starting database service..."
# ... (Implement database startup command based on technology stack)
# Example:
# pg_ctl start -D /var/lib/postgresql/data
store_pid /tmp/mvp_database.pid
wait_for_service "$DATABASE_PORT" 120
if ! verify_service "$DATABASE_PORT"; then
log_error "Database health check failed."
exit 1
fi
}
start_backend() {
log_info "Starting backend service..."
# ... (Implement backend startup command based on technology stack)
# Example:
# uvicorn main:app --host 0.0.0.0 --port 8000
store_pid /tmp/mvp_backend.pid
wait_for_service "$BACKEND_PORT" 120
if ! verify_service "$BACKEND_PORT"; then
log_error "Backend health check failed."
exit 1
fi
}
start_frontend() {
log_info "Starting frontend service..."
# ... (Implement frontend startup command based on technology stack)
# Example:
# npm start
# ... (or yarn start)
}
store_pid() {
local pid_file="$1"
local pid=$(pgrep -f "$2")
if [[ -z "$pid" ]]; then
log_error "Failed to get PID for $2."
exit 1
fi
echo "$pid" > "$pid_file"
}
# Main execution flow
trap cleanup EXIT ERR
log_info "Starting MVP services..."
check_dependencies
# Start services in order of dependency
start_database
start_backend
# ... (Start other services if needed)
log_info "All services started successfully!"