Skip to content

Commit d0ddf28

Browse files
committed
wait for rabbitmq b4 start
1 parent a6b4e81 commit d0ddf28

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"build": "tsc",
4848
"apidoc": "apidoc -i src -o docs",
4949
"prestart": "npm run build",
50-
"start": "cross-env NODE_PATH=dist node dist/run.js",
50+
"start": "cross-env NODE_PATH=dist ./wait-for-it.sh ${AMQP_HOST}:${AMQP_PORT} -- node dist/run.js",
5151
"test": "cross-env NODE_PATH=src NODE_ENV=test mocha --timeout 12000 --exit --require ts-node/register test/utils/setup*.ts test/unit/validators/*.ts test/e2e/*.ts",
5252
"cover": "nyc npm test",
5353
"seedlangs": "node dist/scripts/seed-defaultlangs.js",

wait-for-it.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/sh
2+
# shellcheck shell=dash
3+
4+
#
5+
# Waits for the given host(s) to be available via TCP before executing a given
6+
# command.
7+
#
8+
# Usage: ./wait.sh [-t timeout] host:port [host:port] [...] [-- command args...]
9+
#
10+
# It accepts a number of `host:port` combinations to connect to via netcat.
11+
# The command to execute after each host is reachable can be supplied after the
12+
# `--` argument.
13+
# The default timeout of 10 seconds can be changed via `-t timeout` argument.
14+
#
15+
# Copyright 2016, Sebastian Tschan
16+
# https://blueimp.net
17+
#
18+
# Licensed under the MIT license:
19+
# https://opensource.org/licenses/MIT
20+
#
21+
22+
set -e
23+
24+
TIMEOUT=10
25+
26+
is_integer() {
27+
test "$1" -eq "$1" 2> /dev/null
28+
}
29+
30+
connect_to_service() {
31+
nc -w 1 -z "$1" "$2"
32+
}
33+
34+
wait_for_service() {
35+
local host="${1%:*}"
36+
local port="${1#*:}"
37+
local output
38+
if ! is_integer "$port"; then
39+
printf 'Error: "%s" is not a valid host:port combination.\n' "$1" >&2
40+
return 1
41+
fi
42+
printf 'Waiting for %s to become available ... ' "$1" >&2
43+
# shellcheck disable=SC2155
44+
local timeout=$(($(date +%s)+TIMEOUT))
45+
while ! output="$(connect_to_service "$host" "$port" 2>&1)"; do
46+
if [ "$(date +%s)" -gt "$timeout" ]; then
47+
echo 'timeout' >&2
48+
if [ ! -z "$output" ]; then
49+
echo "$output" >&2
50+
fi
51+
return 1
52+
fi
53+
sleep 1
54+
done
55+
echo 'done' >&2
56+
}
57+
58+
while [ $# != 0 ]; do
59+
if [ "$1" = '-t' ] || [ "$1" = '--timeout' ]; then
60+
if ! is_integer "$2"; then
61+
printf 'Error: "%s" is not a timeout integer.\n' "$2" >&2
62+
exit 1
63+
fi
64+
TIMEOUT="$2"
65+
shift 2
66+
fi
67+
if [ "$1" = '--' ]; then
68+
shift
69+
exec "$@"
70+
fi
71+
wait_for_service "$1"
72+
shift
73+
done

0 commit comments

Comments
 (0)