-
Notifications
You must be signed in to change notification settings - Fork 1
/
start-and-wait-to-init.sh
executable file
·56 lines (41 loc) · 1.21 KB
/
start-and-wait-to-init.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
#!/bin/sh
#
# We trigger an init endpoint to set up all async processes like cron scheduling
# because unfortunately Next.js does not provide a proper way to use a startup callback
#
echo "Start the server in background"
node server.js &
# Store the server process ID
next_pid=$!
stop_server() {
echo "Shut down Next.js server..."
kill $next_pid
exit 0
}
# Bind the callback to the SIGINT signal to shut down the background process properly
trap stop_server SIGINT
check_server_and_init() {
timeout=15
counter=0
while true; do
response=$(curl --write-out %{http_code} --silent --output /dev/null http://$APP_HOST:$PORT)
if [ "$response" = "200" ]; then
break
fi
if [ $counter -eq $timeout ]; then
echo "Error: the Next.js server is not ready within the expected timeframe"
# Kill the server since it has no reason to continue
kill $next_pid
exit 1
fi
echo "the Next.js server is not yet ready"
sleep 1
counter=$((counter+1))
done
curl http://$APP_HOST:$PORT/api/init
}
# In parallel wait for the server readiness to init some services
check_server_and_init
# Wait for the Next.js server to return
wait
echo "The wait instruction has exited normally"