Debug Node, what is keeping it running?
I have a script written for Node.Js that collects a load of data, processes it, saves and then finishes. Unfortunately something I've done that is preventing the script from finishing and node closing, instead it remains open.
I'll probably just work my way through the changes I've made and try to track it down, but... Is there an easy way to debug node and find out what code / event / callback / connection or whatever, that it's waiting for?
I've had a look at node-inspector but I couldn't figure out how to track down anything keeping it open. Any advice?
You can run your node.js program using wtfnode or add require('wtfnode').init();
on the first line or your program. It will then print the list of all the things keeping node open when you press ctrl+c to kill the process.
When I've had this happen it's most often something like mongodb, amqp etc. That is database connections, network connections. Make sure you close those once you're done processing.
I guess you could use lsof -p <PID>
and see what that says. I can see both mongodb and amqp in there.
At the point where you expect it to close add
console.log(process._getActiveHandles());
You can also add a handler
process.on('SIGINT', () => console.log(process._getActiveHandles()) );
So if node does not close correctly, Ctrl + C will tell you why.
链接地址: http://www.djcxy.com/p/52690.html下一篇: 调试节点,什么使它保持运行?