Skip to content

Commit

Permalink
finished healthcheck improvements and added troubleshooting info for …
Browse files Browse the repository at this point in the history
…kubernetes
  • Loading branch information
adrianrudnik committed Aug 31, 2021
1 parent e5344e9 commit 9555f3b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ FROM node:15-alpine
ENV NODE_ENV=production

ENV CORS=""
ENV PORT=80

ENV MJML_KEEP_COMMENTS=false
ENV MJML_VALIDATION_LEVEL=soft
ENV MJML_MINIFY=true
ENV MJML_MINIFY=false
ENV HEALTHCHECK=true

COPY package* ./

Expand All @@ -20,9 +22,8 @@ COPY index.js ./index.js

COPY healthcheck.sh /healthcheck.sh

HEALTHCHECK --timeout=30s \
CMD /healthcheck.sh || exit 1
HEALTHCHECK --start-period=10s --retries=1 CMD /healthcheck.sh || exit 1

EXPOSE 80

ENTRYPOINT [ "node", "index.js"]
ENTRYPOINT [ "node", "index.js" ]
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Due to various challenges this image sports the following features:
- [Defaults](#defaults)
- [Development](#development)
- [Troubleshooting](#troubleshooting)
- [Kubernetes](#kubernetes)

## Overview

Expand All @@ -36,7 +37,7 @@ services:
mjml:
image: adrianrudnik/mjml-server
ports:
- 8889:80
- 8080:80
# environment:
# to change the port:
# - PORT=8080
Expand All @@ -58,6 +59,7 @@ MJML_KEEP_COMMENTS "false"
MJML_VALIDATION_LEVEL "soft"
MJML_MINIFY "true"
MJML_BEAUTIFY "false"
HEALTHCHECK "true"
```

## Development
Expand All @@ -70,6 +72,7 @@ MJML_KEEP_COMMENTS "true"
MJML_VALIDATION_LEVEL "strict"
MJML_MINIFY "false"
MJML_BEAUTIFY "true"
HEALTHCHECK "false"
```

This will escalate any issues you have with invalid mjml code to the docker log (`stdout` or `docker-compose logs`).
Expand All @@ -79,3 +82,26 @@ This will escalate any issues you have with invalid mjml code to the docker log
Make sure you pass along a plain Content-Type header and pass the mjml as raw body.

Catch errors by looking at the HTTP response code.

### Kubernetes

As the default Dockerfile specific `HEALTHCHECK` directive is not supported by kubernetes, you might need to specify your own probes:

```
spec:
containers:
- name: ...
livenessProbe:
exec:
command:
- curl - -X POST - 'http://localhost:80/'
initialDelaySeconds: 30
periodSeconds: 30
readinessProbe:
exec:
command:
- curl - -X POST - 'http://localhost:80/'
initialDelaySeconds: 25
```

Be aware that this does only check the connectivity and that the port might vary. If you want a functional check as well, you could shift to an approach like the ones used for docker with the result of the [healthcheck.sh](healthcheck.sh). But I'm not a kubernetes user, so feel free to do a pull request if you have a slim approach.
6 changes: 5 additions & 1 deletion healthcheck.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/sh

HOST=http://localhost:80/
if [ "$HEALTHCHECK" == "false" ]; then
exit 0
fi

HOST=http://127.0.0.1:${PORT}/
TOKEN=RmVXY49YwsRfuBBfiYcWOpq6Py57pfa2x
RESULT=$(curl -s -f -X POST -H "Content-Type: text/plain" --data "<mjml><mj-body><mj-section><mj-column><mj-text>${TOKEN}</mj-text></mj-column></mj-section></mj-body></mjml>" ${HOST})

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const opts = {
minify: (process.env.MJML_MINIFY === 'true'),
beautify: (process.env.MJML_BEAUTIFY === 'true'),
validationLevel: (['soft', 'strict', 'skip'].includes(process.env.MJML_VALIDATION_LEVEL) ? process.env.MJML_VALIDATION_LEVEL : 'soft'),
healthchecks: (process.env.HEALTHCHECK === 'true')
}

app.all('*', function (req, res) {
Expand Down Expand Up @@ -79,6 +80,7 @@ Object.keys(signals).forEach((signal) => {

console.log('self: ' + os.hostname() + ':' + server.address().port)
console.log('cors: ' + (process.env.CORS || 'n/a'))
console.log('healthchecks: ' + opts.healthchecks)
console.log('mjml keep comments: ' + opts.keepComments)
console.log('mjml validation level: ' + opts.validationLevel)
console.log('mjml minify: ' + opts.minify)
Expand Down

0 comments on commit 9555f3b

Please sign in to comment.