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

nanny

v1.0.1

Published

Scaffolding for node cluster

Downloads

10

Readme

Nanny

In some sense, a nanny watches kids. This module provides a cluster supervisor. A cluster is a group of worker processes that share a server or servers. The cluster supervisor:

  • maintains a pool of workers,
  • monitors the health of the workers including event loop responsiveness and memory consumption,
  • restarts workers if they stop,
  • stops workers if they fail a health check,
  • distributes incoming connections to the cluster workers

The cluster supervisor manages workers and load balancers. This package provides a round robin load balancer. Each connection will go to the least recently connected worker.

Worker processes do not require any special programming to participate in a cluster. The worker will be loaded in proxy by a “thunk” worker that will establish communication with the cluster supervisor, subvert Node.js's networking stack, and execute the worker module. Any attempt to listen for incomming connections with a net, http, or https server will be intercepted and managed by the cluster.

Example

// launcher.js
var ClusterSupervisor = require("nanny");
var supervisor = new ClusterSupervisor({

    workerPath: path.join(__dirname, "worker.js"),
    workerArgv: [],             // Arguments to pass to your worker.
    workerCount: 8,             // Number of workers to spawn. Default is # cores.
    logicalIds: [5, 6, 7]       // Give workers an env variable
                                // "PROCESS_LOGICAL_ID" based on the values in
                                // the array and the order of the array.

    // Configure the supervisor's behavior:

    pulse: 1000,                // The interval at which workers will attempt
                                // to submit their memory usage and load statistics.
    unhealthyTimeout: 10e3,     // The maximum delay in miliseconds after an
                                // expected heart beat that a worker supervisor
                                // will tolerate before stopping a stalled
                                // worker.
    serverRestartDelay: 1000,   // The period that a load balancer will wait before
                                // attempting to restart a socket server.
    workerForceStopDelay: 1000, // The period that a worker supervisor will wait between
                                // a stop command and when it will
                                // automatically force stop with a kill signal.
                                // Set to Infinity to disable.
    workerRestartDelay: 1000,   // The period that a worker will wait between
                                // when a worker stops and when it will attempt
                                // to restart it.
    respawnWorkerCount: 1,      // When workers exit, retry spawning up to x
                                // times. -1 for infinite.

    // Override the child process parameters for each worker:

    execPath: '/usr/bin/node',  // An alternate Node.js binary to use to spawn
                                // worker processes.
    execArgv: [],               // An alternate set of command line arguments
                                // for Node.js to spawn the worker process.
    cwd: '/',                   // The working directory for each worker
                                // process.
    encoding: 'utf-8',          // Encoding for standard IO of the worker.
    silent: false,              // Pipes the worker standard IO into the
                                // supervisor process instead of inheriting the
                                // supervisor's own standard IO.

    // Introduce additional cluster supervisor initialization.
    // `this` is the cluster supervisor.
    initMaster: function() {
    },

    // Override the method used to create the worker environment.
    // `this` is a worker supervisor and `this.id` is its logical id.
    createEnvironment: function (logicalId) {
        return {
            PROCESS_LOGICAL_ID: logicalId
        };
    },

    // Override the health check method of the worker supervisor.
    // If this returns false, the worker process fails and will be stopped.
    // The health check will occur on the same interval as the worker's health
    // report (pulse) but may be offset by as much under normal conditions.
    isHealthy: function (health) {
        return health.memoryUsage.rss < 700e6;
    },

    // Override the logger for the cluster. By default, we create a debuglog
    // named 'nanny' visible with `NODE_DEBUG=nanny`.
    logger: {
        debug: console.log.bind(console),
        warn: console.warn.bind(console),
        error: console.error.bind(console),
        info: consolea.info.bind(console)
    }

});

supervisor.start();
supervisor.stop();
supervisor.inspect();
supervisor.countWorkers();
supervisor.countRunningWorkers();
supervisor.countActiveWorkers();
supervisor.countRunningLoadBalancers();
supervisor.countActiveLoadBalancers();
supervsior.forEachWorker(function (workerSupervisor) { });
supervisor.forEachLoadBalancer(function (loadBalancer) { });
// worker.js
process.title = 'worker';
// initialize worker stuff here...

execPath, execArgv, encoding, cwd, and silent are passed through to Node.js's own child_process.

The cluster supervisor, worker supervisors, and load balancers, are all event emitters.

The worker supervisor emits 'health' events when the child process reports its memory usage and load metrics.

Docs

var supervisor = new ClusterSupervisor(/*arguments*/)

ClusterSupervisor := (spec: SupervisorSpec) => void

This module abstracts logic for spawning workers via node cluster.

Installation

npm install nanny

Tests

npm test

Contributors

  • tomuber
  • jakev
  • kriskowal

MIT Licensed