Node.js从PM2获取SIGINT [英] Node.js getting SIGINT from pm2

查看:274
本文介绍了Node.js从PM2获取SIGINT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pm2将节点应用作为服务运行.

I'm trying to use pm2 to run my node app as a service.

现在,启动和停止该应用程序的工作正常.但是,我想进行正常关机.

Right now, starting and stopping the app works. However, I want to do a graceful shutdown.

我的应用程序已经在侦听SIGINT,关闭服务器,然后退出该进程.但是,尝试放入pm2发送SIGINT只会导致应用重新启动,就像pm2被杀死并重新启动一样.

My app already listens for SIGINT, shutdowns the server and then exits the process. However, trying to put pm2 to send the SIGINT, just causes the app to restart, like if pm2 was killing and starting it again.

这是我创建流程的方式:

This is how I create the process:

pm2启动server.js --name ProcessName --silent --kill-timeout 3000

pm2 start server.js --name ProcessName --silent --kill-timeout 3000

这是我的应用程序用于监听SIGINT的代码:

Here's my app's code for listening the SIGINT:

process.on("SIGINT", function () {
    //graceful shutdown
    server.end().then(() => {
        process.exit();
    }).catch((err) => {
        console.error(err);
    });

});

然后使用pm2关闭应用程序,我正在运行:

Then to shutdown the app using pm2, I'm running:

pm2 sendSignal SIGINT ProcessName

pm2 sendSignal SIGINT ProcessName

再次,重新启动应用程序.

Which, again, restarts the app.

在阅读pm2文档后,我发现pm2还将向应用程序发送shutdown事件,因此我添加了:

Reading over pm2 docs, I found that pm2 will also send a shutdown event to the app, so I added:

process.on('message', function(msg) {
    if (msg == 'shutdown') {
        server.end().then(() => {
            process.exit();
        }).catch((err) => {
            console.error(err);
        });
    }
});

哪个也不工作.

有什么办法解决这个问题吗?

Any idea how to solve this?

谢谢!

推荐答案

如果尚未解决...

根据您提供的信息,我假设您正在Windows上运行它.

Based on the information you provided, I assume you are running it on Windows.

您的应用无法捕获Windows上PM2发送的SIGINT.
shutdown消息也可以在Windows上使用,但只能通过gracefulReload命令发送.

Your app cannot catch SIGINT sent by PM2 on Windows.
shutdown message works on windows too, but it is sent only by gracefulReload command.

(更新)
这些不是完整的解决方案,但可能会有所帮助(希望...)

(update)
These are not complete solutions, but might be helpful (hopefully...)

sendSignal命令最终调用process.kill(),其中一些信号可能有效(尚未尝试).

sendSignal command calls process.kill() eventually, and some of these signals might work (haven't tried).

我还发现了以下方法.仅当关闭选项被关闭时,这才可以正常关闭进程而无需重新启动.
然后,如果发生意外,您的集群将不会重新加载,因此虽然不是您想要的...

I also found the below method. This can gracefully shutdown a process without restarting only if autorestart option is turned off.
And then your clusters will not reload in case of an accident, so it might not be what you want though...

pm2可让您发送自定义消息(

pm2 lets you send a custom message (reference).
Put the code below in a new file:

var pm2 = require('pm2');
var id = process.argv[2];

pm2.connect(() => {
  pm2.sendDataToProcessId({
    type: 'shutdown',
    data:{some: 'data'},
    id: id,
    topic: 'some topic'
  }, (err, res) => {
    console.log('message sent');
    pm2.disconnect();

    if(err) throw err;
  })
});

按如下所示修改侦听shutdown消息的部分:

Modify the part that listens the shutdown message like below:

process.on('message', function(msg){
  if(msg == 'shutdown' || msg.type == 'shutdown'){
    // code to clean up
  }
});

并使用要关闭的集群的 id node运行第一个文件作为参数.

And run the first file with node with id of the cluster you want to shutdown as an argument.

在这种情况下额外使用msg.type == 'shutdown'的原因是pm2.sendDataToProcessId()要求参数是具有这些键的对象,并且不接受简单的shutdown字符串.

The reason for extra msg.type == 'shutdown' in the condition is that pm2.sendDataToProcessId() requires the argument to be an object with those keys, and does not accept simple shutdown string.

这篇关于Node.js从PM2获取SIGINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆