Skip to content

Commit

Permalink
Add health check command
Browse files Browse the repository at this point in the history
  • Loading branch information
oanhnn committed Mar 7, 2019
1 parent 5b43334 commit 2696d63
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ before_script:

script:
- ./hooks/build
- ./hooks/test
- ./hooks/travis_test
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ VOLUME /app

EXPOSE 6001

HEALTHCHECK --interval=30s --timeout=5s \
CMD /usr/local/bin/health-check

CMD ["start"]
40 changes: 40 additions & 0 deletions bin/health-check
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh
set -x

_init () {
scheme="http://"
address="$(netstat -nplt 2>/dev/null | awk ' /(.*\/laravel-echo-serv)/ { gsub(":::","127.0.0.1:",$4); print $4}')"
resource="/socket.io/socket.io.js"
start=$(stat -c "%Y" /proc/1)
}

fn_health_check () {
# In distributed environment like Swarm, traffic is routed
# to a container only when it reports a `healthy` status. So, we exit
# with 0 to ensure healthy status till distributed service starts (120s).
#
# Refer: https://github.com/moby/moby/pull/28938#issuecomment-301753272
if [[ $(( $(date +%s) - start )) -lt 120 ]]; then
exit 0
else
# Get the http response code
http_response=$(curl -s -k -o /dev/null -w "%{http_code}" ${scheme}${address}${resource})

# Get the http response body
http_response_body=$(curl -k -s ${scheme}${address}${resource})

# server returns response 403 and body "SSL required" if non-TLS
# connection is attempted on a TLS-configured server. Change
# the scheme and try again
if [[ "$http_response" = "403" ]] && \
[[ "$http_response_body" = "SSL required" ]]; then
scheme="https://"
http_response=$(curl -s -k -o /dev/null -w "%{http_code}" ${scheme}${address}${resource})
fi

# If http_response is 200 - server is up.
[[ "$http_response" = "200" ]]
fi
}

_init && fn_health_check
12 changes: 0 additions & 12 deletions hooks/test

This file was deleted.

15 changes: 15 additions & 0 deletions hooks/travis_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

IMAGE_NAME="oanhnn/laravel-echo-server:latest"
LARAVEL_ECHO_SERVER_DB="redis"

echo "=> Testing the image ${IMAGE_NAME}"
docker run -d --net="host" \
-e REDIS_HOST=localhost \
-e LARAVEL_ECHO_SERVER_DB=${LARAVEL_ECHO_SERVER_DB} \
${IMAGE_NAME}

sleep 60
docker ps

curl --retry 10 --retry-delay 2 -I http://127.0.0.1:6001/socket.io/socket.io.js

0 comments on commit 2696d63

Please sign in to comment.