Created
September 1, 2012 07:41
-
-
Save mikeabiezzi/3566602 to your computer and use it in GitHub Desktop.
Node server that slingshots Resque jobs into a Redis store
This file contains hidden or 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
function buildJob( data ) { | |
var params = { | |
'class': CLASS_NAME, | |
args: [ {"id": data} ] | |
}; | |
return [ | |
'rpush', | |
'resque:queue:'+ QUEUE_NAME, | |
JSON.stringify( params ) | |
]; | |
} |
This file contains hidden or 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
sendRequest = function() { | |
xhr= new XMLHttpRequest(); | |
xhr.open('POST', '/api/redis/add'); | |
var id = Math.floor(Math.random()*1000000); | |
xhr.send(id); | |
return "id="+id; | |
} | |
sendRequests = function(x) { | |
for( var i = 0; i < x; i++ ) { | |
sendRequest(); | |
} | |
} | |
sendRequests(5000); |
This file contains hidden or 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
1 record(s) processed | |
398 record(s) processed | |
1722 record(s) processed | |
1779 record(s) processed | |
1100 record(s) processed |
This file contains hidden or 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
function sendToRedis() { | |
count = job_queue.length; | |
if( count > 0 ) { | |
var jobs = job_queue.splice( 0, count ); | |
client.multi( jobs ).exec(); | |
console.log( count + ' record(s) processed' ); | |
} | |
} | |
setInterval( sendToRedis, 1000 ); |
This file contains hidden or 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
http.createServer(function( req, res ) { | |
if( req.url !== '/api/redis/add' ) { | |
res.writeHead(404); | |
res.end('Not found'); | |
return; | |
} | |
var buffer = ''; | |
req.on('data', function(chunk) { | |
buffer += chunk; | |
}); | |
req.on('end', function() { | |
job_queue.push( buildJob( buffer ) ); | |
res.writeHead(200); | |
res.end(); | |
}); | |
}).listen( 3000); |
This file contains hidden or 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 http = require('http'); | |
var redis = require('redis'); | |
var QUEUE_NAME = 'default'; | |
var CLASS_NAME = 'SomeJob'; | |
var client = redis.createClient(); | |
var job_queue = []; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment