Giter Club home page Giter Club logo

autocluster's Introduction

Autocluster

Simplify the semantics for setting up a cluster of node processes

Install

npm install autocluster

Use

Autocluster should only be used from a single module in a given process - if two modules running in the same process try to use Autocluster it will throw an Error.

Using Autocluster is fairly simple. Autocluster exports a function that you pass a cluster configuration to. Autocluster then builds a cluster matching that configuration. It looks something like this

var autocluster = require("../index.js"),
    http = require("http");

autocluster({
      master: "./lib/master-module.js",
      workers: [{
          type: "worker",
          number: -1,
          fork: {},
          context: {
            port: 8080
          },
          run: "./lib/worker.js"
      }]
});

Note: Each process, master or worker, gets its own copy of the configuration so that workers can access functions defined in the module. Changes to one process' configuration are not reflected in any of the other processes. It is advised that you make sure your configuration does not vary between processes.

Cluster Configuration

An Autocluster cluster configuration contains two top level keys

  • master - This should be function or a relative path to a module to be loaded in the context of the file in which the configuration is being passed to Autocluster. If a function is provided or the module exports a function, Autocluster will call it, passing in a single argument with two keys
    • conf - the active configuration
    • on - a method that lets you register event listeners on the cluster's master process.
  • workers - This may be a string, a function, a number, an object, or an array. In each case it is converted to a list containing worker definition objects.
    • A string will be treated as a value for that worker definition's run key - that is, a path to a module - and loaded appropriately. Default values for all other keys will be used.
    • A function will be treated as a value for that worker definition's run key. Default values for all other keys will be used.
    • A number will be treated as the value for that worker definition's number key. Default values for all other keys will be used.

Worker Definitions

A worker definition defines these keys

  • type - String, an arbitrary name describing the type of worker process. Default: "worker".
  • number - Integer, Indicates the number of worker processes to generate. Positive numbers are an explicit count - exactly that many will be generated. Zero means use the cpu count. Negative values are subtracted from the number of cpus available and the resulting number of processes are generated. Default: 0.
  • fork - Object, env to send to cluster.fork() for this child. Default: undefined.
  • context - Object, worker-type specific configuration to pass to the worker process when it comes online. Default: {}.
  • run - String or function, the actual functionality of the worker. It may be a function or path to a module to be required. If it resolves to a function, the function will be called and passed the context defined for this worker type, with the worker available as context.worker and the worker definition as context.worker.definition. Default: undefined.

Keep in mind that your calling module will be run for each process. Further, a module required for a worker definition will be run by that worker process. Therefore it is not neccesary to export a function from a module to use it to set up a worker process. However, if such a module does export a function it will be called.

autocluster's People

Contributors

amadsen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.