-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.sh
123 lines (104 loc) · 2.61 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
121
122
123
#!/bin/bash
# Strict error handling
set -euo pipefail
# Load environment variables
if [ -f .env ]; then
source .env
fi
# Validate required environment variables
if [ -z "$DATABASE_URL" ]; then
echo "Error: DATABASE_URL is not set" >&2
exit 1
fi
# Set default values for optional variables
REDIS_URL=${REDIS_URL:-"redis://localhost:6379"}
NODE_ENV=${NODE_ENV:-"development"}
# Define script variables
PROJECT_ROOT=$(pwd)
LOG_FILE="$PROJECT_ROOT/logs/startup.log"
PID_FILE="$PROJECT_ROOT/logs/pids.txt"
DATABASE_TIMEOUT=30
BACKEND_TIMEOUT=30
FRONTEND_TIMEOUT=30
HEALTH_CHECK_INTERVAL=5
# Logging functions
log_info() {
echo "$(date +"%Y-%m-%d %H:%M:%S") INFO: $@" >> "$LOG_FILE"
}
log_error() {
echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR: $@" >&2 >> "$LOG_FILE"
}
# Cleanup function
cleanup() {
log_info "Cleaning up..."
rm -f "$PID_FILE"
# ... (Stop services if needed)
}
# Dependency checks
check_dependencies() {
log_info "Checking dependencies..."
# ... (Check for required tools, like npm, docker, etc.)
}
# Health check functions
check_port() {
local port=$1
nc -z localhost "$port" > /dev/null 2>&1
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
wait_for_service() {
local service_name=$1
local port=$2
local timeout=$3
local attempts=0
log_info "Waiting for $service_name on port $port..."
while [ "$attempts" -lt "$timeout" ]; do
if check_port "$port"; then
log_info "$service_name is available."
return 0
fi
sleep "$HEALTH_CHECK_INTERVAL"
attempts=$((attempts + 1))
done
log_error "Timeout waiting for $service_name."
exit 1
}
verify_service() {
local service_name=$1
local url=$2
log_info "Verifying $service_name at $url..."
# ... (Implement service health check logic)
}
# Service management functions
start_database() {
log_info "Starting PostgreSQL database..."
docker run -d --name fitness-tracker-db -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=database_name -p 5432:5432 postgres:latest
wait_for_service "PostgreSQL" 5432 "$DATABASE_TIMEOUT"
}
start_backend() {
log_info "Starting backend server..."
npm start &
wait_for_service "Backend" 3000 "$BACKEND_TIMEOUT"
}
start_frontend() {
log_info "Starting frontend service..."
npm run dev &
wait_for_service "Frontend" 3000 "$FRONTEND_TIMEOUT"
}
store_pid() {
log_info "Storing process IDs..."
# ... (Save process IDs to PID_FILE)
}
# Trap signals
trap cleanup EXIT ERR
# Main execution flow
check_dependencies
start_database
start_backend
# ... (Start other services, if needed)
start_frontend
store_pid
log_info "Fitness Tracker MVP startup complete."