Skip to content

Commit

Permalink
queue.stats
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Jul 2, 2016
1 parent d9de7fd commit d2b53a5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,21 @@ var worker = new NR.worker({connection: connectionDetails, queues: 'math', 'name
Additional methods provided on the `queue` object:
- **queue.prototype.queues** = function(callback)
- **queue.stats** = function(callback)
- callback(error, stats_from_your_cluster)
- **queue.queues** = function(callback)
- callback(error, array_of_queues)
- **queue.prototype.delQueue** = function(q, callback)
- **queue.delQueue** = function(q, callback)
- callback(error)
- **queue.prototype.queued** = function(q, start, stop, callback)
- **queue.queued** = function(q, start, stop, callback)
- callback(error, jobs_in_queue)
- **queue.prototype.length** = function(q, callback)
- **queue.length** = function(q, callback)
- callback(error, number_of_elements_in_queue)
- **queue.prototype.del** = function(q, func, args, count, callback)
- **queue.del** = function(q, func, args, count, callback)
- callback(error, number_of_items_deleted)
- **queue.prototype.delDelayed** = function(q, func, args, callback)
- **queue.delDelayed** = function(q, func, args, callback)
- callback(error, timestamps_the_job_was_removed_from)
- **queue.prototype.scheduledAt** = function(q, func, args, callback)
- **queue.scheduledAt** = function(q, func, args, callback)
- callback(error, timestamps_the_job_is_scheduled_for)
## Delayed Status
Expand Down
18 changes: 18 additions & 0 deletions lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,24 @@ queue.prototype.retryAndRemoveFailed = function(failedJob, callback){
});
};

queue.prototype.stats = function(callback){
var self = this;
self.connection.redis.keys(self.connection.key('stat:*'), function(err, keys){
if(err){return callback(err); }
if(keys.length === 0){return callback(); }
self.connection.redis.mget(keys, function(err, values){
if(err){return callback(err); }
var data = {};
for (i = 0; i < keys.length; i++){
var k = keys[i];
k = k.replace(self.connection.key('stat:'), '');
data[k] = values[i];
}
callback(null, data);
});
});
};

/////////////
// HELPERS //
/////////////
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "node-resque",
"description": "an opinionated implementation of resque in node",
"license": "Apache-2.0",
"version": "2.0.6",
"version": "2.0.7",
"homepage": "http://github.com/taskrabbit/node-resque",
"repository": {
"type": "git",
Expand Down
15 changes: 13 additions & 2 deletions test/core/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,22 @@ describe('queue', function(){
});
});

it('can load stats', function(done){
queue.connection.redis.set(specHelper.namespace + ':stat:failed', 1);
queue.connection.redis.set(specHelper.namespace + ':stat:processed', 2);
queue.stats(function(err, stats){
should.not.exist(err);
stats.processed.should.equal('2');
stats.failed.should.equal('1');
done();
});
});

describe('failed job managment', function(){

beforeEach(function(done){

var errorPayload = function(id){
var errorPayload = function(id){
return JSON.stringify({
worker: 'busted-worker-' + id,
queue: 'busted-queue',
Expand Down Expand Up @@ -314,7 +325,7 @@ describe('queue', function(){
queue.failed(1,2, function(err, failedJobs){
should.not.exist(err);
failedJobs.length.should.equal(2);

failedJobs[0].worker.should.equal('busted-worker-2');
failedJobs[0].queue.should.equal('busted-queue');
failedJobs[0].exception.should.equal('ERROR_NAME');
Expand Down

0 comments on commit d2b53a5

Please sign in to comment.