npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

autocluster

v0.0.2

Published

Simplify the semantics for setting up a cluster of node processes

Downloads

2

Readme

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.