Manage a cluster of child processes
npm install batch-cluster!PhotoStructure batch-cluster logo
Efficient, concurrent work via batch-mode command-line tools from within Node.js.





Many command line tools, like
ExifTool,
PowerShell, and
GraphicsMagick, support running in a "batch
mode" that accept a series of discrete commands provided through stdin and
results through stdout. As these tools can be fairly large, spinning them up can
be expensive (especially on Windows).
This module allows you to run a series of commands, or Tasks, processed by a
cluster of these processes.
This module manages both a queue of pending tasks, feeding processes pending
tasks when they are idle, as well as monitoring the child processes for errors
and crashes. Batch processes are also recycled after processing N tasks or
running for N seconds, in an effort to minimize the impact of any potential
memory leaks.
As of version 4, retry logic for tasks is a separate concern from this module.
This package powers exiftool-vendored,
whose source you can examine as an example consumer.
``bash`
$ npm install --save batch-cluster
See CHANGELOG.md.
The child process must use stdin and stdout for control/response.
BatchCluster will ensure a given process is only given one task at a time.
1. Create a singleton instance of
BatchCluster.
Note the constructor
options
takes a union type of
- ChildProcessFactory
and
- BatchProcessOptions,
both of which have no defaults, and
- BatchClusterOptions,
which has defaults that may or may not be relevant to your application.
1. The default logger
writes warning and error messages to console.warn and console.error. YouBatchCluster` constructor.
can change this to your logger by using
setLogger or by providing a logger to the
1. Implement the Parser
class to parse results from your child process.
1. Construct or extend the
Task
class with the desired command and the parser you built in the previous
step, and submit it to your BatchCluster's
enqueueTask
method.
See
src/test.ts
for an example child process. Note that the script is _designed_ to be flaky on
order to test BatchCluster's retry and error handling code.