Git Product home page Git Product logo

coffee-resque's Introduction

Coffee-Resque

Coffeescript/Node.js port of Resque.

USAGE

First, you'll want to queue some jobs in your app:

var resque = require('coffee-resque').connect({
  host: redisHost,
  port: redisPort
});
resque.enqueue('math', 'add', [1,2], function(err, remainingJobs) {
  console.log('New job queued. Remaining jobs in queue: ' + remainingJobs);
});

Next, you'll want to setup a worker to handle these jobs.

Upon completion of the job, invoke the passed callback with a result (if a result was produced by the job) or an Error (if an error was encountered). If an Error is received, resque fails the job. In all other cases resque assumes the job is successful.

The callback is important—it notifies resque that the worker has completed the current job and is ready for another. Neglecting to invoke the callback will result in worker starvation.

// implement your job functions.
var myJobs = {
  add: function(a, b, callback) { callback(a + b); },
  succeed: function(arg, callback) { callback(); },
  fail: function(arg, callback) { callback(new Error('fail')); }
}

// setup a worker
var worker = require('coffee-resque').connect({
  host: redisHost,
  port: redisPort
}).worker('*', myJobs)

// some global event listeners
//
// Triggered every time the Worker polls.
worker.on('poll', function(worker, queue) {})

// Triggered before a Job is attempted.
worker.on('job', function(worker, queue, job) {})

// Triggered every time a Job errors.
worker.on('error', function(err, worker, queue, job) {})

// Triggered on every successful Job run.
worker.on('success', function(worker, queue, job, result) {})

worker.start()

Worker Polling Mechanism

As of v0.1.9, workers poll the given queues similar to Ruby Resque:

start
loop do
  if job = reserve
    job.process
  else
    sleep 5 # Polling frequency = 5
  end
end
shutdown

This ensures that multiple queues are polled in the priority mentioned. Eg: If a worker is started on "queue1,queue2", queue1 is drained completely before jobs in queue2 are processed.

Prior to v0.1.9, workers used to poll the queues in a round-robin fashion.

Development

All code is written in Coffee Script and converted to javascript as it's published to npm.

For normal development, all you need to be concerned about is testing:

$ make test

If you need to generate javascript for production purposes and don't want to use npm packages, you can use:

$ make generate-js
$ make remove-js

You can also have coffeescript watch the src directory and generate Javascript files as they're updated.

$ make dev

TODO

  • Generic failure handling
  • Better polling

coffee-resque's People

Contributors

devdazed avatar gshiftlabs-dbuttineau avatar jasonbosco avatar mratzloff avatar steelthread avatar technoweenie avatar

Watchers

 avatar  avatar  avatar

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.