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

recluster

v1.0.0

Published

Clustering library with support for zero-downtime reloading

Downloads

7,378

Readme

recluster

Clustering library with support for zero-downtime reloading

usage

If server.js is your regular http server (e.g. express), create cluster.js and add:

var recluster = require('recluster'),
    path = require('path');

var cluster = recluster(path.join(__dirname, 'server.js'));
cluster.run();

process.on('SIGUSR2', function() {
    console.log('Got SIGUSR2, reloading cluster...');
    cluster.reload();
});

console.log("spawned cluster, kill -s SIGUSR2", process.pid, "to reload");

then run it

node cluster.js

To hot-reload the server, simply run

kill -s SIGUSR2 <cluster_pid>

To find out which of the N (= number of cores by default) worker instances you're running from inside server.js, you can use

process.env.WORKER_ID

which is zero-based i.e. 0 <= WORKER_ID < N

options

var cluster = recluster(file, opt)

where

file

Absolute path to the module that defines the server

opt.workers

Number of active workers (default = cores)

opt.timeout

Timeout to kill old workers after reload (seconds).

Defaults to 1 second in development, 1 hour in production.

opt.respawn

Minimum time between worker respawns when workers die (seconds)

opt.backoff

Maximum respawn time (reached via exponential backoff). Set to 0 or undefined to disable exponential backoff.

opt.readyWhen

Use 'listening' for servers (e.g. for express/connect http servers) and 'started' for workers that are immediately ready.

If you want to manually tell recluster when the worker is ready to replace older workers you can use {readyWhen: 'ready'}. Then, to signal readiness from the worker use process.send({cmd: 'ready'})

opt.args

Array of arguments to pass to the worker

opt.log

Log various events to stdout. Currently only 'respawns' is supported. Default: {respawns: true}

opt.logger

Which logger to use. Requires a console-compatible log method Default: console

cluster

The returned object has the following methods:

cluster.run

Starts the cluster by running child processes

cluster.reload(cb)

Hot-reloads new code. some of the children will remain active for opt.timeout seconds after reload

cluster.terminate(cb)

Terminates the entire cluster and removes all listeners.

cluster.activeWorkers()

Returns a hash of all worker slots (0 <= WORKER_ID < N). If a worker isn't available at that slot, the value in the hash is null or undefined. Otherwise, the value will be a worker object that is ready to serve requests.

cluster.workers()

Returns an array of all the workers, including those that are not yet ready or those that will be replaced.

worker cleanup

A server worker can gracefully exit by cleaning up in the 'close' event of its server:

server.on('close', function() {
    // cleanup
});

Non-server workers can listen for the disconnect command and shut down gracefully before the kill timeout:

process.on('message', function(m) {
    if (m.cmd == 'disconnect') {
        // cleanup
    }
})

sticky sessions support

If you need sticky sessions e.g. for socket.io you can use the experimental companion module sticky-listen, which implements an alternate balancer that distributes the sockets based on the client IP (instead of the regular round-robin one)

Bitdeli