[Ovirt-devel] [PATCH] generalized wait_for_service

Perry N. Myers pmyers at redhat.com
Wed Jul 2 11:35:05 UTC 2008


Alan Pevec wrote:
> services were assumed to be ready immediately after startup, which isn't guaranteed
> This generalizes waiting loop w/ timeout we had for postgres to a shell function, so it can be used where needed.
> Portability fix by Jim Meyering <meyering at redhat.com>
> 
> Signed-off-by: Alan Pevec <apevec at redhat.com>

Looks reasonable to me.  ACK

Perry

> ---
>  wui/scripts/ovirt-wui-install |   35 ++++++++++++++++++++---------------
>  1 files changed, 20 insertions(+), 15 deletions(-)
> 
> diff --git a/wui/scripts/ovirt-wui-install b/wui/scripts/ovirt-wui-install
> index 6eb7d8e..8839358 100755
> --- a/wui/scripts/ovirt-wui-install
> +++ b/wui/scripts/ovirt-wui-install
> @@ -71,6 +71,24 @@ find_ldap_base() {
>      fi   
>  }
>  
> +# wait_for_service 'test command' max_retries seconds_to_sleep_between_retries
> +wait_for_service() {
> +  local testcmd=$1; shift
> +  local n_retries=$1; shift
> +  local sleep_seconds=$1; shift
> +  local total=$((n_retries*sleep_seconds))
> +
> +  while [ $n_retries -gt 0 ]
> +  do
> +    eval "$testcmd" > /dev/null 2>&1 && return 0
> +    echo service not ready yet, retrying...
> +    n_retries=$((n_retries-1))
> +    sleep $sleep_seconds
> +  done
> +  printf 'service not ready after %d seconds, giving up\n' $total 1>&2
> +  return 1
> +}
> +
>  PASSWD=
>  for i ; do
>      case $1 in
> @@ -124,6 +142,7 @@ fi
>  # dnsmasq
>  if [[ "$PROD_INST" == "false" ]]; then
>      service libvirtd status > /dev/null 2&>1 || service libvirtd start > /dev/null 2>&1
> +    wait_for_service 'virsh connect' 10 2 || exit 1
>      virsh net-destroy default
>      virsh net-undefine default
>  fi
> @@ -135,21 +154,7 @@ echo "host all all 127.0.0.1 255.255.255.0 trust" >> /var/lib/pgsql/data/pg_hba.
>  service postgresql stop > /dev/null 2>&1
>  service postgresql start
>  [ $? != 0 ] && echo "Failed to start database" && exit 1
> -declare -i timeout=10
> -while [[ timeout -gt 0 ]]
> -do
> -  psql -l -U postgres > /dev/null 2>&1
> -  rc=$?
> -  if [[ rc -eq 0 ]]
> -  then
> -     break
> -  else
> -     echo Database not ready yet, retrying...
> -  fi
> -  let timeout--
> -  sleep 2
> -done
> -[[ timeout -eq 0 ]] && echo "Failed to start database" && exit 1
> +wait_for_service 'psql -l -U postgres' 10 2 || exit 1
>  
>  if [ -z $PASSWD ]; then
>      # generate random pg user password


-- 
|=-        Red Hat, Engineering, Emerging Technologies, Boston        -=|
|=-                     Email: pmyers at redhat.com                      -=|
|=-         Office: +1 412 474 3552   Mobile: +1 703 362 9622         -=|
|=- GnuPG: E65E4F3D 88F9 F1C9 C2F3 1303 01FE 817C C5D2 8B91 E65E 4F3D -=|




More information about the ovirt-devel mailing list