-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.sh
102 lines (85 loc) · 2.13 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
#!/bin/bash
set -euo pipefail
# Environment setup
PROJECT_ROOT=$(dirname "$0")
LOG_FILE="$PROJECT_ROOT/logs/startup.log"
PID_FILE="$PROJECT_ROOT/logs/startup.pid"
DATABASE_TIMEOUT=30
HEALTHCHECK_INTERVAL=5
# Function definitions
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
}
cleanup() {
log_info "Cleaning up process..."
if [ -f "$PID_FILE" ]; then
kill -9 $(cat "$PID_FILE") 2>/dev/null
rm "$PID_FILE"
fi
}
check_dependencies() {
log_info "Checking dependencies..."
command -v docker >/dev/null 2>&1 || {
log_error "Docker is required. Please install Docker."
exit 1
}
command -v docker-compose >/dev/null 2>&1 || {
log_error "Docker Compose is required. Please install Docker Compose."
exit 1
}
}
check_port() {
local port="$1"
nc -z localhost "$port" >/dev/null 2>&1 && return 0 || return 1
}
wait_for_service() {
local service="$1"
local port="$2"
local timeout="$3"
local counter=0
log_info "Waiting for $service to start on port $port..."
while [ "$counter" -lt "$timeout" ]; do
if check_port "$port"; then
log_info "$service is running."
return 0
fi
sleep "$HEALTHCHECK_INTERVAL"
counter=$((counter + 1))
done
log_error "$service failed to start on port $port."
exit 1
}
verify_service() {
local service="$1"
local port="$2"
local timeout="$3"
log_info "Verifying $service on port $port..."
wait_for_service "$service" "$port" "$timeout"
log_info "$service is healthy."
}
start_database() {
log_info "Starting PostgreSQL database..."
docker-compose up -d database
wait_for_service "PostgreSQL" 5432 "$DATABASE_TIMEOUT"
}
start_backend() {
log_info "Starting backend server..."
docker-compose up -d backend
wait_for_service "Backend" 8000 "$DATABASE_TIMEOUT"
}
store_pid() {
local pid="$1"
local file="$2"
echo "$pid" > "$file"
}
# Main script execution
trap cleanup EXIT ERR
check_dependencies
source "$PROJECT_ROOT/.env"
log_info "Environment variables loaded."
start_database
start_backend
log_info "Services started successfully."