The first world solution to spawning child workers
Installation
npm i controlled-cluster --save
Usage
The code below will run three processes in parallel. It is your responsibility to check the number of cpus available.
const controlledCluster = require('controlled-cluster');
const processes = [
(callback) => {
console.log("running child process");
setTimeout(() => {
//do your work here
let error = false;
callback(error)
}, 1000)
},
(callback) => {
console.log("running child process");
setTimeout(() => {
//do your work here
let error = false;
callback(error)
}, 1400)
},
(callback) => {
console.log("running child process")
setTimeout(() => {
//do your work here
let error = false;
callback(error)
}, 2000)
}
];
controlledCluster(processes, (err) => {
if (!err) {
console.log("They just toss us away like yesterday's jam.");
} else {
console.log(err);
console.log('Have you tried forcing an unexpected reboot?');
}
})