forked from jitsucom/jitsu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-start-console.sh
executable file
·121 lines (105 loc) · 3.1 KB
/
docker-start-console.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
#!/bin/bash
cancel_healthcheck="0"
inited="0"
export CONSOLE_INIT_TOKEN=$RANDOM$RANDOM$RANDOM$RANDOM
export my_pid=$$
init() {
if [ "$inited" = "0" ]; then
echo "Initializing console..."
inited="1"
curl --silent --show-error http://localhost:3000/api/admin/events-log-init?token=$CONSOLE_INIT_TOKEN
echo ""
fi
}
wait_for_service() {
url=$1
interval=$2
max_wait=$3
start_time=$(date +%s)
end_time=$((start_time + max_wait))
while true; do
nc -z $(echo $url | tr ':' ' ') >/dev/null 2>&1
if [ $? -eq 0 ]; then
break
fi
current_time=$(date +%s)
if [ $current_time -ge $end_time ]; then
cancel_healthcheck="1"
exit 1
fi
sleep $interval
done
exit 0
}
healthcheck() {
pid=$1
echo "Waiting for localhost:3000 to be up..."
service_down=$(wait_for_service localhost:3000 1 10)
if [ "$service_down" = "1" ]; then
echo "❌ ❌ ❌ HEALTHCHECK FAILED - $healthcheck_url is not UP"
kill -9 $pid
fi
if [ "$cancel_healthcheck" = "0" ]; then
echo "Running healthcheck..."
healthcheck_url="http://localhost:3000/api/healthcheck"
http_code=$(curl -s $healthcheck_url -o healthcheck-result -w '%{http_code}')
if [ "$http_code" = "200" ]; then
echo "⚡️⚡️⚡️ HEALTHCHECK PASSED - $http_code from $healthcheck_url. Details:"
if [ -f healthcheck-result ]; then
cat healthcheck-result
fi
echo ""
init
else
if [ "$http_code" = "000" ]; then
echo "❌ ❌ ❌ HEALTHCHECK FAILED $healthcheck_url is not available"
else
echo "❌ ❌ ❌ HEALTHCHECK FAILED - $http_code from $healthcheck_url. Response:"
if [ -f healthcheck-result ]; then
cat healthcheck-result
fi
echo ""
fi
kill_console
kill -9 $$
exit 1
fi
fi
}
start_console() {
cd /app/webapps/console
HOSTNAME="::" node server.js & echo $! > /app/console.pid
return $?
}
kill_console() {
if [ -f /app/console.pid ]; then
pid=$(cat /app/console.pid)
echo "Killing console with pid $pid"
kill -9 $pid
#rm /app/console.pid
fi
}
main() {
cmd=$1
export SIGNALS_LYFECICLE=1
if [ -z "$cmd" ]; then
if [ "$FORCE_UPDATE_DB" = "1" ] || [ "$FORCE_UPDATE_DB" = "yes" ] || [ "$FORCE_UPDATE_DB" = "true" ]; then
echo "FORCE_UPDATE_DB is set, updating database schema..."
prisma db push --skip-generate --schema schema.prisma --accept-data-loss
elif [ "$UPDATE_DB" = "1" ] || [ "$UPDATE_DB" = "yes" ] || [ "$UPDATE_DB" = "true" ]; then
echo "UPDATE_DB is set, updating database schema..."
prisma db push --skip-generate --schema schema.prisma
fi
echo "Starting the app"
healthcheck $$ &
exit_code=$(start_console)
sleep 1000
cancel_healthcheck="1"
echo "App stopped with exit code ${exit_code}, exiting..."
elif [ "$cmd" = "db-prepare" ]; then
prisma db push --skip-generate --schema schema.prisma
else
echo "ERROR! Unknown command '$cmd'"
fi
}
main "$@"