Coder Social home page Coder Social logo

node-resque's Introduction

This respository is not under active development

Check out Rick Olson’s coffee-resque which is.

Resque for Node.js

Resque is a redis backed task queue inspired by delayed job. Node.js is a javascript runtime built on the v8 engine.

Why?

Resque.js allows you to chunk together blocks of concurrent code and work through them serially across multiple worker processes & machines. The advantage of building this as drop-in compatible resque workers is that the monitoring interface (resque-web) is already written and it provides a nice queue interface between Rails and node. You’re probably already running resque for Rails; use it for Node, too.

Install

npm install resque If you don’t already have node_redis (redis on npm), npm will install it for you.

Use

sandwich_worker.js

var sandwichWorker = exports

sandwichWorker.makeMeASandwich = function (sandwich) {
  var job = this
  makeSandwich (sandwich, function (err, result) {
    if (err) job.fail ({ error: err })
    else job.succeed ()
  })
}

sandwichWorker.SomeRubyTask = function () {
    require ('sys').puts ("This task could have been pulled off a shared queue")
    this.succeed ()
}

success() and failure(error) callbacks are provided and can be passed in through async code to be called on completion, which frees up the worker to take another job off the queue.

Gentlemen, start your workers

To start the worker, WORKER=sandwich_worker.js QUEUE=sandwich_factory bin/node-resque-worker.
You can optionally provide a url to redis, like WORKER=sandwich_worker.js QUEUE=* REDIS=redis.example.com:9876 bin/node-resque-worker

To stop the worker gracefully (finishing the last task), send a QUIT signal.
To stop the worker less gracefully, send a KILL signal.

Enqueueing tasks

#!/usr/bin/env node
var resque = require ("resque").connect ()
resque.enqueue ('sandwich_factory', 'makeMeASandwich',
  { bread: 'Acme Levain'
  , cheese: 'Cowgirl Creamery St Pat'
  , greens: ['arugula']
  , mustard: true
  })

This code adds a makeMeASandwich request to the sandwich_factory resque queue.

Namespaces

By default, resque uses the redis namespace of resque:. If you require resque as follows, you can use a different namespace:

var resque = require ("resque").connect ({ namespace: "node-only" })
...

Providing a Redis connection

If you already are connected to redis in your app, you can use that client instead of creating a new one. To do so:

var redisClient = require ("redis").createClient ()
  , resque = require ("resque").connect ({ redis: redisClient })
...

Specifying Redis port & host

If Redis is not running locally or is not bound to the default Redis port & host (localhost:6379), you can provide port and host as follows:

var resque = require ("resque").connect ({ host: 'redis.production.example.com', port: 1337 })
...

Cleaning stale workers

At any point, you can call require ("resque").connect({...}).cleanStaleWorkers (), which will ensure that any dead workers have been removed from resque. This is called at the start of node-resque-worker.

node-resque's People

Contributors

jakuboboza avatar migrs avatar optixx avatar technoweenie avatar

Stargazers

 avatar

Watchers

 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.