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

@valkyriestudios/node-cluster

v3.0.2

Published

A cluster script for node processes

Downloads

3

Readme

@valkyriestudios/node-cluster

npm npm

A lightweight node clustering script that can be used to cluster a single worker up to the max amount of cpu's available on a system.

Basic Setup

The easiest way to make it work is running it as an npm lifecycle hook, set your start command in package.json to the following:

...
"scripts": {
    "start": "node node_modules/@valkyriestudios/node-cluster --worker=PATH_TO_MY_SCRIPT"
},
...

For example if the script that you use to run your server is under dist/server/index.js :

...
"scripts": {
    "start": "node node_modules/@valkyriestudios/node-cluster --worker=dist/server/index.js"
},
...

In essence that's all there is to it!

Configuration

Some aspects of the clustering can be controlled through arguments passed to the node-cluster script. Below is a full overview of these parameters, along with a small description of each and their defaults.

  • worker (required) Configures the script of the worker instance that is to be clustered. (shorthand: '-w')

  • count (default: amount of cpus that are available on the system that this is run on) Configures the amount of instances to create of the worker script. (shorthand: '-c')

  • timeout (default: 10000) Configures the time in miliseconds that the node-cluster script will wait for a 'listen' event from a worker instance before shutting it down. (shorthand: '-t')

Getting into the nitty-gritty

To allow for communication between the workers and it's master, node-cluster provides an easy-to-use singleton class called 'WorkerDaemon'. This singleton only needs to be instantiated once and will be used as a proxy on top of the raw node process.

The following is a simple example of setting up the WorkerDaemon on a small Koa application :

'use strict';

import Koa from 'koa';

import { WorkerDaemon } from '@valkyriestudios/node-cluster/WorkerDaemon';

const app = new Koa();
const daemon = new WorkerDaemon();

try {
	app.listen(3000);
} catch (err) {
	daemon.shutdown(err);
}

Take note at the try-catch handler where we use the WorkerDaemon's 'daemon.shutdown' function to tell the master that the boot sequence of the instance failed.

Quicktip : You can call the WorkerDaemon.shutdown function anywhere in your code, for example when your mongo middleware fails to connect, or when your redis instance can't be reached.

WorkerDaemon functionality

shutdown(msg)

Tells the master to shutdown this specific instance on the cluster @variable: msg (String): A message to be logged by the master

log(msg)

Tells the master to log a message @variable: msg (String): A message to be logged by the master

Author

Contributors