Permalink
Browse files

Initial commit - lib, queueing logic and tests

  • Loading branch information...
0 parents commit 8cda0d6378e1b9d7d7850863d84c97e802646100 @TheDeveloper TheDeveloper committed Oct 12, 2012
Showing with 230 additions and 0 deletions.
  1. +15 −0 .gitignore
  2. +18 −0 Makefile
  3. 0 README.md
  4. +13 −0 config.js
  5. +14 −0 config/default.js
  6. +1 −0 config/runtime.json
  7. +13 −0 lib/convoy.js
  8. +6 −0 lib/helpers.js
  9. +9 −0 lib/job.js
  10. +31 −0 lib/queue.js
  11. +34 −0 lib/redis.js
  12. +30 −0 package.json
  13. +46 −0 test/queue.js
@@ -0,0 +1,15 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
@@ -0,0 +1,18 @@
+REPORTER ?= list
+# Flags and arguments for node. Could be "debug" to trigger interactive debug session, or '--debug-brk'
+NODE_FLAGS ?= --timeout 3000
+
+.SILENT:
+
+default:
+ echo "A build command must be specified."
+
+test: test-queue
+
+test-queue:
+ NODE_ENV='test' ./node_modules/mocha/bin/mocha \
+ $(NODE_FLAGS) \
+ --reporter $(REPORTER) \
+ test/queue
+
+.PHONY: test
No changes.
@@ -0,0 +1,13 @@
+var debug = require('debug')('convoy:config');
+
+// Check for config module. If not existent, export an empty config
+try{
+ var config = require('config');
+ debug('Loading config from node-config');
+ module.exports = config.setModuleDefaults('convoy', require('./config/default'));
+}
+catch(e){
+ debug('Setting config to empty object');
+ module.exports = {};
+}
+
@@ -0,0 +1,14 @@
+module.exports = {
+ redis: {
+ host: 'localhost',
+ port: 6379,
+ database: 15
+ },
+
+ keys: {
+ prefix: 'cv:',
+ queued: 'queued',
+ processing: 'processing',
+ failed: 'failed'
+ }
+};
@@ -0,0 +1 @@
+{}
@@ -0,0 +1,13 @@
+var Queue = require('./queue');
+var Job = require('./job');
+var redis = require('./redis');
+
+exports.redis = redis;
+
+exports.createQueue = function(name){
+ var q = new Queue(name);
+ q.client = exports.redis.createClient();
+ return q;
+};
+
+exports.Job = Job;
@@ -0,0 +1,6 @@
+var config = require('config');
+var keyPrefix = config.keys.prefix;
+
+exports.key = function(name){
+ return keyPrefix + name;
+}
@@ -0,0 +1,9 @@
+
+var Job = function(id){
+ if(!id){
+ throw new Error('Jobs must have an ID');
+ }
+ this.id = id;
+};
+
+module.exports = Job;
@@ -0,0 +1,31 @@
+var util = require('util');
+var config = require('config');
+var helpers = require('./helpers');
+
+var prefix = config.keys.prefix;
+
+var Queue = function(name, opts){
+ this.name = name;
+};
+
+module.exports = Queue;
+
+Queue.prototype.addJob = function(job, cb){
+ var self = this;
+
+ // Check job in processing list
+ this.client.zscore(helpers.key('processing'), job.id, function(err, score){
+ if(err)
+ return cb(err);
+ if(score !== null)
+ return cb('processing');
+
+ // If not then queue it
+ self.client.sadd(helpers.key('queued'), job.id, function(err, added){
+ if(err)
+ return cb(err);
+
+ cb(null, added);
+ });
+ });
+};
@@ -0,0 +1,34 @@
+var redis = require('redis');
+
+/**
+ * Create a RedisClient.
+ *
+ * @return {RedisClient}
+ * @api private
+ */
+
+exports.createClient = function(){
+ return redis.createClient();
+};
+
+/**
+ * Create or return the existing RedisClient.
+ *
+ * @return {RedisClient}
+ * @api private
+ */
+
+exports.client = function(){
+ return exports._client || (exports._client = exports.createClient());
+};
+
+/**
+ * Return the pubsub-specific redis client.
+ *
+ * @return {RedisClient}
+ * @api private
+ */
+
+exports.pubsubClient = function(){
+ return exports._pubsub || (exports._pubsub = exports.createClient());
+};
@@ -0,0 +1,30 @@
+{
+ "name": "convoy",
+ "version": "0.0.0",
+ "description": "Redis-backed job queueing",
+ "main": "lib/convoy.js",
+ "scripts": {
+ "test": "make test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/thedeveloper/convoy"
+ },
+ "keywords": [
+ "job",
+ "message",
+ "redis",
+ "queue"
+ ],
+ "author": "Geoff Wagstaff",
+ "license": "MIT",
+ "dependencies": {
+ "config": "~0.4.17",
+ "debug": "~0.7.0",
+ "redis": "https://github.com/mranney/node_redis/tarball/v0.7.2"
+ },
+ "devDependencies": {
+ "mocha": "~1.6.0",
+ "should": "~1.2.0"
+ }
+}
@@ -0,0 +1,46 @@
+var should = require('should');
+var redis = require('redis');
+var Convoy = require('../lib/convoy');
+var config = require('config');
+var helpers = require('../lib/helpers');
+
+var client;
+
+before(function(done){
+ client = redis.createClient();
+ client.select(config.redis.database);
+ client.flushdb(done);
+});
+
+describe('Setting up a queue', function(){
+ it('Sets up queue object', function(done){
+ var q = Convoy.createQueue('rollin');
+ done();
+ });
+
+ it('Can override redis client', function(done){
+ Convoy.redis.createClient = function(){
+ client.testProperty = 'cheese';
+ return client;
+ };
+
+ var q = Convoy.createQueue('rawhide');
+ q.client.testProperty.should.equal('cheese');
+ done();
+ });
+});
+
+describe('Enqueing jobs', function(done){
+ it('places the job in the queued set', function(done){
+ var q = Convoy.createQueue('q');
+ var job = new Convoy.Job(1);
+ q.addJob(job, function(){
+ client.sismember(helpers.key('queued'), job.id, function(err, isMember){
+ should.not.exist(err);
+ should.exist(isMember);
+ isMember.should.equal(1);
+ done();
+ });
+ });
+ });
+});

0 comments on commit 8cda0d6

Please sign in to comment.