Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,7 @@ var kue = require('kue');
var q = kue.createQueue({
prefix: 'q',
redis: {
socket: '/data/sockets/redis.sock',
auth: 'password',
options: {
// see https://github.com/mranney/node_redis#rediscreateclient
}
// see https://github.com/mranney/node_redis#rediscreateclient for options
}
});
```
Expand Down
38 changes: 9 additions & 29 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,12 @@ exports.configureFactory = function( options, queue ) {
options.prefix = options.prefix || 'q';

if( typeof options.redis === 'string' ) {
// parse the url
var conn_info = url.parse(options.redis, true /* parse query string */);
if( conn_info.protocol !== 'redis:' ) {
throw new Error('kue connection string must use the redis: protocol');
}

options.redis = {
port: conn_info.port || 6379,
host: conn_info.hostname,
db: (conn_info.pathname ? conn_info.pathname.substr(1) : null) || conn_info.query.db || 0,
// see https://github.com/mranney/node_redis#rediscreateclient
options: conn_info.query
};

if( conn_info.auth ) {
options.redis.auth = conn_info.auth.replace(/.*?:/, '');
}
options.redis = { url: options.redis }
}

// support legacy passthrough of options
if( options.redis && options.redis.options ) {
Object.assign(options.redis, options.redis.options)
}

options.redis = options.redis || {};
Expand Down Expand Up @@ -108,18 +96,10 @@ exports.configureFactory = function( options, queue ) {
*/

exports.createClientFactory = function( options ) {
var socket = options.redis.socket;
var port = !socket ? (options.redis.port || 6379) : null;
var host = !socket ? (options.redis.host || '127.0.0.1') : null;
var db = !socket ? (options.redis.db || 0) : null;
var client = redis.createClient(socket || port, host, options.redis.options);
if( options.redis.auth ) {
client.auth(options.redis.auth);
}
if( db >= 0 ){
client.select(db);
}
return client;
// socket used to be passed in explicitly as an option
// convert it to how the redis client is looking for it sees it if present.
options.path = options.redis.socket;
return redis.createClient(options.redis);
};

/**
Expand Down
53 changes: 0 additions & 53 deletions test/tdd/redis.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ describe('redis', function() {
redis.reset.restore();
});

it('should parse a url connection string', function () {
var options = {
redis: 'redis://:password@host:1234/db'
};
redis.configureFactory(options);
options.redis.port.should.equal('1234');
options.redis.host.should.equal('host');
options.redis.db.should.equal('db');
});

it('should reset everything', function () {
var options = {
redis: 'redis://:password@host:1234/db'
Expand Down Expand Up @@ -114,49 +104,6 @@ describe('redis', function() {

});

describe('Function: createClientFactory', function() {
var options, client;
beforeEach(function(){
options = {
prefix: 'prefix',
redis: {
port: 'port',
host: 'host',
db: 'db',
options: {}
}
};
client = {
auth: sinon.stub(),
select: sinon.stub()
};
sinon.stub(r, 'createClient').returns(client);
});

afterEach(function(){
r.createClient.restore();
});

it('should create a client', function () {
var c = redis.createClientFactory(options);
r.createClient.called.should.be.true;
r.createClient.calledWith(options.redis.port, options.redis.host, options.redis.options).should.be.true;
});

it('should authenticate if auth is present', function () {
options.redis.auth = 'auth';
var c = redis.createClientFactory(options);
client.auth.calledWith(options.redis.auth).should.be.true;
});

it('should select the passed in db', function () {
options.redis.db = 1;
var c = redis.createClientFactory(options);
client.select.calledWith(options.redis.db).should.be.true;
});

});

describe('Function: client', function() {

it('should return the existing client if there is one', function () {
Expand Down
51 changes: 0 additions & 51 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,56 +66,6 @@ describe('CONNECTION', function(){
} );
});

it( 'should default to 0 db with string', function ( done ) {
var jobs = new kue( {
redis: 'redis://localhost:6379/?foo=bar'
} );

jobs.client.options.port.should.be.eql( 6379 );
jobs.client.options.host.should.be.eql( 'localhost' );
jobs.client.options.foo.should.be.eql( 'bar' );

var jobData = {
title: 'welcome email for tj',
to: '"TJ" <[email protected]>',
template: 'welcome-email'
};
jobs.create( 'email-should-be-processed-5', jobData ).priority( 'high' ).save();
jobs.process( 'email-should-be-processed-5', function ( job, jdone ) {
job.data.should.be.eql( jobData );
job.log( '<p>This is <span style="color: green;">a</span> formatted log<p/>' );
jobs.client.selected_db.should.be.eql(0);
jdone();
done();
} );

});

it( 'should default to 0 db with string and no /', function ( done ) {
var jobs = new kue( {
redis: 'redis://localhost:6379?foo=bar'
} );

jobs.client.options.port.should.be.eql( 6379 );
jobs.client.options.host.should.be.eql( 'localhost' );
jobs.client.options.foo.should.be.eql( 'bar' );

var jobData = {
title: 'welcome email for tj',
to: '"TJ" <[email protected]>',
template: 'welcome-email'
};
jobs.create( 'email-should-be-processed-6', jobData ).priority( 'high' ).save();
jobs.process( 'email-should-be-processed-6', function ( job, jdone ) {
job.data.should.be.eql( jobData );
job.log( '<p>This is <span style="color: green;">a</span> formatted log<p/>' );
jobs.client.selected_db.should.be.eql(0);
jdone();
done();
} );

});

it( 'should configure properly with dictionary', function ( done ) {
jobs = new kue( {
redis: {
Expand All @@ -141,7 +91,6 @@ describe('CONNECTION', function(){
job.data.should.be.eql( jobData );
job.log( '<p>This is <span style="color: green;">a</span> formatted log<p/>' );
// Needs to be here to support the async client.select statement where the return happens sync but the call is async
jobs.client.selected_db.should.be.eql(0);
jdone();
done();
} );
Expand Down