-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait-for-mysql.sh
More file actions
69 lines (56 loc) Β· 1.56 KB
/
wait-for-mysql.sh
File metadata and controls
69 lines (56 loc) Β· 1.56 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
#!/bin/bash
set -e
host="db"
port=3306
user="root"
password="${MYSQL_ROOT_PASSWORD}"
database="${MYSQL_DATABASE}"
max_retries=30
sleep_seconds=2
wait_for_db=${WAIT_FOR_DB}
if [ "$wait_for_db" = "true" ]; then
echo "β³ Waiting for MySQL ($host:$port) to accept connections..."
until nc -z "$host" "$port"; do
echo "β MySQL port not open yet..."
sleep $sleep_seconds
done
echo "β
MySQL port open. Checking full readiness..."
counter=0
until mysql -h"$host" -P"$port" -u"$user" -p"$password" -e "SELECT 1;" "$database" > /dev/null 2>&1; do
counter=$((counter+1))
echo "β MySQL not ready yet... ($counter/$max_retries)"
if [ $counter -ge $max_retries ]; then
echo "π₯ MySQL did not become ready. Exiting."
exit 1
fi
sleep $sleep_seconds
done
echo "β
MySQL fully ready. Waiting 5 seconds..."
sleep 5
else
echo "β‘ Skipping DB wait (WAIT_FOR_DB == false)"
fi
REDIS_HOST="jobda-redis"
REDIS_PORT=6379
echo "β³ Waiting for Redis ($REDIS_HOST:$REDIS_PORT)..."
until nc -z "$REDIS_HOST" "$REDIS_PORT"; do
echo "β Redis port not open yet..."
sleep 2
done
echo "β
Redis port open. Verifying Redis ping..."
until redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" ping | grep -q PONG; do
echo "β Redis not responding yet..."
sleep 2
done
echo "β
Redis fully ready."
echo "π Starting Spring Boot app..."
set +e
java -jar app.jar
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "β οΈ App exited with $exit_code. Retrying once after short delay..."
sleep 5
java -jar app.jar
exit_code=$?
fi
exit $exit_code