我怎么能故意崩溃我的node.js服务器?

时间:2020-11-28 17:09:06

How can I purposefully crash my node.js server? (I want to do this when certain fatal errors occur so they are brought to my attention immediately to be fixed)

我怎么能故意崩溃我的node.js服务器? (我想在发生某些致命错误时这样做,以便立即引起我的注意以便修复)

I have seen this post here but I do not want to use a C module or run an endless loop (since my app is running on a server where I pay for the amount of CPU time I use) which means it will be very costly and difficult to max out the CPU.

我在这里看过这篇文章,但我不想使用C模块或运行无限循环(因为我的应用程序运行在服务器上,我支付我使用的CPU时间量),这意味着它将非常昂贵,很难最大化CPU。

I have tried using process.exit and process.abort but that only closes the module it is called from.

我尝试过使用process.exit和process.abort,但只关闭了调用它的模块。

For example my server is started by calling node main.js and it requires several custom modules I have written in other files. If process.exit or process.abort is called in one of those other files then it will close anything happening in those other files correctly but it will not close the entire node server and main.js.

例如,我的服务器是通过调用节点main.js启动的,它需要我在其他文件中编写的几个自定义模块。如果在其中一个文件中调用process.exit或process.abort,那么它将正确关闭那些其他文件中发生的任何事情,但它不会关闭整个节点服务器和main.js.

Here is a simplified example of what is happening in code:

以下是代码中发生的事情的简化示例:

//crashServer.js
var exampleVar;

module.exports = function(){
    if (!exampleVar){
        var err = new Error("An error has occured");
        Error.captureStackTrace(err);
        //error logging code
        console.log("An error has occured and the server will now crash.");
        process.exit();
    }
};

//main.js
var crashServer = require("./crashServer.js");

crashServer();
while (true){
    console.log("Server still running");
}

If I run

如果我跑

node main.js

then it will print out server still running indefinitely.

那么它将打印出仍然无限期运行的服务器。

If I remove the while true loop it will simply print out "An error has occured and the server will now crash." and then exit (since there is no more code to run). This proves the process.exit command is being run and yet the while (true) loop still runs.

如果我删除while true循环,它将简单地打印出“出现错误,服务器现在将崩溃”。然后退出(因为没有更多的代码可以运行)。这证明了process.exit命令正在运行,而while(true)循环仍在运行。

Any other ways I can accomplish this?

我可以通过其他方式完成此任务吗?

1 个解决方案

#1


2  

Infinite loops in Javascript are simply evil as they don't allow the event loop to do any of it's normal shut-down processing, some of which is required in order to allow some Javascript events to be processed as part of the shutdown process.

Javascript中的无限循环简直是邪恶的,因为它们不允许事件循环执行任何正常的关闭处理,其中一些是为了允许一些Javascript事件作为关闭过程的一部分进行处理所必需的。

If you replace this infinite loop:

如果你替换这个无限循环:

while (true){
    console.log("Server still running");
}

with this:

setInterval(function() {
    console.log("server still running");
}, 1000);

Then your server should shut-down.

然后你的服务器应该关闭。

#1


2  

Infinite loops in Javascript are simply evil as they don't allow the event loop to do any of it's normal shut-down processing, some of which is required in order to allow some Javascript events to be processed as part of the shutdown process.

Javascript中的无限循环简直是邪恶的,因为它们不允许事件循环执行任何正常的关闭处理,其中一些是为了允许一些Javascript事件作为关闭过程的一部分进行处理所必需的。

If you replace this infinite loop:

如果你替换这个无限循环:

while (true){
    console.log("Server still running");
}

with this:

setInterval(function() {
    console.log("server still running");
}, 1000);

Then your server should shut-down.

然后你的服务器应该关闭。