Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new EXTRA wait parameter, and perl functions for mac compatibility #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions wait-for-it.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
#!/usr/bin/env bash
#!/bin/bash
# Use this script to test if a given TCP host/port are available

cmdname=$(basename $0)

echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }

# Added for mac compatibility.
unameOS="$(uname -s)"
if [[ "$unameOS" == "Darwin" ]]; then
function timeout() { perl -e 'alarm shift; exec @ARGV' "$@"; }
function realpath() { [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"; }
fi

usage()
{
cat << USAGE >&2
Usage:
$cmdname host:port [-s] [-t timeout] [-- command args]
$cmdname host:port [-s] [-t timeout] [-x extra] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-x EXTRA | --extra=WAIT Extra time in seconds to wait after the port is open
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
Expand All @@ -42,20 +50,23 @@ wait_for()
if [[ $result -eq 0 ]]; then
end_ts=$(date +%s)
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
if [[ $EXTRA -gt 0 ]]; then
echoerr "sleeping an EXTRA ${EXTRA} seconds"
sleep $EXTRA
fi
break
fi
sleep 1
done
return $result
}

wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $QUIET -eq 1 ]]; then
timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT --extra=$EXTRA &
else
timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT --extra=$EXTRA &
fi
PID=$!
trap "kill -INT -$PID" INT
Expand Down Expand Up @@ -116,6 +127,15 @@ do
TIMEOUT="${1#*=}"
shift 1
;;
-x)
EXTRA="$2"
if [[ $EXTRA == "" ]]; then break; fi
shift 2
;;
--extra=*)
EXTRA="${1#*=}"
shift 1
;;
--)
shift
CLI=("$@")
Expand All @@ -130,16 +150,15 @@ do
;;
esac
done

if [[ "$HOST" == "" || "$PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi

TIMEOUT=${TIMEOUT:-15}
TIMEOUT=${TIMEOUT:-15}
STRICT=${STRICT:-0}
CHILD=${CHILD:-0}
QUIET=${QUIET:-0}
EXTRA=${EXTRA:-0}

# check to see if timeout is from busybox?
# check to see if timeout is from busybox?
Expand All @@ -151,7 +170,6 @@ else
ISBUSY=0
BUSYTIMEFLAG=""
fi

if [[ $CHILD -gt 0 ]]; then
wait_for
RESULT=$?
Expand All @@ -165,7 +183,6 @@ else
RESULT=$?
fi
fi

if [[ $CLI != "" ]]; then
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
echoerr "$cmdname: strict mode, refusing to execute subprocess"
Expand All @@ -174,4 +191,4 @@ if [[ $CLI != "" ]]; then
exec "${CLI[@]}"
else
exit $RESULT
fi
fi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @nbrookins,
Please keep the new line here.