简体   繁体   English

NodeJS - 子节点进程?

[英]NodeJS - Child node process?

I'm using NodeJS to run a socket server (using socket.io).我正在使用 NodeJS 运行套接字服务器(使用 socket.io)。 When a client connects, I want am opening and running a module which does a bunch of stuff.当客户端连接时,我想打开并运行一个模块来做很多事情。 Even though I am careful to try and catch as much as possible, when this module throws an error, it obviously takes down the entire socket server with it.尽管我小心翼翼地尝试尽可能多地捕获,但当这个模块抛出错误时,它显然会用它来关闭整个套接字服务器。

Is there a way I can separate the two so if the connected clients module script fails, it doesn't necessarily take down the entire server?有没有办法可以将两者分开,所以如果连接的客户端模块脚本失败,它不一定会关闭整个服务器?

I'm assuming this is what child process is for, but the documentation doesn't mention starting other node instances.我假设这是子进程的用途,但文档没有提到启动其他节点实例。

I'd obviously need to kill the process if the client disconnected too.如果客户端也断开连接,我显然需要终止该进程。

I'm assuming these modules you're talking about are JS code.我假设您正在谈论的这些模块是 JS 代码。 If so, you might want to try the vm module .如果是这样,您可能想尝试vm 模块 This lets you run code in a separate context, and also gives you the ability to do a try / catch around execution of the specific code.这使您可以在单独的上下文中运行代码,并且还使您能够在执行特定代码时进行try / catch

You can run node as a separate process and watch the data go by using spawn , then watch the stderr/stdout/exit events to track any progress.您可以将节点作为单独的进程运行并使用spawn观察数据 go ,然后观察 stderr/stdout/exit 事件以跟踪任何进度。 Then kill can be used to kill the process if the client disconnects.然后如果客户端断开连接,可以使用kill来终止进程。 You're going to have to map clients and spawned processes though so their disconnect event will trigger the process close properly.您将不得不使用 map 客户端和衍生进程,因此它们的断开连接事件将触发进程正确关闭。

Finally the uncaughtException event can be used as a "catch-all" for any missed exceptions, making it so that the server doesn't get completely killed (signals are a bit of an exception of course).最后, uncaughtException事件可以用作任何遗漏异常的“包罗万象”,这样服务器就不会被完全杀死(当然,信号有点例外)。

As the other poster noted, you could leverage the 'vm' module, but as you might be able to tell from the rest of the response, doing so adds significant complexity.正如另一张海报指出的那样,您可以利用“vm”模块,但正如您可以从响应的 rest 中看出的那样,这样做会增加显着的复杂性。

Also, from the 'vm' doc:此外,来自“vm”文档:

Note that running untrusted code is a tricky business requiring great care. 
To prevent accidental global variable leakage, vm.runInNewContext is quite 
useful, but safely running untrusted code requires a separate process.

While I'm sure you could run a new nodejs instance in a child process, the best practice here is to understand where your application can and will fail, and then program defensively to handle all possible error conditions.虽然我确信您可以在子进程中运行一个新的 nodejs 实例,但这里的最佳实践是了解您的应用程序可能会失败和将失败的地方,然后进行防御性编程以处理所有可能的错误情况。

If some part of your code "take(s) down the entire... server", then you really to understand why this occurred and solve that problem rather than rely on another process to shield you from the work required to design and build a production-quality service.如果您的代码的某些部分“占用了整个...服务器”,那么您真的要理解为什么会发生这种情况并解决该问题,而不是依赖另一个过程来保护您免受设计和构建所需的工作生产优质服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM