github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/docker/development/shyftApi/wait-for.sh (about)

     1  #!/bin/sh
     2  # https://github.com/eficode/wait-for.git
     3  log() {
     4    echo "[wait-for] [`date +'%Y%m%d%H%M%S'`] $@"
     5  }
     6  
     7  usage() {
     8    echo "Usage: `basename "$0"` [--timeout=15] <HOST>:<PORT> [<HOST_2>:<PORT_2>]"
     9  }
    10  
    11  unknown_arg() {
    12    log "[ERROR] unknown argument: '$@'"
    13    usage
    14    exit 2
    15  }
    16  
    17  wait_for() {
    18    timeout=$1 && host=$2 && port=$3
    19    log "wait '$host':'$port' up to '$timeout'"
    20    for i in `seq $timeout` ; do
    21      if nc -z "$host" "$port" > /dev/null 2>&1 ; then
    22        log "wait finish '$host:$port' [`expr $(date +%s) - $START`] seconds"
    23        exit 0
    24      fi
    25      log "wait attempt '${host}:${port}' [$i]"
    26      sleep 1
    27    done
    28    log "[ERROR] wait timeout of '$timeout' on '$host:$port'"
    29    exit 1
    30  }
    31  
    32  trap 'kill $(jobs -p) &>/dev/null' EXIT
    33  
    34  START=$(date +%s)
    35  timeout=30
    36  pids=""
    37  for i in $@ ; do
    38    case $i in
    39      --timeout=*) timeout="${i#*=}" ;;
    40      -t=*) timeout="${i#*=}" ;;
    41      *:* )
    42        wait_for "$timeout" "${i%%:*}" "${i##*:}" &
    43        pids="$pids $!"
    44      ;;
    45      *) unknown_arg "$i" ;;
    46    esac
    47  done
    48  
    49  status=0
    50  for pid in $pids; do
    51    if ! wait $pid ; then
    52      status=1
    53    fi
    54  done
    55  
    56  log "wait done with status=$status"
    57  exit $status