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

master-cluster

v0.3.4

Published

Facilitate using cluster and domain modules, live reload application in development mode on code change.

Downloads

54

Readme

master-cluster

master-cluster is a utility to facilitate implementing node.js core cluster module in any project. I created this module as I use the cluster module in pretty much all my projects and wanted to remove the boilerplate code. It also provides hot reloading of files on the worker in development mode so that workers are restarted when code is changed.

Usage

In boot.js:

var MC = require('master-cluster')
  , worker = require.resolve('./server.js');
MC.start({exec: worker});

In server.js:

var MC = require('master-cluster')
  , app = require('./app.js')
  , http = require('http');

// set the handler that will be used for all new requests
MC.setFnHandlers(app.index, shutdown);

// create the server and pass MC run handler
// workers are restarted automatically on crash
var server = http.createServer(MC.run);
server.listen(3000, function () {
  console.log("Listening on %d", 3000);
});

function shutdown () {
    // optional - cleanly close db connections and other resources
}

In app.js:

function index (req, res) {
    // do something with the request and response
}

exports.index = index;

Convenience http server method

The code above can be simplified if all you need is to start a http server.

In server.js:

var MC = require('master-cluster')
  , app = require('./app.js');

MC.createHttpServer(app.index, 3000, shutdown, function () {
  console.log("Listening on %d", 3000);
});

function shutdown () {
    // cleanly close db connections and other resources
}

With express

var app = require('express')();
var MC = require('master-cluster');
var responseTime = require('response-time');

// setup middlewares, mount routes, etc.
app.use(responseTime({ header: 'x-response-time' }));
app.get('/', function (req, res) {
  res.end('Hello world');
});
MC.createHttpServer(app, 3000);
console.log('Server listening on %d', 3000);

Configuration

Following options can be passed to the master cluster configuration:

  • exec: file path to worker file (see exec option of cluster.setupMaster)
  • size: the number of workers to start, default is require("os").cpus().length
  • reload: boolean, default is false except when /^dev/.test(process.env.NODE_ENV)
  • logger: optional logger for errors (must implement error method)
  • isCluster: pass false to disable cluster and run master-only process. Setting exec is required in this case

Reloader specific options:

  • cooldown: number of milliseconds to wait between file changes, default is 100ms
  • extensions: file extensions to watch for and reload on change, default to js
  • path: path to watch for change, default to current directory (.)

Worker options:

  • killTimeout: timeout to kill the worker on uncaught exception (default 30s)
  • logger: optional logger for errors (must implement error method)

Method handlers

  • start (options): start the master with cluster options
  • run (): worker http handler that runs the request
  • setFnHandlers (runFn, errorFn): set the run and error handlers
  • setOptions (options): set the options for the workers (logger and kill timeout)
  • createHttpServer (handler, port, onShutdown, onListening): create the http server and setup the run and error handlers

Miscellaneous

  • cluster: expose node.js cluster module

Disclaimer

This module is still experimental and subject to change. Please open issues for bugs and suggestions in github. Pull requests welcome.

Author

Jerome Touffe-Blin, @jtblin, http://www.linkedin.com/in/jtblin

License

master-cluster is copyright 2013 Jerome Touffe-Blin and contributors. It is licensed under the BSD license. See the include LICENSE file for details.