Puppet wait for a service to be ready

10,514

Solution 1

Thanks to lzap and people in Puppet irc channel here is a solution:

exec {"wait for tomcat":
  require => Service["tomcat6"],
  command => "/usr/bin/wget --spider --tries 10 --retry-connrefused --no-check-certificate https://localhost:8443/service/",
}

When using require => Exec["wait for tomcat"] in dependant manifest, it won't run until the service is really ready.

Solution 2

Not a puppet, but shell...

max=30; while ! wget --spider http://localhost:8080/APP > /dev/null 2>&1; do
  max=$(( max - 1 )); [ $max -lt 0 ] && break; sleep 1
done; [ $max -gt 0 ]

This is improved version.

It returns true when application was found, false when max has been reached.

Share:
10,514
iNecas
Author by

iNecas

Updated on July 29, 2022

Comments

  • iNecas
    iNecas over 1 year

    I am using Puppet for machines provisioning. I have a service running in Tomcat 6 app server and another manifest being dependant on that service (sending some REST requests as part of the installation). The problem is, the service is not available right after tomcat being started using:

    service {"tomcat6":
      ensure  => running, enable => true, hasstatus => true, hasrestart => true;
    }
    

    So I need some require condition for another manifest that would ensure that the service is really running (e.g. checking some URL to be available). And in case it's not ready yet wait some time and try it again with some limit on the amount of retries.

    Is there some idiomatic Puppet solution, or some another, that would achieve this?

    Note - sleep is not a solution.

  • iNecas
    iNecas over 12 years
    unfortunately, max can't be used this way as Puppet command (Puppet complains), fortunately wget can retry automatically (see my answer)
  • lzap
    lzap over 12 years
    Nice. Was so focused on the bash solution. So it returns zero when tomcat goes up, non-zero when it fails 10 times. Cool.