diff --git a/README.md b/README.md index f169999..ca2d3a9 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ When using this tool, you only need to pick the `wait-for` file as part of your ## Usage ``` -./wait-for host:port [-t timeout] [-- command args] +wait-for host:port [-t timeout] [-- command args] -q | --quiet Do not output any status messages - -s | --strict Only execute subcommand if the test succeeds + -l | --loose Execute subcommand even if the test times out -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes ``` @@ -22,18 +22,17 @@ To check if [eficode.com](https://eficode.com) is available: ``` $ ./wait-for www.eficode.com:80 -- echo "Eficode site is up" - -Connection to www.eficode.com port 80 [tcp/http] succeeded! Eficode site is up ``` The subcommand will be executed regardless if the service is up or not. If you wish to execute the subcommand only if the service is up, add the --strict argument. In this example, we will test port 81 on www.google.com which will fail: ``` -$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up" -$ ./wait-for www.google.com:81 --timeout=1 --strict -- echo "google is up" +$ ./wait-for www.google.com:81 --timeout=1 -- echo "google is up" +Operation timed out +$ ./wait-for www.google.com:81 --timeout=1 --loose -- echo "waited for google" Operation timed out -google is up +waited for google ``` To wait for database container to become available: diff --git a/wait-for b/wait-for index f4cf020..d6b04d8 100755 --- a/wait-for +++ b/wait-for @@ -4,9 +4,11 @@ OLD_TIMEOUT=$TIMEOUT OLD_QUIET=$QUIET OLD_PORT=$PORT OLD_HOST=$HOST +OLD_LOOSE=$LOOSE TIMEOUT=15 QUIET=0 +LOOSE=0 if ! which nc >/dev/null; then echo "Netcat is not installed. This script requires netcat to work correctly." @@ -23,7 +25,7 @@ usage() { Usage: $(basename $0) host:port [-t timeout] [-- command args] -q | --quiet Do not output any status messages - -s | --strict Only execute subcommand if the test succeeds + -l | --loose Execute subcommand even if the test times out -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes USAGE @@ -40,27 +42,19 @@ test_connection() { } wait_for() { + local result for i in `seq $TIMEOUT` ; do # use a 1-second timeout, but still sleep 0.1 seconds after just to be safe test_connection "$HOST" "$PORT" - result=$? - if [ $result -eq 0 ] ; then - if [ $# -gt 0 ] ; then - TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST exec "$@" - fi - exit 0 - fi + if [ $result -eq 0 ] ; then break ; fi sleep 1 done - echo "Operation timed out" >&2 - if [ $result -ne 0 ] && [ $STRICT -ne 1 ] ; then - if [ $# -gt 0 ] ; then - exec "$@" - fi - exit 0 + [ $result -ne 0 ] && echo "Operation timed out" >&2 + if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then + TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@" fi - exit 1 + exit $result } while [ $# -gt 0 ] @@ -75,8 +69,8 @@ do QUIET=1 shift 1 ;; - -s | --strict) - STRICT=1 + -l | --loose) + LOOSE=1 shift 1 ;; -t) @@ -107,6 +101,4 @@ if [ "$HOST" = "" -o "$PORT" = "" ]; then usage 2 fi -STRICT=${STRICT:-0} - wait_for "$@" diff --git a/wait-for.bats b/wait-for.bats index c720419..fa873ac 100644 --- a/wait-for.bats +++ b/wait-for.bats @@ -13,11 +13,27 @@ [ "$output" != "success" ] } -@test "preserve existing environment variable" { - HOST=myweb.com - PORT=8080 +@test "nonexistent server should start command if loose option is specified" { + run ./wait-for -t 1 -l noserver:9999 -- echo 'passable' 2>&1 + + [ "$status" -eq 0 ] + + [ "${lines[0]}" = "Operation timed out" ] + [ "${lines[1]}" = "passable" ] +} + +@test "preserve existing environment variables" { + TIMEOUT=mytimeout + QUIET=myquiet + HOST=myhost + PORT=myport + LOOSE=myloose + run ./wait-for google.com:80 -- echo 'success' - [ "$(echo $HOST)" = 'myweb.com' ] - [ "$(echo $PORT)" = '8080' ] + [ "$(echo $TIMEOUT)" = 'mytimeout' ] + [ "$(echo $QUIET)" = 'myquiet' ] + [ "$(echo $HOST)" = 'myhost' ] + [ "$(echo $PORT)" = 'myport' ] + [ "$(echo $LOOSE)" = 'myloose' ] }