const cluster =require('cluster')
const os  = require('os');
const consola=require('consola')
const { cleanPath } = require('../utilities/utils');
const cpuCount = os.cpus().length




/**
 * 
 * @param {{name:string,i:number}} opts 
 */
function StartModeCluster(opts={
    name:"App",
    i:2
}) {
    console.log(`The total number of CPUs is ${cpuCount}`);
    console.log(`Primary pid=${process.pid}`);
    cluster.setupPrimary({
        exec: cleanPath(process.cwd() + "/bitev.config.js"),
    });


    opts.i>cpuCount ? opts.i=cpuCount :null

    for (let i = 0; i < opts.i; i++) {
        cluster.fork();
    }
    cluster.on("exit", (worker, code, signal) => {
        console.log(`App ${worker.process.pid} has been killed`);
        console.log("Starting another App");
        cluster.fork();
    });

}





module.exports={
    StartModeCluster
}