How to restart if NodeJS API service failed?

时间:2017-12-18 05:26:06

标签: node.js linux cluster-computing prometheus

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.

2 个答案:

答案 0 :(得分:1)

您可以在服务器中使用monit https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-monit来定期监控您的流程,如果您的项目崩溃,它会再次重启,甚至可以通知您。但是在这个你必须在服务器中进行一些配置,因为monit定期监视一个端口,如果它从该端口得到任何回复,那么它会重新启动它。

否则你可以使用forever模块。易于安装且易于使用 - https://www.npmjs.com/package/forever 它监控并在1秒内重新启动您的应用程序

答案 1 :(得分:0)

我最近发现,如果工作人员事件已经死亡/关闭,我们可以听取工作人员事件并重新启动。

以下是代码:

'use strict';

const cluster = require('cluster');
var express = require('express');
const metricsServer = express();
var os = require('os');

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

 cluster.on(
        "exit",
        function handleExit( worker, code, signal ) {

            console.log(  "Worker has died.", worker.process.pid );
            console.log(  "Death was suicide:", worker.exitedAfterDisconnect );

            // If a Worker was terminated accidentally (such as by an uncaught
            // exception), then we can try to restart it.
            if ( ! worker.exitedAfterDisconnect ) {

                var worker = cluster.fork();
                // CAUTION: If the Worker dies immediately, perhaps due to a bug in the
                // code, you can run [from what I have READ] into rapid CPU consumption
                // as Master continually tries to create new Workers.

            }

        }
    );

} else {
 require('./app.js');
}