Skip to content

Commit

Permalink
resque basics in place
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Sep 22, 2013
0 parents commit 1b60e48
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/*
.DS_Store
npm-debug.log
dump.rdb
45 changes: 45 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var actionResque = require(__dirname + "/index.js");
// In your projects: var actionResque = require("actionResque");
var connectionDetails = {
host: "127.0.0.1",
password: "",
port: 6379,
database: 0,
// namespace: "resque",
// looping: true
}

var jobsToComplete = 0;

var jobs = {
add: function(a,b,callback){
console.log("adding " + a + " + " + b);
jobsToComplete--;
if(jobsToComplete == 0){ shutdown(); }
callback(a + b);
},
subtract: function(a,b,callback){
console.log("subtracting " + a + " - " + b);
jobsToComplete--;
if(jobsToComplete == 0){ shutdown(); }
callback(a - b);
},
};

var worker = new actionResque.worker({connection: connectionDetails, queues: 'math'}, jobs, function(){
worker.start();
});

var queue = new actionResque.queue({connection: connectionDetails, queue: 'math'}, function(){
queue.enqueue("add", [1,2]);
queue.enqueue("subtract", [2,1]);
jobsToComplete = 2;
});

var shutdown = function(){
setTimeout(function(){
worker.end(function(){
process.exit();
});
}, 500);
}
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.connection = require(__dirname + "/lib/connection.js").connection;
exports.queue = require(__dirname + "/lib/queue.js").queue;
exports.worker = require(__dirname + "/lib/worker.js").worker;
52 changes: 52 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var connection = function(options){
var self = this;
var defaults = self.defaults();
for(var i in defaults){
if(options[i] == null){
options[i] = defaults[i];
}
}
self.options = options;
}

connection.prototype.defaults = function(){
return {
package: require("redis"),
host: "127.0.0.1",
password: "",
port: 6379,
database: 0,
namespace: "resque",
}
}

connection.prototype.connect = function(callback){
var self = this;
var options = self.options;
self.redis = options.package.createClient(options.port, options.host, options.options);
if(options.password != null && options.password != ""){
self.redis.auth(options.password, function(err){
self.redis.select(options.database, function(err){
callback(err);
});
});
}else{
self.redis.select(options.database, function(err){
callback(err);
});
}
}

connection.prototype.disconnect = function(){
var self = this;
return self.redis.quit();
}

connection.prototype.key = function(){
var args;
args = 1 <= arguments.length ? [].slice.call(arguments, 0) : [];
args.unshift(this.options.namespace);
return args.join(":");
}

exports.connection = connection;
37 changes: 37 additions & 0 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var connection = require(__dirname + "/connection.js").connection;

var queue = function(options, callback){
var self = this;
self.options = options;
self.connection = new connection(options.connection);
self.connection.connect(function(){
if(typeof callback == 'function'){ callback(); }
});
}

queue.prototype.enqueue = function(func, args){
var self = this;
var queue = self.options.queue;
self.connection.redis.sadd(self.connection.key('queues'), queue);
return self.connection.redis.rpush(self.connection.key('queue', queue), JSON.stringify({
"class": func,
args: args || []
}));
}

queue.prototype.queues = function(callback){
var self = this;
self.connection.redis.smembers(self.connection.key('queues'), function(err, queues){
callback(err, queues);
});
}

queue.prototype.length = function(callback){
var self = this;
var queue = self.options.queue;
self.connection.redis.llen(self.connection.key('queue', queue), function(err, length){
callback(err, length);
});
}

exports.queue = queue;
Loading

0 comments on commit 1b60e48

Please sign in to comment.