Skip to content

Commit 9a5d28a

Browse files
committed
Set a maximum number of Elasticsearch checks
1 parent 1322b65 commit 9a5d28a

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

Diff for: .circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- run:
4848
command: |
4949
pushd metacpan-docker/src/metacpan-api
50-
./wait-for-es.sh elasticsearch_test http://localhost:9200
50+
./wait-for-es.sh http://localhost:9200 elasticsearch_test --
5151
name: wait for ES
5252
- run:
5353
command: |

Diff for: wait-for-es.sh

+23-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55

66
set -ux
77

8-
container="$1"
9-
host="$2"
108

11-
preamble="docker-compose exec $container"
9+
HOST="$1"
10+
CONTAINER=${2:-""}
11+
PREAMBLE=""
12+
13+
echo "container |$CONTAINER|"
14+
if [[ $CONTAINER != "" ]]; then
15+
PREAMBLE="docker-compose exec $CONTAINER"
16+
fi
1217

1318
while true; do
14-
response=$($preamble curl --write-out '%{http_code}' --silent --fail --output /dev/null "$host")
19+
response=$($PREAMBLE curl --write-out '%{http_code}' --silent --fail --output /dev/null "$HOST")
1520
if [[ "$response" -eq "200" ]]; then
1621
break
1722
fi
@@ -24,16 +29,27 @@ done
2429
# if the server was not available
2530
set -e
2631

32+
COUNTER=0
33+
MAX_LOOPS=60
2734
while true; do
2835
## Wait for ES status to turn to yellow.
2936
## TODO: Ideally we'd be waiting for green, but we need multiple nodes for that.
30-
health=$($preamble curl -fsSL "$host/_cat/health?format=JSON" | jq '.[0].status == "yellow" or .[0].status == "green"')
37+
health=$($PREAMBLE curl -fsSL "$HOST/_cat/health?format=JSON" | jq '.[0].status == "yellow" or .[0].status == "green"')
3138
if [[ $health == 'true' ]]; then
39+
echo "Elasticsearch is up" >&2
3240
break
3341
fi
3442
echo "Elastic Search is unavailable ($health) - sleeping" >&2
43+
COUNTER=$((COUNTER + 1))
44+
if [[ $COUNTER -gt $MAX_LOOPS ]]; then
45+
echo "Giving up after $COUNTER attempts"
46+
exit 1
47+
break
48+
fi
3549
sleep 1
3650
done
3751

38-
echo "Elastic Search is up" >&2
39-
exit 0
52+
# Allow commands to be chained
53+
shift
54+
shift
55+
exec "$@"

0 commit comments

Comments
 (0)