Skip to content

Instantly share code, notes, and snippets.

@tfiechowski
Created January 17, 2019 09:27
Show Gist options
  • Save tfiechowski/4a1ac1bdbc8e4ad1a31f7d098e2b7f4f to your computer and use it in GitHub Desktop.
Save tfiechowski/4a1ac1bdbc8e4ad1a31f7d098e2b7f4f to your computer and use it in GitHub Desktop.
NodeJS script running in cluster
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