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.
const throng =
$ 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:
Specify the number of workers:
More options:
Handling signals:
(for cleaning up before disconnecting a worker on a SIGTERM, for instance)
{ console process})
All options (with defaults)
A complex example
const throng = // This will only be called once { console process} // This will be called four times { console process process { console }}
$ 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