• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 14 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The simplest possible event driven job manager, FIFO queue, and "task based cache" in node.js

Neuron Build Status

The simplest possible event driven job manager, FIFO queue, and "task based cache" in node.js

Usage

Neuron is a simple job queue with support for granular concurrency and persistent worker storage. It provides a way to manage jobs as they are created and completed in an async, event-driven manner. Heuristics for parallelization, ordering, and pooling are simple right now and jobs are processed in a FIFO order.

Managing Jobs

Managing jobs in neuron is easy. Neuron doesn't assume anything about the internal structure of the properties for each of your jobs except that they have a function called work(). The concurrency property is also useful but optional. If it isn't specified, neuron defaults to running 50 concurrent jobs.

Here's a quick sample of managing a single job called listDir with neuron.

  var util = require('util'),
      neuron = require('neuron');

  //
  // Create the manager and set the job.
  //
  var manager = new neuron.JobManager();
  manager.addJob('listDir', {
    dirname: __dirname,
    concurrency: 25,
    work: function (dirname) {
      var self = this;
      exec('ls -la ' + dirname || this.dirname, function (error, stdout, stderr) {
        if (error) self.error = error;
        else self.stdout = stdout;

        //
        // Finish the job, this will notify the manager.
        //
        self.finished = true;
      });
    }
  });

Working with and Finishing Job instances

A JobManager will create a worker for the specified Job associated (i.e. add it to the job queue) each time the enqueue() method is called. All parameters passed to the enqueue method are passed on to the Job work() function.

A Job function is 'finished' when it sets this.finished = true. This raises an event which is handled by the manager and re-emitted for the programmer. So when a worker completes, the JobManager raises the finish event:

  //
  // Start a worker and listen for finish
  //
  manager.on('finish', function (job, worker) {
    //
    // Log the result from the worker (the directory listing for '/')
    //
    console.dir(worker.stdout);
  });

  //
  // All arguments passed to the enqueue() function after the job name
  // are consumed by the work() function passed to the job.
  //
  manager.enqueue('listDir', '/');

Using the Persistent WorkerCache

Neuron has a built-in WorkerCache that stores the ordering and arguments to your workers for single instance durability. You don't have to worry about all the cache consistency nonsense though, just include the cache property when creating a JobManager.

  var manager = new neuron.JobManager({
    cache: {
      host: 'localhost',
      port: 6379
    }
  });

  manager.addJob('delayAdd', {
    work: function (a, b, c) {
      var self = this;
      setTimeout(function () {
        self.result = a + b + c;
        self.finished = true;
      }, 1000);
    }
  });

If there are any workers stored in your Redis server, you can load them by calling manager.load(). The manager will emit the load event when it is finished. Make sure that you have already added your jobs to your neuron JobManager before calling load or they will not be placed in the queue for that job.

  manager.on('finish', function (job, worker) {
    //
    // Log the output from the delayed addition
    //
    console.log(worker.result);
  });

  manager.on('load', function () {
    console.log('This will be called before any `finish` events');
  })

  manager.load();

Installing neuron

  $ [sudo] npm install neuron --save

Authors: Donovan Buck, Charlie Robbins

More Repositories

1

j5e

Framework for embedded devices using ECMA-419, the ECMAScript® embedded systems API specification, based on Johnny-Five's API
JavaScript
64
star
2

barcli

A simple tool for displaying real time bar graphs in the console
JavaScript
49
star
3

tharp

An inverse kinematics solver for robots, optimized for Johnny-Five.
JavaScript
27
star
4

spas

Super Proxy Asset Server
JavaScript
10
star
5

schiffer-ipsum

Funky dope technobabble
JavaScript
6
star
6

canDo.js

A canvas wrapper for lightweight animations. Ideal for simple animated UI elements in the 2d rendering context.
JavaScript
4
star
7

leap-controlled-rolling-spider

A Leap Motion Controlled Rolling Spider
JavaScript
3
star
8

leap-motion-color-explorer

A color explorer for the Leap Motion Controller. Not as practical as Kuler but fun.
3
star
9

servo-tuner

JavaScript
1
star
10

j5e-docs

Documentation and website for J5e
JavaScript
1
star
11

huskylens

Johnny-Five plug in for the HuskyLens AI Vision Center
JavaScript
1
star
12

thecollectiveme

1
star
13

timecop

Monitors Traffic Live to see if users have entered their time. If they are behind, a reminder email will be sent
JavaScript
1
star
14

dtex.github.com

JavaScript
1
star
15

cascadia-workshop

1
star
16

siz-marquee

Framework for marquees and banner animations
JavaScript
1
star
17

Extensible-Open-Website-Schema

A free and open standard schema that can be used to describe a sites structure and content
1
star
18

these-bots-are-made-for-walkin

Presentation used at various conferences in 2014 and 2015
CSS
1
star
19

t2-sumobot

For the T2 kit used at Nodebots Day Houston
JavaScript
1
star
20

SAM-YouTube

SAM + jQuery YouTube video embed
JavaScript
1
star