Giter Club home page Giter Club logo

firework's Introduction

npm package build status dependency status code climate

Firework is a distributed, fault-tolerant work queue for Firebase.

Creating Jobs

Since you're using Firebase, you'll probably want to create jobs directly from the browser. To do this, setup a new Firebase location reference that will serve as your queue. To start, you may want to make sure that this location is writable by everyone but only readable by your server (i.e. worker) process.

In this example, we'll assume that your queue is located at https://my-firebase.firebaseio.com/myQueue. In your client code, you'll want to create a new child of the pendingJobs child of that location reference, like so:

var jobs = new Firebase('https://my-firebase.firebaseio.com/myQueue/pendingJobs');
jobs.push({ my: 'job' });

That's it. You've now pushed a job onto the queue for a worker to process sometime later.

It should be noted that workers pull from the beginning of the queue (FIFO). If you have jobs of varying importance you can use a priority to run some jobs before others.

jobs.push().setWithPriority({ important: 'job' }, 0);
jobs.push().setWithPriority({ less: 'important' }, 100);

Firework also provides an API for creating jobs from within your server process. A Firework.Queue (see below) has a push method for this purpose.

var queue = require('firework').createQueue('https://my-firebase.firebaseio.com/myQueue');
queue.push({ my: 'job' });

Important: The following job property names are reserved:

  • _key
  • _startedAt
  • _succeededAt
  • _failedAt
  • _error

Processing Jobs

The easiest way to start processing the jobs on a Firework queue is to use the firework command:

$ firework create-worker.js -w 5

The -w argument specifies the number of workers to use. When a worker has an unrecoverable error it is removed from the worker pool and replaced with a new one.

The create-worker.js module that you pass to firework should export a function that is used to create new workers. To process jobs from the queue we pushed onto in the Creating Jobs section above, our create-worker.js file could look something like this:

var Firework = require('firework');
var queue = Firework.createQueue('https://my-firebase.firebaseio.com/myQueue');

module.exports = function () {
  return Firework.createWorker(queue, function (job, callback) {
    // process the given job.

    // call the callback when you're done, optionally with
    // any error that was encountered while doing the work.
    callback(error);
  });
};

When Firework is done processing a job it stores the job along with some metadata in the startedJobs child location of your queue. Thus, pendingJobs contains a list of all jobs that still need to be done and startedJobs contains a list of all jobs that you have attempted.

Retrying Failed Jobs

If a job fails, it will have _failedAt and _error properties. You can use the _error property to determine the reason of the failure. Once you've fixed the problem, you can retry all jobs that failed using:

queue.retryFailedJobs();

Installation

Using npm:

$ npm install firework

Issues

Please file issues on the issue tracker on GitHub.

Tests

To run the tests in node:

$ npm install
$ npm test

To run the tests in Chrome:

$ npm install
$ npm run test-browser

License

MIT

firework's People

Contributors

mjackson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

firework's Issues

Handle Authentication

Hi @mjackson,

First of all, very nice work with firework. Thank you for this!

Do you planning to support Firebase Authentication? I will change it because I can´t allow clients to read from my queue.

Multiple workers?

This probably betrays my ignorance of node's concurrency model, but is there an actual advantage to running multiple firework workers with the runner? It doesn't appear that the source code makes any use of cluster, child_process or other approaches.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.