forked from nzbdav-dev/nzbdav
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
124 lines (103 loc) · 3.05 KB
/
entrypoint.sh
File metadata and controls
124 lines (103 loc) · 3.05 KB
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
124
#!/bin/sh
wait_either() {
local pid1=$1
local pid2=$2
while true; do
if ! kill -0 "$pid1" 2>/dev/null; then
wait "$pid1"
EXITED_PID=$pid1
REMAINING_PID=$pid2
return $?
fi
if ! kill -0 "$pid2" 2>/dev/null; then
wait "$pid2"
EXITED_PID=$pid2
REMAINING_PID=$pid1
return $?
fi
sleep 0.5
done
}
# Signal handling for graceful shutdown
terminate() {
echo "Caught termination signal. Shutting down..."
if [ -n "$BACKEND_PID" ] && kill -0 "$BACKEND_PID" 2>/dev/null; then
kill "$BACKEND_PID"
fi
if [ -n "$FRONTEND_PID" ] && kill -0 "$FRONTEND_PID" 2>/dev/null; then
kill "$FRONTEND_PID"
fi
# Wait for children to exit
wait
exit 0
}
trap terminate TERM INT
# Use env vars or default to 1000
PUID=${PUID:-1000}
PGID=${PGID:-1000}
# Create group if it doesn't exist
if ! getent group appgroup >/dev/null; then
addgroup -g "$PGID" appgroup
fi
# Create user if it doesn't exist
if ! id appuser >/dev/null 2>&1; then
adduser -D -H -u "$PUID" -G appgroup appuser
fi
# Set environment variables
if [ -z "${BACKEND_URL}" ]; then
export BACKEND_URL="http://localhost:8080"
fi
if [ -z "${FRONTEND_BACKEND_API_KEY}" ]; then
export FRONTEND_BACKEND_API_KEY=$(head -c 32 /dev/urandom | hexdump -ve '1/1 "%.2x"')
fi
# Change permissions on /config directory to the given PUID and PGID
chown $PUID:$PGID /config
# Run backend database migration
cd /app/backend
echo "Running database maintenance."
su-exec appuser ./NzbWebDAV --db-migration
if [ $? -ne 0 ]; then
echo "Database migration failed. Exiting with error code $?."
exit $?
fi
echo "Done with database maintenance."
# Run backend as appuser in background
su-exec appuser ./NzbWebDAV &
BACKEND_PID=$!
# Wait for backend health check
echo "Waiting for backend to start."
MAX_BACKEND_HEALTH_RETRIES=${MAX_BACKEND_HEALTH_RETRIES:-30}
MAX_BACKEND_HEALTH_RETRY_DELAY=${MAX_BACKEND_HEALTH_RETRY_DELAY:-1}
i=0
while true; do
echo "Checking backend health: $BACKEND_URL/health ..."
if curl -s -o /dev/null -w "%{http_code}" "$BACKEND_URL/health" | grep -q "^200$"; then
echo "Backend is healthy."
break
fi
i=$((i+1))
if [ "$i" -ge "$MAX_BACKEND_HEALTH_RETRIES" ]; then
echo "Backend failed health check after $MAX_BACKEND_HEALTH_RETRIES retries. Exiting."
kill $BACKEND_PID
wait $BACKEND_PID
exit 1
fi
sleep "$MAX_BACKEND_HEALTH_RETRY_DELAY"
done
# Run frontend as appuser in background
cd /app/frontend
su-exec appuser npm run start &
FRONTEND_PID=$!
# Wait for either to exit
wait_either $BACKEND_PID $FRONTEND_PID
EXIT_CODE=$?
# Determine which process exited
if [ "$EXITED_PID" -eq "$FRONTEND_PID" ]; then
echo "The web-frontend has exited. Shutting down the web-backend..."
else
echo "The web-backend has exited. Shutting down the web-frontend..."
fi
# Kill the remaining process
kill $REMAINING_PID
# Exit with the code of the process that died first
exit $EXIT_CODE