【问题标题】:What happens to the cluster master when all Node workers die?当所有 Node 工作人员都死亡时,集群主服务器会发生什么?
【发布时间】:2018-12-15 09:11:15
【问题描述】:

所以我一直在尝试了解 Node 集群的工作原理,并且我有以下代码:

const cluster = require('cluster')
const os = require('os')
const express = require('express')
const app = express()

let cpus

// Check to see if the master process is running this
if (cluster.isMaster) {
    console.log(`Master Process: ${process.pid} is running.`)

    // Get the number of CPU's and fork that many workers
    cpus = os.cpus().length
    for(let i = 0; i < cpus; i++) {
        console.log(`Forking process #${i+1}`)
        cluster.fork()
    }

    // If worker process is killed log it
    cluster.on('exit', (worker) => {
        console.log(`Worker ${worker.process.pid} died.`)
    })

    // Once a worker is connected output they are online
    cluster.on('online', (worker) => {
        console.log(`Worker ${worker.process.pid} is online.`)
    })

    process.on('SIGINT', () => {
        for(const id in cluster.workers) {
            cluster.workers[id].kill('SIGINT')
        }
    })
} else {
    // Each worker should listen on port 3000
    app.listen(3000)
}

当我运行它然后以Ctrl+C 退出时,我收到了这个控制台输出,我的主进程也死了。

Master Process: 19988 is running.
Forking process #1
Forking process #2
Forking process #3
Forking process #4
Worker 14684 is online.
Worker 14672 is online.
Worker 12520 is online.
Worker 3276 is online.
Worker 3276 died.
Worker 12520 died.
Worker 14672 died.
Worker 14684 died.

所以它正在做我想做的事情,但是当我拥有SIGINT 所做的只是杀死工人时,为什么主进程也会死掉?

【问题讨论】:

    标签: javascript node.js node-cluster


    【解决方案1】:

    这有助于思考任何 Node 进程停止的原因。

    例如,如果你的 Node 程序是一行:

    console.log("Starting…stoping")
    

    它将显示文本并退出。但这仍然存在:

    setInterval(() => console.log("still going"), 1000)
    

    只要有诸如计时器或 I/O 事件之类的事情需要处理,Node 进程就会在事件循环中不断循环。一旦没有,循环停止并且过程结束。

    因此,在您的示例中,子进程保持活动状态,因为正在侦听来自 Express 的打开 I/O 连接。主集群处于活动状态,因为它正在等待来自子节点的 I/O 事件。当孩子们死后,轮询队列中没有任何东西可供主人做,所以它退出。 (这里有更多详细信息:https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

    您可以通过执行以下操作来保持主集群运行:

    if (cluster.isMaster) {
        console.log(`Master Process: ${process.pid} is running.`)
        setInterval(() => {
            console.log('Master still here')
        }, 1000)
       // ... etc
      }
    

    即使在孩子们离开后,主人也会继续滴答作响(停下来会很烦人,因为你正在捕捉 SIGINT)。

    【讨论】:

    • 谢谢!这对我来说更有意义了。
    猜你喜欢
    • 2019-05-26
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多