1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #!/bin/bash
-
- ffoo import core
-
-
- wait_until() {
- #
- # Wait until command succeeds (or fails)
- #
- # Repeat calling a command until it succeeds or exits with
- # acceptable status. For example:
- #
- # wait_until -t 90 my_command
- # wait_until -t 90 -d 30 my_expensive_command
- # wait_until -x 7 command_that_should_exit_with_7
- # wait_until -X "-lt 7" command_that_should_exit_with_less_than_7
- # wait_until -e some_bash_builtin
- #
- # Note that polling happens synchronously so if your
- # command blocks for more than specified delay, freequency
- # and timeout cannot be guaranteed. If you need to assure
- # more predictable behavior, limit your command with
- # timeout(1)
- #
- local timeout=10
- local delay=1
- local use_eval=false
- local es_test="-eq 0"
- while true; do case "$1" in
- -x) es_test="-eq $2"; shift; ;;
- -X) es_test="$2"; shift; ;;
- -e) use_eval=true; shift; break ;;
- -d) delay="$2"; shift 2; ;;
- -t) timeout="$2"; shift 2; ;;
- --) shift 1; break; ;;
- *) break; ;;
- esac done
- think "waiting for: $@"
- local elapsed=0
- local start=$(date +%s)
- while true;
- do
- sleep $delay
- local now=$(date +%s)
- local elapsed=$(($now - $start))
- local es=0
- debug "\$@='$@'"
- if $use_eval;
- then
- eval $@;
- es=$?
- else
- $@;
- es=$?
- fi
- debug -v es
- if [ $es $es_test ];
- then
- return 0;
- fi
- if test $elapsed -gt $timeout;
- then
- warn "timeout reached ($elapsed>$timeout) when waiting for ($es $es_test): $@"
- return 1
- fi
- think "waiting for another $delay s"
- done
- }
|