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

@captemulation/throng

v6.0.2

Published

A simple worker-manager for clustered apps

Downloads

8

Readme

Throng

npm package Dependency Status devDependency Status Build Status

Dead-simple one-liner for clustered Node.js apps.

Forks N workers and creates new ones if they go down. Correctly handles signals from the OS.

const throng = require('throng')

throng(id => console.log(`Started worker ${id}`))
$ node examples/basic
Started worker 1
Started worker 2
Started worker 3
Started worker 4

Installation

$ npm install --save throng

Use

Fork 1 worker per CPU:

throng(workerStartFunction)

Specify the number of workers:

throng({ worker: workerStartFunction, count: 3 })

More options:

throng({
  master: masterStartFunction,
  worker: workerStartFunction,
  count: 16,
  grace: 1000
})

Handling signals:

(for cleaning up before disconnecting a worker on a SIGTERM, for instance)

throng({ worker })

function worker(id, disconnect) {
  console.log(`Started worker ${id}`)

  process.on('SIGTERM', () => {
    console.log(`Worker ${id} exiting (cleanup here)`)
    disconnect()
  })
})

All options (with defaults)

throng({
  master: () => {},               // Fn to call in master process (can be async)
  worker: yourWorkerFunc,         // Fn to call in cluster workers (can be async)
  count: os.cpus().length,        // Number of workers
  lifetime: Infinity,             // Min time to keep cluster alive (ms)
  grace: 5000,                    // Grace period between signal and hard shutdown (ms)
  signals: ['SIGTERM', 'SIGINT']  // Signals that trigger a shutdown (proxied to workers)
})

A complex example

const throng = require('throng')

throng({ master, worker, count: 4 })

// This will only be called once
function master() {
  console.log('Started master')

  process.on('beforeExit', () => {
    console.log('Master cleanup.')
  })
}

// This will be called four times
function worker(id, disconnect) {
  let exited = false

  console.log(`Started worker ${ id }`)
  process.on('SIGTERM', shutdown)
  process.on('SIGINT', shutdown)

  async function shutdown() {
    if (exited) return
    exited = true

    await new Promise(r => setTimeout(r, 300))  // simulate async cleanup work
    console.log(`Worker ${ id } cleanup done.`)
    disconnect()
  }
}
$ node examples/complex
Started master
Started worker 1
Started worker 3
Started worker 2
Started worker 4
^C
Worker 1 cleanup done.
Worker 3 cleanup done.
Worker 2 cleanup done.
Worker 4 cleanup done.
Master cleanup.

Staying alive

Throng forks replacements for workers that crash so your cluster can continue working through failures.

$ node examples/crashy
-1--2--3--4--2--1--3--4--crash!--1--3--4--crash!--5--3--4--6--5--3--4--crash!--6--crash!--crash!--7--6--8--9--7--6--8--9--crash!--7--6--9--10--7--6--9--10--crash!--7--9--10--11--7--crash!--9--crash!--7--12--9--13--crash!--12--9--crash!--crash!--crash!--14--crash!--12--15--crash!--14--18--15--19--14--18--15--crash!--19--14--crash!--15--20--14--21--15--20--14--21--15--20--14--21--15--20--14--21--15-

Test

$ docker-compose run --rm dev

node@docker:/home/app$ npm test