File size: 1,323 Bytes
aa7cb02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash
#   Use this script to test if a given URL is responding
#
# Usage:
# ./wait-for-it.sh --url http://0.0.0.0:8000/health --strict -- node /app/app/server.js

url=""
cmd=""
strict=""

while [[ $# -gt 0 ]]
do
  key="$1"

  case $key in
    --url)
      url="$2"
      shift # past argument
      shift # past value
      ;;
    --)
      shift
      cmd="$@"
      break
      ;;
    --strict)
      strict="true"
      shift # past argument
      ;;
    *)
      echo "Unknown option: $key"
      exit 1
      ;;
  esac
done

if [[ -z "$url" ]]; then
  echo "URL is required"
  exit 1
fi

wait_for_url() {
  response=$(curl --write-out "%{http_code}" --silent --output /dev/null "$1")
  if [[ "$response" -eq 200 ]]; then
    return 0
  else
    return 1
  fi
}

if [[ "$strict" == "true" ]]; then
  echo "wait-for-it.sh: waiting for $url without a timeout"
  while ! wait_for_url "$url"; do
    sleep 1
  done
  echo "wait-for-it.sh: $url is available"
else
  echo "wait-for-it.sh: waiting for $url with a timeout of 15 seconds"
  timeout=15
  while ! wait_for_url "$url"; do
    sleep 1
    timeout=$((timeout - 1))
    if [[ $timeout -eq 0 ]]; then
      echo "wait-for-it.sh: timeout occurred after waiting for $url"
      exit 1
    fi
  done
  echo "wait-for-it.sh: $url is available"
fi

exec $cmd