moleculerjs/moleculer-web

View on GitHub
benchmarks/cluster/index.js

Summary

Maintainability
A
0 mins
Test Coverage
/* eslint-disable no-console */

"use strict";

const
    os = require("os"),
    cluster = require("cluster"),
    stopSignals = [
        "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT",
        "SIGBUS", "SIGFPE", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGTERM"
    ],
    production = true; //process.env.NODE_ENV == "production";

let stopping = false;

cluster.on("disconnect", function(worker) {
    if (production) {
        if (!stopping) {
            cluster.fork();
        }
    } else
        process.exit(1);
});

if (cluster.isMaster) {
    const workerCount = process.env.NODE_CLUSTER_WORKERS || os.cpus().length;
    console.log(`Starting ${workerCount} workers...`);
    for (let i = 0; i < workerCount; i++) {
        let worker = cluster.fork();
    }

    if (production) {
        stopSignals.forEach(function(signal) {
            process.on(signal, function() {
                console.log(`Got ${signal}, stopping workers...`);
                stopping = true;
                cluster.disconnect(function() {
                    console.log("All workers stopped, exiting.");
                    process.exit(0);
                });
            });
        });
    }
} else {
    let worker = cluster.worker;
    //console.log(worker);
    let hostname = os.hostname();
    worker.process.argv.push(hostname + "-worker-" + worker.id);

    require("./worker.js");

}

/**
 * Result on i7 4770K 32GB RAM Windows 10 x64
 *
 *     Throughtput: 21 220 req/sec
 */