Skip to content

Instantly share code, notes, and snippets.

@mikeabiezzi
Created September 1, 2012 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeabiezzi/3566602 to your computer and use it in GitHub Desktop.
Save mikeabiezzi/3566602 to your computer and use it in GitHub Desktop.
Node server that slingshots Resque jobs into a Redis store
function buildJob( data ) {
var params = {
'class': CLASS_NAME,
args: [ {"id": data} ]
};
return [
'rpush',
'resque:queue:'+ QUEUE_NAME,
JSON.stringify( params )
];
}
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);
1 record(s) processed
398 record(s) processed
1722 record(s) processed
1779 record(s) processed
1100 record(s) processed
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 );
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);
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