Skip to content

Instantly share code, notes, and snippets.

@cookrn

cookrn/app.js

Created Dec 26, 2011
Embed
What would you like to do?
Push to Resque Queue from Node
require( './boot' );
var DbItem = require( './db_item' )
, item = new DbItem( '123456abcdefg' );
item.queue( function( error , response ){
console.log( 'Response: ' + response );
process.exit();
} );
var redis = require( 'redis' );
if(! GLOBAL._ ) GLOBAL._ = require( 'underscore' );
if(! GLOBAL.app ) GLOBAL.app = app = {};
var config = {
'redis': {
'host': 'localhost'
, 'port': '6379'
}
};
app.redis_client = redis.createClient( config.redis.port , config.redis.host );
var QueuePusher = require( './queue_pusher' );
var db_item_methods = {
'fail': function(){
console.log( 'Failed' );
}
, 'queue': function( callback ){
var self = this;
var wrapped_callback = function( error , response ){
var fn = function( err , rsp ){
if( err ) return this.fail();
this.succeed();
return callback.call( self , error , response );
}
return fn.call( self , error , response );
}
var pusher = new QueuePusher( self , { callback: wrapped_callback } );
return pusher.push();
}
, 'succeed': function(){
console.log( 'Succeeded' );
}
}
function DbItem( id ){
this._id = id;
return this;
}
DbItem.prototype = db_item_methods;
module.exports = DbItem;
{
"name": "resque_test"
, "description": "Test queueing a Resque job from Node"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"hiredis": "~ 0.1.12"
, "redis": "~ 0.7.1"
, "underscore": "~ 1.2.1"
}
}
var queue_pusher_methods = {
createMessage: function(){
var message = {
'args': [ this.db_item._id ]
, 'class': this.opts.worker_class
};
return JSON.stringify( message );
}
, push: function(){
var call = app.redis_client.rpush( 'resque:queue:' + this.opts.queue , this.createMessage() , this.opts.callback );
return call;
}
};
function QueuePusher( db_item , opts ){
this.db_item = db_item;
this.opts = {
callback: function( err , response ){}
, queue: 'sample_queue'
, worker_class: 'SampleQueueWorker'
};
_( this.opts ).extend( opts );
return this;
};
QueuePusher.prototype = queue_pusher_methods;
module.exports = QueuePusher;
#!/usr/bin/env sh
#
# Requirements:
# => Node
# => NPM
# => A local redis server running on port 6379
#
# Use the SampleQueueWorker in a Resque environment to watch it work
# on the item persisted by the node script.
#
npm install
node app.js
class SampleQueueWorker
@queue = "sample_queue"
attr_accessor :db_item_id
def self.perform( *args , &block )
new *args , &block
end
def initialize( db_item_id )
@db_item_id = db_item_id
run
end
def run
puts "Running queue worker for DbItem #{ db_item_id }"
end
end
@cookrn

This comment has been minimized.

Copy link
Owner Author

@cookrn cookrn commented Dec 26, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment