-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.sh
63 lines (53 loc) · 1.3 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
#!/bin/bash
set -euo pipefail
export DATABASE_URL="postgresql://user:password@host:port/database"
start_database() {
echo "Starting database..."
pg_ctl start -D /var/lib/postgresql/data -l /var/log/postgresql/postgresql.log
wait_for_service postgresql 5432
store_pid "database"
}
start_backend() {
echo "Starting backend..."
uvicorn main:app --host 0.0.0.0 --port 8000
wait_for_service backend 8000
store_pid "backend"
}
store_pid() {
local service_name=$1
local pid=$(pgrep -f "$service_name")
if [[ -n "$pid" ]]; then
echo "$pid" > "/var/run/$service_name.pid"
fi
}
wait_for_service() {
local service_name=$1
local port=$2
local timeout=30
local attempts=0
until nc -z "$HOST" "$port" 2>/dev/null; do
((attempts++))
if [[ $attempts -gt $timeout ]]; then
echo "Error: Timeout waiting for $service_name on port $port"
exit 1
fi
sleep 1
done
}
cleanup() {
echo "Stopping services and cleaning up..."
for pid_file in /var/run/*.pid; do
service_name=$(basename "$pid_file" .pid)
if [[ -f "$pid_file" ]]; then
local pid=$(cat "$pid_file")
if [[ -n "$pid" ]]; then
kill "$pid" 2>/dev/null
fi
rm "$pid_file"
fi
done
}
trap cleanup EXIT ERR
start_database
start_backend
echo "Application started successfully"