# throng

> A simple worker-manager for clustered apps

Latest version **5.0.0** (published 2020-09-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install throng
pnpm add throng
yarn add throng
bun add throng
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2020-09-21 |
| First published | 2014-08-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/throng) |
| Module format | CommonJS |
| Node | >= 10 |
| Dependencies | 1 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 880 |
| Author | Hunter Loftis |
| Maintainers | hunterloftis |
| Keywords | cluster, worker, process |

## Links

- npm: https://www.npmjs.com/package/throng
- Repository: https://github.com/hunterloftis/throng
- Issues: https://github.com/hunterloftis/throng/issues
- npm.io page: https://npm.io/package/throng

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.20

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 5.0.0 (latest) — 2020-09-21
- 4.0.0 — 2016-04-14
- 3.0.0 — 2016-03-23
- 2.0.1 — 2016-02-12
- 2.0.0 — 2016-02-11
- 1.0.1 — 2015-09-10
- 1.0.0 — 2014-08-28

## README

# Throng

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.

```js
const throng = require('throng')

throng(id => console.log(`Started worker ${id}`))
```

```
$ node example
Started worker 1
Started worker 2
Started worker 3
Started worker 4
```

## Installation

```
$ npm install --save throng
```

## Use

### Fork 1 worker per CPU:

```js
throng(workerStartFunction)
```

### Specify the number of workers:

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

### More options:

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

### Handling signals:

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

```js
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)

```js
throng({
  master: masterFn,               // Fn to call in master process (can be async)
  worker: workerFn,               // Fn to call in cluster workers (can be async)
  count: 4,                       // Number of workers (cpu count)
  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

```js
const throng = require('throng')

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

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

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

// This will be called four times
function worker(id, disconnect) {
  console.log(`Started worker ${ id }`)
  process.once('SIGTERM', shutdown)
  process.once('SIGINT', shutdown)

  function shutdown() {
    console.log(`Worker ${ id } cleanup.`)
    disconnect()
  }
}
```

```
$ node example-complex.js
Started master
Started worker 1
Started worker 3
Started worker 2
Started worker 4
^C
Worker 1 cleanup.
Worker 3 cleanup.
Worker 2 cleanup.
Worker 4 cleanup.
Master cleanup.
```

## Staying alive

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

```
$ node example-crashy.js
-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
```

---
_Source: https://npm.io/package/throng · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
