Graceful shutdown with nodejs pm2 SIGINT not working

两盒软妹~` 提交于 2020-01-25 10:13:10

问题


Hello I Have an application server written in js and is running on windows. Furthermore I'm using pm2 module but I have noticed that when I start my application server via pm2 start index.js -i 8 and then I want to stop all pm2 processes via pm2 stop all the event listeners for SIGINT are completely ignored...

I should add that this works fine when used with plain node and not pm2. So I was wondering am I missing something obvious here? Bellow is a sample test source and pm2 docs

const http = require('http'); 
const port = 3000

const requestHandler = (request, response) => {
    response.end('Hello Node.js Server!'); 
}

const server = http.createServer(requestHandler);

server.listen(port, (err) => {
    if (err) {
        console.error(err);
    } else {
        console.log(`server is listening on ${port}`);
    } 
});

process.on('SIGINT', () => {

    console.log('NIX SIGINT!!!');

    process.exit(0);

});

process.on('message', (msg) => {

    if (msg == 'shutdown') {

            console.log('WIN SIGINT!!!');

            process.exit(0)

    } else {
        console.log('msg:', msg);
    }

});

Update:

Versions & OS

OS: Windows

pm2 version: ^4.2.1

Nodejs version: v12.14.1

pm2 logs

PM2      | App [grace:0] starting in -cluster mode-
PM2      | App [grace:0] online
0|grace  | server is listening on 3000
PM2      | Stopping app:grace id:0
PM2      | App name:grace id:0 disconnected
PM2      | App [grace:0] exited with code [1] via signal [SIGINT]
PM2      | pid=10820 msg=process killed

来源:https://stackoverflow.com/questions/59691598/graceful-shutdown-with-nodejs-pm2-sigint-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!