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

cluster-fuck

v1.1.4

Published

a simple, elegant cluster forker

Downloads

17

Readme

cluster-fuck

A simple, elegant cluster forker with TCP REPL server via prepl.

Usage

var ClusterFuck = require('cluster-fuck'),
    myCluster;
    
myCluster = new ClusterFuck({
    exec: "mySpecialWorker.js",
    args: ['--my', '--arguments'],
    silent: false,
    workers: 8
});

myCluster.start();

When workers are ready, they must send a ready signal and their memory usage:

var server = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
});

server.on('listening', function () {
    process.send({
        memoryUsage: process.memoryUsage(),
        ready: true
    });
});

server.listen(1337, '127.0.0.1');

^C quits gracefully; however, you may connect to the REPL to further control the cluster using nc:

$ nc -U prepl.sock
> help
        COMMAND         DESCRIPTION
        help            Display this help message
        exit            Disconnect from REPL
        status          Display the status of the Cluster
        memory          Display detailed memory information.
        start           Start the cluster
        restart         Restart all workers gracefully
        stop            Stop all workers gracefully
        kill            Kill all workers immediately
        shutdown        Stop all workers gracefully, then shutdown the cluster.
> 

or another node app:

var net = require('net');
var client = net.connect({path: 'prepl.sock'}), function() {
  console.log('connected!');
});

client.on('data', function(data) {
  console.log(data.toString());
});

replClient.on('error', function (err) {
    console.log(err);
})

var commands = [
    'help\n',
    'kill\n',
    'status\n',
    'start\n', 
    'status\n'
];

for (var i = 0; i < commands.length; i++) {
    (function (command) {
        setTimeout(function() {
            replClient.write(command);
        }, i*2000);
    }(commands[i]));
}

which would output something similar to:

connected!
>       COMMAND     DESCRIPTION
        help            Display this help message
        exit            Disconnect from REPL
        status          Display the status of the Cluster
        memory          Display detailed memory information.
        start           Start the cluster
        restart         Restart all workers gracefully
        stop            Stop all workers gracefully
        kill            Kill all workers immediately
        shutdown        Stop all workers gracefully, then shutdown the cluster.
> 
> 
        State:          stopped
        Ready Workers:  0
        Memory:         15.421875 MB
> 
> 
        State:          ready
        Ready Workers:  7
        Memory:         300.91015625 MB
> 

Configuration

Default configuration:

{
    "args": [],
    "exec": "server.js",
    "silent": true,
    "shutdownTimeout": 5000,
    "workers": 1
}

Options

API

ClusterFuck

Exposed by require('cluster-fuck').

ClusterFuck()

Creates a new ClusterFuck. Works with and without new:

var ClusterFuck = require('cluster-fuck')();
  // or
var ClusterFuck = require('cluster-fuck');
var myCluster = new ClusterFuck();

ClusterFuck.start()

Starts the cluster:

myCluster.start();

ClusterFuck.shutdown([done:Function])

  • done function: function to be called when the cluster has shutdown

Used to stop all workers and shutdown the cluster:

myCluster.shutdown(function () {
    console.log('cluster shutdown!');
})

ClusterFuck.memoryUsage([unit: String])

  • unit function: if specified, this function returns the total memory formatted in units.

Returns the total memory usage for each process

var memory = myCluster.memoryUsage();
//{ cluster: { rss: 308719616, heapUsed: 17281008, heapTotal: 34354176 },
//  master: { rss: 15187968, heapTotal: 10456064, heapUsed: 4650712 },
//  workers: 
//   { '1': { rss: 43851776, heapTotal: 34354176, heapUsed: 19252248 },
//     '2': { rss: 41627648, heapTotal: 34354176, heapUsed: 16673376 },
//     '3': { rss: 41598976, heapTotal: 34354176, heapUsed: 17105496 },
//     '4': { rss: 41418752, heapTotal: 34354176, heapUsed: 16969800 },
//     '5': { rss: 41639936, heapTotal: 34354176, heapUsed: 16935840 },
//     '6': { rss: 41676800, heapTotal: 34354176, heapUsed: 17133768 },
//     '7': { rss: 41717760, heapTotal: 34354176, heapUsed: 17281008 } },
//  workerTotal: 293531648 }

// or
var memory = myCluster.memoryUsage('MB');
// 294.11328125 MB

note: unit must be a valid unit in techjeffharris-utils.

Events

In addtion to all events inherited from cluster, ClusterFuck exposes the following events:

Event: 'starting'

Emitted before the cluster starts all workers:

myCluster.on('starting', function () {
    console.log('starting cluster...');
});

Event: 'ready'

Emitted when all workers are ready:

myCluster.on('ready', function () {
    console.log('the cluster is ready!');
});

Event: 'restarting'

Emitted before the cluster restarts all workers:

myCluster.on('restarting', function () {
    console.log('restarting cluster...');
});

Event 'restarted'

Emitted when all workers are ready after a restart:

myCluster.on('restarted', function () {
    console.log('cluster has restarted!');
});

Event: 'shuttingDown'

Emitted when the cluster starts shutting down.

myCluster.on('shuttingDown', function () {
    console.log('shutting down cluster...');
});

Event: 'shutdown'

Emitted when the cluster has shut down.

myCluster.on('shutdown', function () {
    console.log('cluster has shutdown!');
});

Event: 'stopping'

Emitted before the cluster stops all workers:

myCluster.on('stopping', function () {
    console.log('stopping cluster...');
});

Event: 'stopped'

Emitted when all workers have stopped:

myCluster.on('stopped', function () {
    console.log('cluster has stopped!');
});

Event 'killing'

Emitted before the cluster kills all workers:

myCluster.on('killing', function () {
    console.log('killing cluster...');
});

Event: 'killed'

Emitted when all workers have been killed:

myCluster.on('killed', function () {
    console.log('cluster has been killed!');
});