forked from pgjdbc/pgjdbc
/
wait_for_pg_isready
executable file
·54 lines (47 loc) · 1.48 KB
/
wait_for_pg_isready
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
#!/usr/bin/env bash
set -euo pipefail
# Helper script to wait for a Postgresql server to startup.
#
# Environment variables to control wait time:
#
# PG_ISREADY_MAX_WAIT_SECONDS - Maximum wait time in seconds (Default 60)
# PG_ISREADY_SLEEP_SECONDS - Sleep time in seconds per iteration (Default 1)
#
# Any command line arguments are passed as-is to pg_isready
#
# To wait for up to 60 seconds:
#
# wait_for_pg_isready
#
# To wait for up to 15 seconds with additional pg_isready options:
#
# PG_ISREADY_MAX_WAIT_SECONDS=15 wait_for_pg_isready -h localhost -p 12345
#
# TO sleep for 5 seconds per iteration:
#
# PG_ISREADY_SLEEP_SECONDS=5 wait_for_pg_isready
err () {
echo "$@" 1>&2
exit 1
}
utc_seconds () {
date -u +%s
}
main () {
local max_wait_seconds="${PG_ISREADY_MAX_WAIT_SECONDS:-60}"
local sleep_seconds="${PG_ISREADY_SLEEP_SECONDS:-1}"
local positive_int_regex='^[1-9][0-9]*$'
[[ "${max_wait_seconds}" =~ ${positive_int_regex} ]] || err "Invalid PG_ISREADY_MAX_WAIT_SECONDS: ${PG_ISREADY_MAX_WAIT_SECONDS}"
[[ "${sleep_seconds}" =~ ${positive_int_regex} ]] || err "Invalid PG_ISREADY_SLEEP_SECONDS: ${PG_ISREADY_SLEEP_SECONDS}"
local started_at=$(utc_seconds)
while :;
do
if pg_isready "$@"; then
exit 0
fi
local elapsed=$(( $(utc_seconds) - ${started_at} ))
[[ "${elapsed}" -le "${max_wait_seconds}" ]] || err "Max wait time exceeded"
sleep "${sleep_seconds}"
done
}
main "$@"