Skip to content

Commit

Permalink
Quiet down healthchecks to save battery and CPU usage, fixes ddev#1663 (
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay authored Jul 3, 2019
1 parent 53fb9c0 commit a2b82a6
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 35 deletions.
4 changes: 2 additions & 2 deletions containers/ddev-dbserver/10.1/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mariadb:10.1.38-bionic
FROM mariadb:10.1.40-bionic

ENV MYSQL_DATABASE db
ENV MYSQL_USER db
Expand Down Expand Up @@ -26,4 +26,4 @@ ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 3306
# The following line overrides any cmd entry
CMD []
HEALTHCHECK --interval=2s --retries=30 CMD ["/healthcheck.sh"]
HEALTHCHECK --interval=1s --retries=30 --timeout=120s CMD ["/healthcheck.sh"]
1 change: 1 addition & 0 deletions containers/ddev-dbserver/10.1/files/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -eu
set -o pipefail

SOCKET=/var/tmp/mysql.sock
rm -f /tmp/healthy

# Wait for mysql server to be ready.
function serverwait {
Expand Down
21 changes: 20 additions & 1 deletion containers/ddev-dbserver/10.1/files/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
## mysql health check for docker. original source: https://github.com/docker-library/healthcheck/blob/master/mysql/docker-healthcheck

set -eo pipefail
sleeptime=59

mysql --host=127.0.0.1 -udb -pdb --database=db -e "SHOW DATABASES LIKE 'db';" >/dev/null
# Since docker doesn't provide a lazy period for startup,
# we track health. If the last check showed healthy
# as determined by existence of /tmp/healthy, then
# sleep at startup. This requires the timeout to be set
# higher than the sleeptime used here.
if [ -f /tmp/healthy ]; then
printf "container was previously healthy, so sleeping ${sleeptime} seconds before continuing healthcheck... "
sleep ${sleeptime}
fi


if mysql --host=127.0.0.1 -udb -pdb --database=db -e "SHOW DATABASES LIKE 'db';" >/dev/null; then
printf "healthy"
touch /tmp/healthy
exit 0
fi

rm -f /tmp/healthy
exit 1

4 changes: 2 additions & 2 deletions containers/ddev-dbserver/10.2/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mariadb:10.2.22
FROM mariadb:10.2.25

ENV MYSQL_DATABASE db
ENV MYSQL_USER db
Expand Down Expand Up @@ -26,4 +26,4 @@ ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 3306
# The following line overrides any cmd entry
CMD []
HEALTHCHECK --interval=2s --retries=30 CMD ["/healthcheck.sh"]
HEALTHCHECK --interval=1s --retries=30 --timeout=120s CMD ["/healthcheck.sh"]
2 changes: 2 additions & 0 deletions containers/ddev-dbserver/10.2/files/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ set -o pipefail

SOCKET=/var/tmp/mysql.sock

rm -f /tmp/healthy

# Wait for mysql server to be ready.
function serverwait {
for i in {60..0};
Expand Down
21 changes: 20 additions & 1 deletion containers/ddev-dbserver/10.2/files/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
## mysql health check for docker. original source: https://github.com/docker-library/healthcheck/blob/master/mysql/docker-healthcheck

set -eo pipefail
sleeptime=59

mysql --host=127.0.0.1 -udb -pdb --database=db -e "SHOW DATABASES LIKE 'db';" >/dev/null
# Since docker doesn't provide a lazy period for startup,
# we track health. If the last check showed healthy
# as determined by existence of /tmp/healthy, then
# sleep at startup. This requires the timeout to be set
# higher than the sleeptime used here.
if [ -f /tmp/healthy ]; then
printf "container was previously healthy, so sleeping ${sleeptime} seconds before continuing healthcheck... "
sleep ${sleeptime}
fi


if mysql --host=127.0.0.1 -udb -pdb --database=db -e "SHOW DATABASES LIKE 'db';" >/dev/null; then
printf "healthy"
touch /tmp/healthy
exit 0
fi

rm -f /tmp/healthy
exit 1

4 changes: 2 additions & 2 deletions containers/ddev-router/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nginx:1.15.12
FROM nginx:1.17.0

ENV MKCERT_VERSION=v1.3.0

Expand Down Expand Up @@ -34,4 +34,4 @@ ENTRYPOINT ["/app/docker-entrypoint.sh"]
CMD ["forego", "start", "-r"]
WORKDIR /app/

HEALTHCHECK --interval=5s --retries=5 CMD /app/healthcheck.sh
HEALTHCHECK --interval=1s --retries=10 --timeout=120s --start-period=10s CMD /app/healthcheck.sh
2 changes: 2 additions & 0 deletions containers/ddev-router/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
set -e

rm -f /tmp/healthy

# Warn if the DOCKER_HOST socket does not exist
if [[ $DOCKER_HOST = unix://* ]]; then
socket_file=${DOCKER_HOST#unix://}
Expand Down
38 changes: 35 additions & 3 deletions containers/ddev-router/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,39 @@
set -eu
set -o pipefail

# Check nginx config
nginx -t || exit 1
sleeptime=59

# Since docker doesn't provide a lazy period for startup,
# we track health. If the last check showed healthy
# as determined by existence of /tmp/healthy, then
# sleep at startup. This requires the timeout to be set
# higher than the sleeptime used here.
if [ -f /tmp/healthy ]; then
printf "container was previously healthy, so sleeping ${sleeptime} seconds before continuing healthcheck... "
sleep ${sleeptime}
fi

config=false
connect=false

if nginx -t ; then
config=true
printf "nginx config: OK "
fi

# Check our healthcheck endpoint
curl --fail --connect-timeout 2 --retry 2 http://127.0.0.1/healthcheck || (echo "ddev-router healthcheck endpoint not responding" && exit 2)
if curl --fail --connect-timeout 2 --retry 2 http://127.0.0.1/healthcheck; then
connect=true
echo "nginx healthcheck endpoint: OK "
else
echo "ddev-router healthcheck endpoint not responding "
fi

if [ ${config} = true -a ${connect} = true ]; then
printf "healthy"
touch /tmp/healthy
exit 0
fi

rm -f /tmp/healthy
exit 1
4 changes: 2 additions & 2 deletions containers/ddev-ssh-agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
FROM alpine:3.8
FROM alpine:3.10

# Install dependencies
RUN apk add --no-cache \
Expand All @@ -48,7 +48,7 @@ RUN ln -s $SSH_KEY_DIR /home/.ssh

RUN mkdir ${SOCKET_DIR} && mkdir ${SSH_KEY_DIR} && chmod 777 ${SOCKET_DIR} ${SSH_KEY_DIR}

HEALTHCHECK --interval=2s --retries=5 CMD ["/healthcheck.sh"]
HEALTHCHECK --interval=1s --retries=5 --timeout=120s CMD ["/healthcheck.sh"]

VOLUME ${SOCKET_DIR}

Expand Down
1 change: 1 addition & 0 deletions containers/ddev-ssh-agent/files/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


set -eo pipefail
rm -f /tmp/healthy

# Print a debug message if debug mode is on ($DEBUG is not empty)
# @param message
Expand Down
18 changes: 17 additions & 1 deletion containers/ddev-ssh-agent/files/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@
# ddev-ssh-agent healthcheck

set -eo pipefail
sleeptime=59

# Make sure that both socat and ssh-agent are running
killall -0 socat && killall -0 ssh-agent
# Since docker doesn't provide a lazy period for startup,
# we track health. If the last check showed healthy
# as determined by existence of /tmp/healthy, then
# sleep at startup. This requires the timeout to be set
# higher than the sleeptime used here.
if [ -f /tmp/healthy ]; then
printf "container was previously healthy, so sleeping ${sleeptime} seconds before continuing healthcheck... "
sleep ${sleeptime}
fi
if killall -0 socat ssh-agent; then
printf "healthy"
touch /tmp/healthy
exit 0
fi
rm -f /tmp/healthy
exit 1

2 changes: 1 addition & 1 deletion containers/ddev-webserver/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,6 @@ RUN chmod ugo+x /start.sh /healthcheck.sh


EXPOSE 80 8025
HEALTHCHECK --interval=3s --retries=6 CMD ["/healthcheck.sh"]
HEALTHCHECK --interval=1s --retries=10 --timeout=120s CMD ["/healthcheck.sh"]

CMD ["/start.sh"]
49 changes: 46 additions & 3 deletions containers/ddev-webserver/scripts/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,49 @@

set -eo pipefail

curl --fail -s 127.0.0.1/phpstatus >/dev/null && printf "phpstatus: OK, " || (printf "phpstatus FAILED" && exit 1)
ls /var/www/html >/dev/null && printf "/var/www/html: OK, " || (printf "/var/www/html access FAILED" && exit 2)
curl --fail -s localhost:8025 >/dev/null && printf "mailhog: OK" || (printf "mailhog FAILED" && exit 3)
sleeptime=59

# Make sure that both phpstatus, mounted code, and mailhog
# are working.
# Since docker doesn't provide a lazy period for startup,
# we track health. If the last check showed healthy
# as determined by existence of /tmp/healthy, then
# sleep at startup. This requires the timeout to be set
# higher than the sleeptime used here.
if [ -f /tmp/healthy ]; then
printf "container was previously healthy, so sleeping ${sleeptime} seconds before continuing healthcheck... "
sleep ${sleeptime}
fi

phpstatus=false
htmlaccess=false
mailhog=false
if curl --fail -s 127.0.0.1/phpstatus >/dev/null ; then
phpstatus=true
printf "phpstatus: OK "
else
printf "phpstatus FAILED "
fi

if ls /var/www/html >/dev/null; then
htmlstatus=true
printf "/var/www/html: OK "
else
printf "/var/www/html access FAILED"
fi

if curl --fail -s localhost:8025 >/dev/null; then
mailhog=true
printf "mailhog: OK " ;
else
printf "mailhog FAILED "
fi

if ${phpstatus} = true -a ${htmlaccess} = true -a ${mailhog} = true ; then
touch /tmp/healthy
exit 0
fi
rm -f /tmp/healthy
exit 1


2 changes: 2 additions & 0 deletions containers/ddev-webserver/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -x
set -o errexit nounset pipefail

rm -f /tmp/healthy

# If DDEV_PHP_VERSION isn't set, use a reasonable default
DDEV_PHP_VERSION="${DDEV_PHP_VERSION:-$PHP_DEFAULT_VERSION}"

Expand Down
25 changes: 15 additions & 10 deletions pkg/ddevapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ services:
- TZ={{ .Timezone }}
command: "$DDEV_MARIADB_LOCAL_COMMAND"
healthcheck:
interval: 5s
retries: 12
start_period: 60s
interval: 1s
retries: 10
start_period: 10s
timeout: 120s
web:
container_name: {{ .Plugin }}-${DDEV_SITENAME}-web
{{ if .WebBuildContext }}
Expand Down Expand Up @@ -114,9 +115,10 @@ services:
{{ range $hostname := .Hostnames }}- "ddev-router:{{ $hostname }}"
{{ end }}
healthcheck:
interval: 4s
retries: 6
interval: 1s
retries: 10
start_period: 10s
timeout: 120s
{{ if .WebcacheEnabled }}
bgsync:
container_name: ddev-${DDEV_SITENAME}-bgsync
Expand Down Expand Up @@ -168,7 +170,7 @@ services:
# HTTP_EXPOSE allows for ports accepting HTTP traffic to be accessible from <site>.ddev.site:<port>
- HTTP_EXPOSE=${DDEV_PHPMYADMIN_PORT}:{{ .DBAPort }}
healthcheck:
interval: 90s
interval: 120s
timeout: 2s
retries: 1
Expand Down Expand Up @@ -397,9 +399,10 @@ services:
- ddev-global-cache:/mnt/ddev-global-cache:rw
restart: "no"
healthcheck:
interval: 6s
retries: 6
interval: 1s
retries: 10
start_period: 10s
timeout: 120s
networks:
default:
Expand Down Expand Up @@ -427,8 +430,10 @@ services:
environment:
- SSH_AUTH_SOCK=/tmp/.ssh-agent/socket
healthcheck:
interval: 2s
retries: 5
interval: 1s
retries: 2
start_period: 10s
timeout: 62s
networks:
default:
external:
Expand Down
5 changes: 2 additions & 3 deletions pkg/dockerutil/dockerutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func TestMain(m *testing.M) {
},
Env: []string{"HOTDOG=superior-to-corndog", "POTATO=future-fry"},
},
// "PortBindings": { "22/tcp": [{ "HostPort": "11022" }] },
HostConfig: &docker.HostConfig{
PortBindings: map[docker.Port][]docker.PortBinding{
"80/tcp": {
Expand Down Expand Up @@ -127,11 +126,11 @@ func TestGetContainerHealth(t *testing.T) {
healthDetail, err := ContainerWait(15, labels)
assert.NoError(err)

assert.Equal("phpstatus: OK, /var/www/html: OK, mailhog: OK", healthDetail)
assert.Equal("phpstatus: OK /var/www/html: OK mailhog: OK ", healthDetail)

status, healthDetail = GetContainerHealth(container)
assert.Equal(status, "healthy")
assert.Equal("phpstatus: OK, /var/www/html: OK, mailhog: OK", healthDetail)
assert.Equal("phpstatus: OK /var/www/html: OK mailhog: OK ", healthDetail)
}

// TestContainerWait tests the error cases for the container check wait loop.
Expand Down
8 changes: 4 additions & 4 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ var DockerComposeFileFormatVersion = "3.6"
var WebImg = "drud/ddev-webserver"

// WebTag defines the default web image tag for drud dev
var WebTag = "20190626_apache" // Note that this can be overridden by make
var WebTag = "20190626_healthcheck" // Note that this can be overridden by make

// DBImg defines the default db image used for applications.
var DBImg = "drud/ddev-dbserver"

// BaseDBTag is the main tag, DBTag is constructed from it
var BaseDBTag = "v1.9.0"
var BaseDBTag = "20190626_healthcheck"

// DBAImg defines the default phpmyadmin image tag used for applications.
var DBAImg = "drud/phpmyadmin"
Expand All @@ -69,11 +69,11 @@ var BgsyncTag = "v1.9.0" // Note that this can be overridden by make
var RouterImage = "drud/ddev-router"

// RouterTag defines the tag used for the router.
var RouterTag = "v1.9.0" // Note that this can be overridden by make
var RouterTag = "20190626_healthcheck" // Note that this can be overridden by make

var SSHAuthImage = "drud/ddev-ssh-agent"

var SSHAuthTag = "v1.9.0"
var SSHAuthTag = "20190626_healthcheck"

// COMMIT is the actual committish, supplied by make
var COMMIT = "COMMIT should be overridden"
Expand Down

0 comments on commit a2b82a6

Please sign in to comment.