Skip to content
Advertisement

How to restart if NodeJS API service failed?

I’ve the similar NodeJS code:

cluster.js

'use strict';

const cluster = require('cluster');
var express = require('express');
const metricsServer = express();
const AggregatorRegistry = require('prom-client').AggregatorRegistry;
const aggregatorRegistry = new AggregatorRegistry();
var os = require('os');

if (cluster.isMaster) {
    for (let i = 0; i < os.cpus().length; i++) {
        cluster.fork();
    }

    metricsServer.get('/metrics', (req, res) => {
        aggregatorRegistry.clusterMetrics((err, metrics) => {
            if (err) console.log(err);
            res.set('Content-Type', aggregatorRegistry.contentType);
            res.send(metrics);
        });
    });

    metricsServer.listen(3013);
    console.log(
        'Cluster metrics server listening to 3013, metrics exposed on /metrics'
    );
} else {
    require('./app.js'); // Here it'll handle all of our API service and it'll run under port 3000
}

As you can see in the above code I’m using NodeJS Manual cluster method instead of PM2 cluster, because I need to monitor my API via Prometheus. I’m usually starting the cluster.js via pm2 start cluster.js, however due to some DB connection our app.js service failed but cluster.js didn’t. It apparently looks like I’ve not handled the db connection error, even though I’ve not handle it. I want to know,

  • How can I make sure my app.js and cluster.js always restarts if it crashes?

  • Is there a Linux crontab can be place to check the certain ports are always running (i.e 3000 and 3013)? (If this a good idea, I appreciate if you could provide me the code, I’m not much familiar with Linux)

  • Or I can deploy another NodeJS api to check the certain services are running, but since my API’s real-time and catching certain amount of load; I’m not much happy do this?

Any help would be appreciate, Thanks in advance.

Advertisement

Answer

You can use monit https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-monit in your server to regular monitor your process, if your project crashes it restart it again and can even notify you. but in this you have to do some configuration in server as monit regularly monitors a port, if it dosent get any reply from thta port then it restarts it.

otherwise you can use forever module. Easy to install and easy to use-https://www.npmjs.com/package/forever it monitors and within 1 sec it restarts your application

Advertisement