How to use cluster - 10 common examples

To help you get started, we’ve selected a few cluster examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Akryum / vue-cli-plugin-ssr / lib / server.js View on Github external
return new Promise(async (resolve, reject) => {
    const clustered = process.env.NODE_ENV === 'production' && config.clustered

    // If the clustering mode is enabled and we are in the master process, create one worker per CPU core
    if (cluster.isMaster && clustered) {
      const cpus = require('os').cpus().length
      console.log(`[${new Date().toTimeString().split(' ')[0]}] Setting up clusters for ${cpus} cores`)
      for (let i = 0; i < cpus; i += 1) {
        cluster.fork()
      }

      // Notify if new worker is created
      cluster.on('online', (worker) => {
        console.log(`[${new Date().toTimeString().split(' ')[0]}] Worker ${worker.id} is online and listening on ${host}:${port}`)
      })

      // If a worker dies, create a new one to keep the performance steady
      cluster.on('exit', (worker, code, signal) => {
        console.log(`[${new Date().toTimeString().split(' ')[0]}] Worker ${worker.id} exited with code/signal ${code || signal}, respawning...`)
        cluster.fork()
      })

      // If the clustering mode is disabled or we are in a worker process, setup the server
    } else {
      const app = express()

      await applyApp(app)

      app.listen(port, host, err => {
github nodules / luster / lib / master.js View on Github external
async _run() {
        await this.whenInitialized();

        cluster.setupMaster(this._masterOpts);

        // TODO maybe run this after starting waitForAllWorkers
        this.forEach(worker => worker.run());

        await this.waitForAllWorkers('worker ready');

        this.emit('running');
    }
github sogehige / sogeBot / src / bot / widgets / customvariables.ts View on Github external
constructor() {
    super();
    this.addWidget('customvariables', 'widget-title-customvariables', 'fas fa-dollar-sign');

    require('cluster').on('message', (worker, message) => {
      if (message.type !== 'widget_custom_variables') {
        return;
      }
      this.emit(message.emit); // send update to widget
    });
  }
github sogehige / sogeBot / src / bot / widgets / custom_variables.js View on Github external
constructor () {
    global.panel.addWidget('customvariables', 'widget-title-customvariables', 'fas fa-dollar-sign')
    this.sockets()

    require('cluster').on('message', (worker, message) => {
      if (message.type !== 'widget_custom_variables') return
      global.panel.io.of('/widgets/customVariables').emit(message.emit) // send update to widget
    })
  }
github MarcusCemes / responsive-image-builder / lib / controller.js View on Github external
this.run = async function() {

    cluster.on('message', handleWorker);
    const worker_count = this.workers.length;
    
    for (let i = 0; i !== worker_count; i++) {
      this.workers[i].send({
        code: 'TASK',
        data: this.image_list[this.pointer]
      });
      this.pointer++;
    }

    // Wait for all tasks to complete
    return new Promise(resolve => cluster.once('complete', resolve));
  }
github thinkjs / think-cluster / lib / master.js View on Github external
captureReloadSignal() {
    const signal = this.options.reloadSignal;
    const reloadWorkers = () => {
      util.getAliveWorkers().forEach(worker => worker.send(util.THINK_RELOAD_SIGNAL));
    };
    if (signal) process.on(signal, reloadWorkers);

    // if receive message `think-cluster-reload-workers` from worker, restart all workers
    cluster.on('message', (worker, message) => {
      if (message !== 'think-cluster-reload-workers') return;
      reloadWorkers();
    });
  }
  /**
github instana / nodejs-sensor / load-test / src / index.js View on Github external
function initMaster() {
  console.log('Master ' + process.pid + ' is running');

  for (var i = 0; i < config.app.workers; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(worker) {
    console.log('worker ' + worker.process.pid + ' died');
  });
}
github remy / nodemon / oldtests / fixtures / cluster / index.js View on Github external
require('os').cpus().forEach(function() {
    var worker = cluster.fork();
    worker.on('online', function(msg) {
      console.log('worker', worker.process.pid, 'online');
      workers[worker.process.pid] = worker;
    });
  });
  cluster.on('exit', function(worker, code, signal) {
github remy / nodemon / oldtests / fixtures / cluster / index-0.6.x.js View on Github external
require('os').cpus().forEach(function() {
    var worker = cluster.fork();
    worker.on('message', function(msg) {
      console.log('worker', worker.pid, 'online');
      workers[worker.pid] = worker;
    });
  });
github imjacobclark / cors-container / app.js View on Github external
function clusterApp(){
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('exit', worker => console.error(`worker ${worker.process.pid} died`));

    console.info("cors-container listening on port 3000 with " + numCPUs + " threads.")
}

cluster

extensible multi-core server manager

MIT
Latest version published 13 years ago

Package Health Score

56 / 100
Full package analysis