8000 fix setting global redis/search options, callback based state changes · JavaScriptExpert/kue@cecd631 · GitHub
[go: up one dir, main page]

Skip to content

Commit cecd631

Browse files
committed
fix setting global redis/search options, callback based state changes
1 parent 0ad405a commit cecd631

File tree

5 files changed

+51
-50
lines changed

5 files changed

+51
-50
lines changed

lib/kue.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports = module.exports = Queue;
2424
* Library version.
2525
*/
2626

27-
exports.version = '0.7.0';
27+
exports.version = '0.7.4';
2828

2929
/**
3030
* Expose `Job`.
@@ -83,10 +83,16 @@ exports.workers = [];
8383

8484
function Queue( options ) {
8585
options = options || {};
86-
this.client = redis.createClient( options.redis );
86+
if( options.redis ){
87+
redis.createClient = function() {
88+
// redis.debug_mode = true;
89+
return require('redis').createClient( options.redis );
90+
};
91+
}
92+
this.client = redis.createClient();
8793
this.promoter = null;
8894
this.workers = exports.workers;
89-
this.disableSearch = options.disableSearch;
95+
Job.disableSearch = options.disableSearch;
9096
}
9197

9298
/**

lib/queue/job.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var EventEmitter = require('events').EventEmitter
2222
exports = module.exports = Job;
2323

2424

25+
exports.disableSearch = false;
26+
2527
/**
2628
* Search instance.
2729
*/
@@ -430,12 +432,11 @@ Job.prototype.remove = function (fn) {
430432
client.del('q:job:' + this.id + ':log');
431433
client.del('q:job:' + this.id);
432434
// multi.exec(function (err, replies) {
433-
// console.log( "removeJob", this.id, err, replies );
434435
// events.remove D7AE (this);
435436
fn && fn(err);
436-
// TODO bad smell: change singelton access!
437-
if( !require('../kue').singleton.disableSearch ){
437+
if( !exports.disableSearch ){
438438
getSearch().remove(this.id, function(){
439+
console.log( "remove index...");
439440
}.bind( this ));
440441
}
441442
// }.bind(this));
@@ -487,9 +488,10 @@ Job.prototype.state = function (state, fn) {
487488
// multi.exec(function (err, replies) {
488489
// console.log( "setState(%d) End ", this.id, state, this._state/*, replies*/ );
489490
this.set('updated_at', Date.now());
490-
this.set('state', state, fn);
491-
// increase available jobs, used by Worker#getJob()
492-
if ('inactive' == state) client.lpush('q:' + this.type + ':jobs', 1);
491+
this.set('state', state, function(){
492+
// increase available jobs, used by Worker#getJob()
493+
('inactive' == state) ? client.lpush('q:' + this.type + ':jobs', 1, fn) : fn();
494+
}.bind(this));
493495
// }.bind(this));
494496
}.bind(this));
495497
return this;
@@ -525,9 +527,7 @@ Job.prototype.error = function (err) {
525527
*/
526528

527529
Job.prototype.complete = function (clbk) {
528-
return this.set('progress', 100).state('complete', function () {
529-
clbk && clbk();
530-
}.bind(this));
530+
return this.set('progress', 100).state('complete', clbk);
531531
};
532532

533533
/**
@@ -629,11 +629,11 @@ Job.prototype.update = function (fn) {
629629
this.state(this._state, fn);
630630
}.bind(this));
631631

632-
// TODO bad smell: change singelton access!
633-
// if( require('../kue').singleton.disableSearch ) {
632+
if( !exports.disableSearch ) {
634633
getSearch().index(json, this.id, function(){
634+
console.log( "add index...");
635635
}.bind(this));
636-
// }
636+
}
637637
};
638638

639639
/**

lib/queue/worker.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,28 @@ Worker.prototype.error = function (err, job, asWorkerError) {
114114
Worker.prototype.failed = function (job, err, fn) {
115115
var self = this;
116116
self.error(err, job, false);
117-
job.error(err).failed();
118-
job.attempt(function (error, remaining, attempts, max) {
119-
if (error) return self.error(error, job);
120-
/*remaining
121-
? job.inactive()
122-
: job.failed();*/
123-
if (remaining) {
124-
self.emit('job failed attempt', job);
125-
events.emit(job.id, 'failed attempt', attempts);
126-
if (job.delay()) { //TODO WHEN should we honor the original delay in retries?
127-
job.inactive(); //job.delayed();
117+
job.error(err).failed( function(){
118+
job.attempt(function (error, remaining, attempts, max) {
119+
if (error) return self.error(error, job);
120+
/*remaining
121+
? job.inactive()
122+
: job.failed();*/
123+
if (remaining) {
124+
self.emit('job failed attempt', job);
125+
events.emit(job.id, 'failed attempt', attempts);
126+
if (job.delay()) { //TODO WHEN should we honor the original delay in retries?
127+
job.inactive(); //job.delayed();
128+
} else {
129+
job.inactive();
130+
}
128131
} else {
129-
job.inactive();
132+
self.emit('job failed', job);
133+
events.emit(job.id, 'failed');
134+
// job.failed();
130135
}
131-
} else {
132-
self.emit('job failed', job);
133-
events.emit(job.id, 'failed');
134-
// job.failed();
135-
}
136-
self.start(fn);
137-
});
136+
self.start(fn);
137+
}.bind(this));
138+
}.bind(this));
138139
};
139140

140141
/**
@@ -153,7 +154,7 @@ Worker.prototype.process = function (job, fn) {
153154
var self = this
154155
, start = new Date();
155156
this.job = job;
156-
job.active();
157+
job.active( function(){
157158

158159
// this.domain.run(function(){
159160
// process.nextTick( function(){
@@ -163,10 +164,11 @@ Worker.prototype.process = function (job, fn) {
163164
if (err) {
164165
return self.failed(job, err, fn);
165166
}
166-
job.complete();
167167
job.set('duration', job.duration = new Date - start);
168-
self.emit('job complete', job);
169-
events.emit(job.id, 'complete');
168+
job.complete( function(){
169+
self.emit('job complete', job);
170+
events.emit(job.id, 'complete');
171+
}.bind(this));
170172
self.job = null;
171173
self.start(fn);
172174
},{
@@ -191,6 +193,7 @@ Worker.prototype.process = function (job, fn) {
191193
);
192194
// }.bind( this ));
193195
// }.bind( this ));
196+
}.bind(this));
194197
return this;
195198
};
196199

lib/redis.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,9 @@ var redis = require('redis');
1818
* @api private
1919
*/
2020

21-
exports.createClient = function ( options ) {
21+
exports.createClient = function () {
2222
var client;
23-
if( options ) {
24-
client = redis.createClient(
25-
options.port || 6379,
26-
options.host,
27-
options.options || {}
28-
);
29-
} else {
30-
client = redis.createClient();
31-
}
23+
client = redis.createClient();
3224
return client;
3325
};
3426

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
"url": "https://github.com/LearnBoost/kue.git"
1616
},
1717
"dependencies": {
18-
"redis": "0.7.2",
18+
"redis": "~0.10.0",
1919
"express": "~3.1.1",
2020
"jade": "0.26.3",
2121
"stylus": "0.27.2",
2222
"nib": "0.5.0",
23-
"reds": "0.1.4"
23+
"reds": "~0.2.4"
2424
},
2525
"main": "index",
2626
"devDependencies": {

0 commit comments

Comments
 (0)
0