A simple task-consumer pool for nodejs
minionpool allows you to concurrently process any tasks you need to (similar to a worker pool). And it's very simple to use.
The npm package is called minionpool.
First things first. To make use of this, you just have to provide a few callbacks, then instantiate a MinionPool and start() it. A MinionPool has Minions, that process tasks. Tasks are provided by a task source, which is called to get one task at a time, one for each minion started, and one for each minion that finishes a previous task.
Both the minions and the task source can keep a state, which is useful to keep database connections for example.
You can either make the pool poll for tasks from the task source, or inject tasks asynchronously "from the outside". You can also find an example of polling from tasks in the mysql example, and one that injects tasks in the rabbitmq example. See below.
var minionpoolMod = require'minionpool';var minionPool = options;minionPoolstart;
Let's see now what can be defined inside the options that we're passing to the MinionPool constructor:
var options =// A name for your minions pool, useful for debugging.name: 'test'// Optional. When 'true', some messages are written to console.debug: true// How many minions to run concurrently.concurrency: 5// Optional. Uses the given Function to log messages.logger: console.log// Optional, defaults to true. If false, the pool will stop processing tasks// and will end on the first task that finishes with error.continueOnError: true// Optional. Called to initialize a 'task source'. It should call the callback// with an initial 'state' (like db connections, file descriptors, etc).// See below. The state will be passed when calling the next property.// Use the err variable to signal errors.var err = ;var state = ;callbackerr state; // err === undefined if no errors// Polling for tasks: The task source produce 'tasks', that are assigned to// minions. A task source receives its state from 'taskSourceStart' first, and// then from whatever it returns on subsequen calls (see below). Also, it// should call the callback with the new task (or 'undefined' when none is found).//// If you are not going to poll for tasks, set this one to undefined, and call// MinionPool.injectTask() to inject the tasks into the pool, they will be// assigned to free minions (or rescheduled if none is available at the time,// be careful not to produce too many tasks).// Use the err variable to signal errors. On errors, will return undefined,// which also means "no more tasks" and the pool will end.var task = ;var err = ;callbackerr task; // err === undefined if no errorsreturn state;// Optional. Called to do any cleanup for the task generator once it runs out// of tasks. Receives the 'state', doesn't have to pass any arguments to the// callback.callback;// The actual code that works on a task. Should call the 'callback' when// done, passing the new state.// Use the err variable to signal errors.var err = ;callbackerr state; // err === undefined if no errors// Optional. Called to initialize each one of the minions. Returns the initial// state for each one of them.// Use the err variable to signal errors.var err = ;callbackerr state; // err === undefined if no errors// Optional. Called to cleanup any needed stuff for each minion.callback;// Optional. Called when the pool has finished all the available work.processexit0;;
As with mysql, there's a specialized minionpool that you might want to check in case you want to work with RabbitMQ, it's called rabbitmq_minionpool.
Also, see this for an example of with plain vanilla minionpool.
In the case of having rabbitmq and mysql workers, it's very useful to take advantage of multicore cpu's. For this, you can use taskset and launch multiple minionpool instances on different cores.