Skip to content

Instantly share code, notes, and snippets.

@JonLevin25
Last active December 10, 2022 19:23
Show Gist options
  • Save JonLevin25/0990a52a6c407fe2583b169e5efeba27 to your computer and use it in GitHub Desktop.
Save JonLevin25/0990a52a6c407fe2583b169e5efeba27 to your computer and use it in GitHub Desktop.
NodeJS Run multiple processes from simple functions
const cluster = require('cluster');
/**
* @param {Function[]} workloads - simple paramaterless functions
* @param {{ restartOnExit: bool? }} opts
*/
function runWorkloads(workloads, opts) {
const restartOnExit = opts?.restartOnExit ?? false;
const workloadLookup = Object.fromEntries(
Array.from(new Set(workloads))
.map(fn => [fn.name, fn])
);
if (cluster.isPrimary) {
console.log(`Enter from primary`);
workloads.map(fn => fn.name).forEach(workloadName => {
const worker = startWorker(workloadName);
});
} else {
process.on('message', msg => {
const workloadName = msg;
const workloadFn = workloadLookup[msg]; // function name sent
logStartedWork(workloadName)
workloadFn();
})
}
function startWorker(workloadName) {
/**
* @type {cluster.Worker} worker
*/
const worker = cluster.fork();
console.log(`Forked. workload: ${workloadName}`);
worker.send(workloadName);
if (restartOnExit) worker.on('exit', () => startWorker(workloadName));
return worker;
}
function logStartedWork(workloadName){
console.log(`Worker process id ${cluster.worker.id} starting workload (${workloadName})..`);
}
}
// work function exampless
function countTo10Slow(){
let i = 1;
const interval = setInterval(() => {
if (i >= 10) clearInterval(interval);
console.log(i++);
}, 1000);
}
function throwAfter5Sec(){
setTimeout(()=> {throw `Worker ${cluster.worker.id} threw..`}, 5000);
}
// define and run our workloads (simple paramaterless functions, can repeat same workload more than once)
const workloads = [
countTo10Slow,
throwAfter5Sec,
countTo10Slow
];
runWorkloads(workloads, {restartOnExit: true}); // restartOnExit is useful for restarting if any exceptions occured
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment