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

forerunner

v0.3.17

Published

forerunner is a distributed job queue framework

Downloads

34

Readme

forerunner

Build Status

Forerunner is a distributed job queue framework. It consists of a central job queue and a pool of workers that can be configured to take on a wide variety of jobs. Workers can be spun up and down independent of the manager, allowing your platform to meet your specific demands.

npm install forerunner

Forerunner Overview

Forerunner System Overview

Forerunner is made up of four main parts. They are the manager, worker, store, and queue. More detailed information can be found in the wiki.

Manager

The manager is the centerpiece of forerunner. It is responsible for having a publicly facing API, assigning jobs to workers, and routing jobs between the store and queue.

Worker

Worker processes are where the majority of your custom code is going to live.

A worker is a simple process that tries to run a set of predefined tasks (functions) in series for a given job. If all the tasks succeed, then the result of the callback is sent to the manager and the job is marked as complete. Otherwise, if an error is thrown or returned in a callback, then the error is returned to the manager, with the job optionally being re-queued for later processing.

Store

The store is where the state of all jobs are recorded as they are processed by forerunner. It has a few methods that the manager calls to get or set data, and it is relatively straight forward in function.

Forerunner comes with an in-memory store for testing that should not be used for production. There is an store implementation for postgres. You can also build your own custom store. If your store passes the store test suite, then it will work with the forerunner system.

Queue

The queue is ephemeral and responsible for managing the order of jobs as they are sent out to workers. Queues can be simple FIFO, random sampled, or weighted against some function that determines importance.

Forerunner comes with an in-memory queue for testing that should not be used for production. There is a FIFO queue implementation for redis. You can also build your own custom queue. If your queue passes the queue test suite then it will work with the forerunner system.

Basic Example

The forerunner platform is made up of two main pieces, the manager and the worker pool. They always run as different processes.

// manager
var forerunner = require('forerunner').forerunner;

// basic set up with defaults
// listens on port 2718
forerunner.start();

// post job hook
forerunner.onComplete('ping', function(id, data) {
  console.log('ping\'ed worker with job id: ' + id);
});

// assign a new job to the worker pool
forerunner.assignJob('ping', {foo: 'bar'}, function(err, status) {
  if (err) {
    console.log('failed to add job initial ping job');
  }
});
// worker
var worker = require('forerunner').worker;
var ping = require('forerunner').builtin.tasks.ping;

worker.registerJobHandler('ping', ping);

// start the worker
var forerunnerLocation = 'http://localhost:2718';
worker.start(forerunnerLocation);

Running both of these scripts will result in the manager telling the worker to execute the ping task with a given payload. The job is sent off to the worker which executes the task and returns the results to the manager.

Compositing Jobs

Jobs are often complicated and require multiple steps to complete. Workers have the ability to composite jobs from existing jobs to create more complex jobs in a simple manner.

What is powerful about composited jobs is that they are guaranteed to execute in series on a single worker, which makes them very predictable and easy to work with. Composited jobs will fail if any of the steps along the way fail and will return immediately to the manager.

// this example will create a new job that will download a set of files
// then tar.gz the result
// and then execute a custom function

var worker = require('forerunner').worker;
// use a builtin task
var fetch = require('forerunner').builtin.fetch;
var targz = require('forerunner').builtin.targz;

// register job handlers as normal
worker.registerJobHandler('fetch', fetch);
worker.registerJobHandler('targz', targz);

// now create a composition
var composedJob = worker.compose([
  // execute the 'fetch' job handler
  'fetch',
  function(id, data, callback) {
    // alter the keys of the returned values to match the targz call
    callback(null, {origin: data.destination, destination: data.tarDestination});
  },
  // execute the 'targz' job handler
  'targz',
  function(id, data, callback) {
    // ... do some custom logic here
    callback(err, newDataFromCustomLogic);
  }
]);

// then register just like you would normally
worker.registerJobHandler('download_and_archive', composedJob);

Custom Jobs

Jobs can take on many different shapes and sizes and can be created for whatever your specific requirements are.

A job is a function that has the following pattern:

function exampleJob(id, data, callback) {
  // id - the assigned job id
  // data - the job data to work will
  // callback(err, results) - callback to be called when the job is done
  //                        - if err is not falsely then the job will be marked as failed and NO results will be returned to the manager

  // ... Do some mind-bending javascript ...

  callback(null, {results: derp});
}

LICENSE

Copyright (c) 2013 Kiernan Tim McGowan (dropdownmenu)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.