Created
January 17, 2019 09:27
-
-
Save tfiechowski/4a1ac1bdbc8e4ad1a31f7d098e2b7f4f to your computer and use it in GitHub Desktop.
NodeJS script running in cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require("child_process"); | |
const fs = require("fs"); | |
const cluster = require("cluster"); | |
const numCPUs = require("os").cpus().length; | |
const scriptFile = './script.sh'; | |
let tasks = []; | |
function runScript(arg) { | |
const script_process = spawn(scriptFile, [arg]); | |
script_process.stdout.on("data", msg => { | |
console.log(`[${process.env.TASK}]\t`, msg.toString()); | |
}); | |
script_process.stderr.on("data", error => { | |
console.log(`[${process.env.TASK}]\t`, error.toString()); | |
}); | |
return script_process; | |
} | |
function getNextTask() { | |
return tasks.shift(); | |
} | |
if (cluster.isMaster) { | |
// Fork workers. | |
const initialWorkers = Math.min(numCPUs, tasks.length); | |
for (let i = 0; i < initialWorkers; i++) { | |
const task = getNextTask(); | |
cluster.fork({ TASK: task }); | |
} | |
cluster.on("exit", (worker, code, signal) => { | |
if (tasks.length > 0) { | |
const task = getNextTask(); | |
cluster.fork({ TASK: task }); | |
} | |
}); | |
} else { | |
const task = process.env.TASK; | |
const task_process = runScript(task); | |
task_process.on("exit", process.exit); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment