问题
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