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

forkpool

v2.0.1

Published

Fork pool for managing forks in node.js.

Downloads

96

Readme

Fork Pool

Managing forks has never been easier!

Build Status

How to

Setup

Install the dependency

npm install --save forkpool

Define ForkPool and Forklet.

var ForkPool = require('forkpool').ForkPool;
var Forklet = require('forkpool').Forklet;

Usage

Initialise the pool. Here we create a pool of size 2. This means that we'll only allow executing of at most two forklets in this particular pool.

var pool = new ForkPool(2); 

Create a Forklet. A forklet is an object that defines how the process must be forked. The two parameters it requires are the file name to be forked and an array of arguments that needs to be passed to the forked process. An optional third parameter specifying the timeout within which the process must run can be specified. If the process execution time is longer than the time specified here, it is killed with SIGKILL signal.

var videoProcessingForklet = new Forklet('./videoprocessor.js', ["/movie.mov"]);

Or with 3 second (3000ms) timeout

var videoProcessingForklet = new Forklet('./videoprocessor.js', ["/movie.mov"], 3000);

Our videoProcessingForklet is ready to be forked. The pool.fork function requires one argument with an additional optional argument:

  1. The forklet object
  2. A callback function that is subscribed to all events relating to that forklet. This is optional.

This callback function, if passed can accept two parameters as follows:

  1. State object representing the state of the forklet. There are four distinct states:
  2. scheduled. This state means that the fork pool has maxed out and the forklet that has been passed has been scheduled for execution. Since the forklet hasn't actually been started, it's processObject isn't available yet and hence is undefined.
  3. started. The forklet has been started. The processObject for the underlying child process is now available.
  4. timedout. The time it took to execute the forklet has exceeded the timeout set within it and hence has been killed. An exited event is always fired after a timedout event.
  5. exited. The forklet has finished execution.
  6. Inner child process object that is running the forklet. This object is only available when the forklet is in started or exited state. This is the standard Node.js process object. More about this object can be found here.
pool.fork(videoProcessingForklet, function(stateObject, processObject) {
  if(stateObject.name === 'started') {
    // do something here
  }
});

API Documentation

ForkPool

A fork pool is a pool that manages execution of forks. In a way it orchestrates execution of forks based on it's size and definition of forklet.

maxSize

Size of the pool. This must be passed in the constructor. It is highly recommended that you never change maxSize once it's defined to prevent collisions and dead pool. This value must be greater than zero.

Function: fork(forklet, callback)

Allows forking of a forklet. This function accepts two arguments: forklet and an optional callback subscribing to all events relating to the passed forklet. These events are:

  1. scheduled
  2. started
  3. timedout
  4. exited

Forklet

A forklet is an object that defines how the child process must be forked. It has three fundamental components:

moduleName

This is the name of the module that will be forked. In layman terms, this is the JavaScript file that will be run in a separate process.

envars

Environment arguments/variables or arguments that will be passed to the child process upon execution. If your child process is going to read in a file, the value here might look something like ['/path/to/the/file']. This must always be an array, even if it only has one item.

(optional) timeout

Time in milliseconds. When the child process is executed, it will be allowed to run in this time. If it's execution time exceeds the time specified here, it will be killed with SIGKILL signal.

(optional) Function: onScheduled(callback)

Chainable function. Callback here is called when the forklet is scheduled for execution. Callback is called without any parameters.

(optional) Function: onStarted(callback)

Chainable function. Callback here is called when the forklet has been executed and is running as a child process. Callback is called with the child process object as its first parameter.

(optional) Function: onExited(callback)

Chainable function. Callback here is called when the child process relating to the forklet has finished its execution. Callback is called with a state params object containing exit state details ({code: code, signal: signal}) as its first parameter and the child process object as its second.

(optional) Function: onTimedout(callback)

Chainable function. Callback here is called when the child_process relating to the forklet has its execution time exceed the specified timeout (in ms). Callback is called with the child process object as its first parameter.

Feel free to raise issues on this Github project for any questions/suggestions.