Created
December 26, 2011 20:18
-
-
Save cookrn/1522038 to your computer and use it in GitHub Desktop.
Push to Resque Queue from Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require( './boot' ); | |
var DbItem = require( './db_item' ) | |
, item = new DbItem( '123456abcdefg' ); | |
item.queue( function( error , response ){ | |
console.log( 'Response: ' + response ); | |
process.exit(); | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Props to @pgr0ss for helping me along :: https://gist.github.com/317707 and http://www.pgrs.net/2010/02/28/node-js-redis-and-resque/