在node.js上调试正常关机 [英] Debugging a graceful shutdown on node.js

查看:152
本文介绍了在node.js上调试正常关机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的app.js中包含以下代码:

  process.stdin.resume(); //因此程序不会立即关闭

//在应用程序关闭
时执行某些操作process.on('exit',exitHandler.bind(null,{exit:true,event: exit} ));

//捕获未捕获的异常
process.on(‘uncaughtException’,exitHandler.bind(null,{exit:false,event: uncaughtException})));

我阅读了所有有关正常关闭node.js服务器的信息,但找不到任何有关如何调试它的事情。



问题是,当您单击调试控制台中的停止按钮时,IDE必须终止/杀死节点进程。 p>

有人对我如何调试优雅的关机代码有想法吗?
关闭调试会话时,调试模式永远不会在 exitHandler 内的断点处停止。



m使用WebStorm,但我读到其他IDE也可能发生同样的情况。



谢谢

解决方案

嗯,没有魔术。要获得'exit'事件,您的代码最终必须命中 process.exit()代码,并获得'uncaughtException'您需要引发未处理的异常。



也许有一种疯狂的方式可以触发此代码而不创建实际事件,但是您应该坚持正常的程序生命周期,因为这就是产品在生产中的工作方式-绝对是您要测试的内容。



要练习:如果您只想看一次如果您是正确的,则只需将适当的代码手动添加到您的应用中,以便在加载后立即触发该代码:

  setTimeout(function(){
process.exit(1);
//或抛出新的Error( Uncaught exception);
},2000);

调试,然后删除。



如果您打算以某种方式将其集成到一些测试中,您需要在代码内保持以下行为,并添加一种触发它的方法:

  .... // //加载应用程序时
if(process.env.NODE_TEST_TRIGGER_PROCESS_EXIT){
process.exit(1);
}

并添加执行方案以使用适当的选项运行它:

  $ NODE_TEST_TRIGGER_PROCESS_EXIT =真实节点myapp.js 

UPD:如果您真的不在乎其他事件,请检查其他信号: https://nodejs.org/api/process.html#process_signal_events 也许应用会收到SIGINT或SIGTERM。但是,如果是SIGKILL,您仍然无法捕获,因此您必须自己创建活动。


I have the following code in my app.js:

process.stdin.resume();//so the program will not close instantly

//do something when app is closing
process.on('exit',exitHandler.bind(null,{exit:true,event: "exit"}));

//catches uncaught exceptions
process.on('uncaughtException',exitHandler.bind(null, {exit:false,event: "uncaughtException"}));

I read all that there is to read about gracefully shutting down node.js server but couldn't find anything about how to debug it.

The problem is that must IDE's terminated/kill the node process when you click on the "Stop" button in the debug console.

Does anyone have an idea on how can i debug my graceful shutdown code? the debug mode never stops on breakpoints inside exitHandler when shutting down the debug session.

I'm using WebStorm, but i read that the same probably happens on other IDEs as well.

Thanks

解决方案

Well, there's no magic. To get 'exit' event your code must eventually hit process.exit() code, and to get 'uncaughtException' you need to raise unhandled exception.

Maybe there is an insane way to trigger this code without creating actual events, but you should stick to normal program lifecycle, because that's how things work in production - and that's definitely what you want to test.

To practice: If you want to see once if you're correct, you can just manually add proper code just to your app, so it will be triggered once it is loaded:

setTimeout(function() {
    process.exit(1);
    // or throw new Error("Uncaught exception");
}, 2000);

Debug, then remove.

If you're planning to somehow integrate this into some tests, you need to keep following behavior within code, and add a way to trigger it:

.... // when app is loaded
if (process.env.NODE_TEST_TRIGGER_PROCESS_EXIT) {
    process.exit(1);
}

And add an execution scenario to run it with proper option:

$ NODE_TEST_TRIGGER_PROCESS_EXIT=true node myapp.js

UPD: If you really don't care about different events, check other signals: https://nodejs.org/api/process.html#process_signal_events Maybe app will receive SIGINT or SIGTERM. But if it's SIGKILL, you still can't catch it, so you have to create event yourself.

这篇关于在node.js上调试正常关机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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