繁体   English   中英

如何防止 Node.js 12 工作线程立即终止?

[英]How to Prevent a Node.js 12 worker Thread from terminating immediately?

我有一个运行 Express 服务器的 Node.js 应用程序和一个定期执行某些操作的工作线程

当服务器停止时,我需要清理与工作线程启动时打开的外部系统的连接。

我试图在进程上为 SIGTERM 和 SIGINT 信号添加处理程序,但这不起作用,工作线程中的处理程序 function 未被调用,当父进程收到 SIGINT 或 SIGTERM 时,它立即退出并退出代码 1,虽然父进程也有一个处理程序,这些处理程序确实被调用。

这是一个重现问题的简单代码示例:

开始.js

const http = require("http");
const express = require("express");
const path = require("path");
const { Worker } = require("worker_threads");

let myWorker = null;

process.on("SIGTERM", stop);
process.on("SIGINT", stop);

const app = express();
const server = http.Server(app);

myWorker = new Worker(path.join(__dirname, "./worker.js"));

myWorker.on("exit", code => console.info(`Worker exited with code ${code}`));

server.listen(3000);

function stop() {
  console.log("Main process: stop()");
  process.exit(0);
}

worker.js

process.on("SIGTERM", stop);
process.on("SIGINT", stop);

setInterval(() => console.log("beep"), 1000);

function stop() {
  console.log("Worker process: stop()");
  process.exit(0);
}

当我启动soWorker.js时,当我用 CTRL+C 中断进程时,我在控制台上得到了这个 output:

╰─➤  node start.js
beep
beep
beep
^CMain process: stop()
Worker exited with code 1

期望发生的是这样的:

╰─➤  node start.js
beep
beep
beep
^CMain process: stop()
Worker process: stop()
Worker exited with code 0

我希望调用工作线程中的stop function,这样我就可以用它来关闭与外部系统的连接,然后优雅地退出工作线程进程并返回代码 0。

相反,工作线程以代码 1 退出, 这是工作线程停止时的默认值

我的问题:

当主进程终止时,如何在我的工作线程中执行一些清理代码?

我是否采用上述方法走在正确的轨道上?

我主要是在这里吐痰,但我认为这会让你走上正确的轨道:

添加到 start.js

 process.on('SIGTERM', cleanup); process.on('SIGINT', cleanup); myWorker.once('exit', stop); function cleanup() { myWorker.postMessage('cleanup'); }

worker.js

 const { parentPort } = require('worker_threads'); parentPort.on('message', (value) => { if (value === 'cleanup') { cleanup(); //then process.exit(0); } });

以下解决方案应该有效 -

  • 在工作线程中订阅主线程message事件
  • 在主线程中订阅SIGTERM (或其他)
  • 当你在主线程中捕捉到信号时,向工作线程发送一条消息,请求它清理并退出。 ** 暂时不要退出主线程**。
  • 在主线程中通过订阅exit事件等待工作线程停止。
  • 当工作线程停止时,退出主线程。

暂无
暂无

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

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