Publish a Node.js App to Axway’s ARS – Part 2 – Add a Healthcheck

Publish a Node.js App to Axway's ARS
Publish a Node.js App to Axway's ARS

In Part 1, we described how to publish a Node.js app to Axway’s ARS. When we published the app to ARS, we got a warning about a missing healthcheck.

No Healthcheck Warning During Publish to ARS

We went ahead and published anyway since the health check is optional (but recommended).

The healthcheck tells Docker how to test a container to check that it is still working. This can detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server process is still running.

In part 1, we ignored the healthcheck warning but in this blog post, we’ll describe how to add one to your app. When ARS detects that the app is no longer healthy, it will try to start a new instance of your app.

Here are the steps we will follow in this blog post:

  • Create a separate, simple healthcheck Node.js app that will make an http request to our main Node.js app and check the status of the response, looking for 200
  • Modify our Dockerfile and add a HEALTHCHECK instruction for how, when and how often to check for our app’s health
  • Modify our package.json file to include a “version” property as is required for ARS

You can download the assets for this project here.

Healthcheck Node.js App

A common Docker healthcheck technique is to have a simple Node.js app that makes an http request to our main app and look for a response that indicates that the app is running normally (healthy). Since our main Node.js app is a web server, we can simply make an http request to the root path (‘/’) and look for a 200 status. If we get a 200 status, then we can set the healthcheck app exit status to 0, otherwise, we can set to 1. This is described here.

My heathcheck Node.js app, healthcheck.js, is shown below:

const http = require('http');
const options = {
    host: 'localhost',
    port: process.env.PORT || 4001,
    timeout: 5000,
    path: '/'
};

const healthCheck = http.request(options, (res) => {
    console.log(`HEALTHCHECK STATUS: ${res.statusCode}`);
    if (res.statusCode == 200) {
        process.exit(0);
    }
    else {
        process.exit(1);
    }
});

healthCheck.on('error', function (err) {
    console.error('ERROR = '+err);
    process.exit(1);
});

healthCheck.end();

The inspiration for this healthcheck app comes from this tutorial as well as the healthcheck app that is part of the default API Builder project.

This file resides in the root folder of my main Node.js app.

You run this app using the following command:

node healthcheck.js

You can test the healthcheck app locally, by running your main app in one terminal window and then running the healthcheck in another as follows:

Test Your Healthcheck App Locally

Add HEALTHCHECK Instruction to Your Dockerfile

Modify your Dockerfile and add the following line:

HEALTHCHECK --interval=5s --start-period=10s --timeout=5s --retries=5 CMD node healthcheck.js

Your full Dockerfile should look like this:

FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install
COPY . .

HEALTHCHECK --interval=5s --start-period=10s --timeout=5s --retries=5 CMD node healthcheck.js

CMD ["npm","start"]

Modify Your Package.json File

Our package.json is very minimal and not compliant with ARS. We need to add a version property to it. Edit your package.json as follows:

{
  "version": "0.2.0",
  "dependencies": {
    "express": "*"
  },
  "scripts": {
    "start": "node index.js"
  }
}

Note that we only added the line: “version”: “0.2.0” at the top of our package.json file

Follow the steps in Part 1 to create a Docker image and publish your app as version 0.2 using:

amplify acs publish simplenodeapp --delete_oldest --force --image simplenodeapp --app_version 0.2

This time you will not get a healthcheck warning! Furthermore, if your app ever becomes “unhealthy”, ARS will spin up a new instance, minimizing downtime for your application.

Summary

In this blog post, we saw how you can add a healthcheck to your Node.js app published in Axway’s ARS.

 

 

 

 

 

 

 

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.