github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/containers/ddev-webserver/ddev-webserver-prod-scripts/healthcheck.sh (about)

     1  #!/bin/sh
     2  
     3  # This uses /bin/sh, so it doesn't initialize profile/bashrc/etc
     4  
     5  # ddev-webserver healthcheck
     6  
     7  set -e
     8  
     9  sleeptime=59
    10  
    11  # Make sure that phpstatus, mounted code, webserver, NOT mailpit
    12  # (mailpit is excluded on hardened/prod)
    13  # are working.
    14  # Since docker doesn't provide a lazy period for startup,
    15  # we track health. If the last check showed healthy
    16  # as determined by existence of /tmp/healthy, then
    17  # sleep at startup. This requires the timeout to be set
    18  # higher than the sleeptime used here.
    19  if [ -f /tmp/healthy ]; then
    20      printf "container was previously healthy, so sleeping %s seconds before continuing healthcheck... " ${sleeptime}
    21      sleep ${sleeptime}
    22  fi
    23  
    24  # Shutdown the supervisor if one of the critical processes is in the FATAL state
    25  for service in php-fpm nginx apache2; do
    26    if supervisorctl status "${service}" 2>/dev/null | grep -q FATAL; then
    27      printf "%s:FATAL " "${service}"
    28      supervisorctl shutdown
    29    fi
    30  done
    31  
    32  phpstatus="false"
    33  htmlaccess="false"
    34  gunicornstatus="false"
    35  
    36  if ls /var/www/html >/dev/null; then
    37      htmlaccess="true"
    38      printf "/var/www/html:OK "
    39  else
    40      printf "/var/www/html:FAILED "
    41  fi
    42  
    43  # If DDEV_WEBSERVER_TYPE is not set, use reasonable default
    44  DDEV_WEBSERVER_TYPE=${DDEV_WEBSERVER_TYPE:-nginx-fpm}
    45  
    46  if [ "${DDEV_WEBSERVER_TYPE#*-}" = "gunicorn" ]; then
    47    phpstatus="true"
    48    if pkill -0 gunicorn; then
    49      gunicornstatus="true"
    50      printf "gunicorn:OK "
    51    else
    52      printf "gunicorn:FAILED "
    53    fi
    54  
    55  fi
    56  
    57  if [ "${DDEV_WEBSERVER_TYPE#*-}" = "fpm" ]; then
    58    gunicornstatus="true"
    59    if curl --fail -s 127.0.0.1/phpstatus >/dev/null; then
    60      phpstatus="true"
    61      printf "phpstatus:OK "
    62    else
    63      printf "phpstatus:FAILED "
    64    fi
    65  fi
    66  
    67  if [ "${phpstatus}" = "true" ] && [ "${gunicornstatus}" = "true" ] && [ "${htmlaccess}" = "true" ]; then
    68      touch /tmp/healthy
    69      exit 0
    70  fi
    71  rm -f /tmp/healthy
    72  
    73  exit 1