Search code examples
node.jsworker

node cluster fork executes code too often


I have jobs (functions) that I want to execute in separate threads. I have therefore created a utility function to do so:

import {cpus} from "os"
import cluster from 'cluster'

export default function runInThread(func: Function, useAllCores = false, numThreads = useAllCores ? cpus().length : 1) {
    if(cluster.isMaster) {
        for(let i = 0; i < numThreads; i++) {
            cluster.fork()
        }
    } else {
        func()
    }
}

then I want to execute the jobs like this:

import runInThread from "../helpers/threading"
import job1 from "./job1"
import job2 from "./job2"

let jobs = [
    job1, 
    job2
]

jobs.forEach(job => runInThread(job))

The example jobs are:

export default function job1() {
    console.log('job1')
}

export default function job2() {
    console.log('job2')
}

Unfortunately they seem to get executed twice, since the output is:

job1
job1
job2
job2

So it seems that every job gets forked twice...?


Solution

  • Main idea - each thread (fork) like host for certain function. In example this is just function name, but in real work you can generate some id for function and pass it to worker as env variable.

    import {cpus} from "os"
    import cluster from 'cluster'
    
    export default function runInThread(func: Function, useAllCores = false, numThreads = useAllCores ? cpus().length : 1) {
        if(cluster.isMaster) {
            for(let i = 0; i < numThreads; i++) {
                cluster.fork({FUNC_ID: func.name})
            }
        } else {
           if (process.env.FUNC_ID === func.name) {
             func()
           }
        }
    }