-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait-for-rabbitmq.sh
executable file
·46 lines (36 loc) · 1.09 KB
/
wait-for-rabbitmq.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
#!/bin/bash
# wait-for-rabbitmq.sh
set -e
host="rabbitmq"
shift
cmd=("$@") # Store command and arguments as an array
# RabbitMQ credentials
RABBITMQ_USER="guest"
RABBITMQ_PASS="guest"
# Function to check RabbitMQ status
check_rabbitmq() {
local status_code
echo "Checking RabbitMQ status at http://${host}:15672/api/healthchecks/node"
status_code=$(curl -u "${RABBITMQ_USER}:${RABBITMQ_PASS}" -s -o /dev/null -w "%{http_code}" "http://${host}:15672/api/healthchecks/node")
if [ "$status_code" -eq 000 ]; then
echo "Failed to connect to RabbitMQ. Retrying..."
return 1
fi
echo "Received status code: $status_code"
[ "$status_code" -eq 200 ]
}
# Maximum number of retries
max_retries=5
retry_count=0
# Wait until RabbitMQ is available
until check_rabbitmq; do
if [ "$retry_count" -ge "$max_retries" ]; then
>&2 echo "RabbitMQ is still unavailable after $((retry_count * 10)) seconds - exiting"
exit 1
fi
>&2 echo "RabbitMQ is unavailable - sleeping"
sleep 10
retry_count=$((retry_count + 1))
done
>&2 echo "RabbitMQ is up - executing command"
exec "${cmd[@]}"