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

minionpool

v0.0.10

Published

A simple task-consumer pool for nodejs

Downloads

14

Readme

About

minionpool allows you to concurrently process any tasks you need to (similar to a worker pool). And it's very simple to use.

Installing it

The npm package is called minionpool.

How it works

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.

Polling for tasks vs Injecting tasks

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.

Quick Example

Main code

var minionpoolMod = require('minionpool');

var minionPool = new minionpoolMod.MinionPool(options);
minionPool.start();

Configuring the pool

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.
  taskSourceStart: function(callback) {
    var err = ... ;
    var state = ... ;
    callback(err, 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.
  taskSourceNext: function(state, callback) {
    var task = ...;
    var err = ...;
    callback(err, task); // err === undefined if no errors
    return 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.
  taskSourceEnd: function(state, 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.
  minionTaskHandler: function(task, state, callback) {
    var err = ...;
    callback(err, 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.
  minionStart: function(callback) {
    var err = ...;
    callback(err, state); // err === undefined if no errors
  },

  // Optional. Called to cleanup any needed stuff for each minion.
  minionEnd: function(state, callback) {
    callback();
  },

  // Optional. Called when the pool has finished all the available work.
  poolEnd: function() {
    process.exit(0);
  }
};

Example using the ArrayMinionPool

See this.

Example using MySQL (will process all rows in a given table).

You might want to check out mysql_minionpool if you need to work with mysql, since it's a specialized minionpool. In any case, see this for an example using plain vanilla minionpool.

Example using RabbitMQ

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.

Using multiple cores

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.

Into the details: Lifecycle (Polling)

  1. A pool starts.
  2. All minions in the pool are started. For each minion, minionStart is called.
  3. After all the minions are started, the task source is started, by calling taskSourceStart. On error, the pool will end.
  4. After the task source starts one task per minion is requested by calling taskSourceNext. If taskSourceNext returns error, it will interpreted as no more tasks available, and the pool will end.
  5. When a minion finishes processing a task (minionTaskHandler), a new one will be assigned to it.
  6. When a minion finishes a task, but the task source does not have a task for it, that minion will become idle, and shutdown, by calling minionEnd.
  7. When all the minions have shutdowned, the task source will shutdown too, by calling taskSourceEnd.
  8. When the task source shuts down, the pool will shut down, and poolEnd will be called.

Into the details: Lifecycle (Injected tasks)

  1. A pool starts.
  2. All minions in the pool are started. For each minion, minionStart is called.
  3. After all the minions are started, the task source is started, by calling taskSourceStart.
  4. Someone calls MinionPool.injectTask from the outside, a free minion is picked up and the task is assigned to it. Otherwise, the task is rescheduled internally until a free minion is available.
  5. The pool only shuts down when MinionPool.end() is called. Then all minions are finished.
  6. When all the minions have shutdowned, the task source will shutdown too, by calling taskSourceEnd.
  7. When the task source shuts down, the pool will shut down, and poolEnd will be called.