I have this Nodejs app that runs in Elastic Beanstalk. I'd like to do some cleaning when the application exits (e.g new version is deployed, restarting the app server). To do so I'm listening to some events & signals, but none of them seems to be triggered. Here's the code from app.js:
process.on("SIGTERM", function() {
console.log("SIGTERM, clean");
process.exit(0);
});
process.on("exit", function(code) {
console.log("Process exiting with code " + code);
});
process.on("uncaughtException", function() {
console.log("Unhandled exception occurred);
process.exit(99);
});
When I restart the app through the management console, I don't see anything in the log, even though a new node process is created and the old one is killed.
If I manually kill the process I can see the log messages in the output. Does that mean Beanstalk-triggered events do not send kill signals? If so, how is it possible to do some graceful exit?
Edit: here's the result of more investigation. Beanstalk uses Upstart to manage the application. In /etc/init/nodejs, there's this line:
exec su -s /bin/sh -c 'PATH=$PATH:$NODE_HOME/bin $EB_NODE_COMMAND 2>&1' nodejs >> /var/log/nodejs/nodejs.log
Which runs npm start
, which starts the application using the package.json config. Here's the process tree I have:
root 29155 su -s /bin/sh -c PATH=$PATH:$NODE_HOME/bin $EB_NODE_COMMAND 2>&1 nodejs
nodejs 29156 \_ sh -c PATH=$PATH:$NODE_HOME/bin $EB_NODE_COMMAND 2>&1
nodejs 29157 \_ npm
nodejs 29168 \_ node app/app.js
initctl status nodejs
show the pid of the first process. So I guess Upstart sends a SIGTERM to this process, which does not forward it all the way to my process. Unfortunately I still have no idea how to fix that.
Unfortunately, AWS support confirmed that there is no workaround for now. A feature request has been created for the Beanstalk dev team, but who knows when that gets implemented.
There seems to be an undocumented way to "intercept" the SIGKILL by adding a script to the hooks/appdeploy
folder:
https://forums.aws.amazon.com/thread.jspa?messageID=493887
This is pretty much undocumented and unsupported by the AWS support pointed me to this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With