From d2b53a559e1e927dc8947609062b4e569e22b374 Mon Sep 17 00:00:00 2001 From: Evan Tahler Date: Sat, 2 Jul 2016 16:17:06 -0700 Subject: [PATCH] queue.stats --- README.md | 16 +++++++++------- lib/queue.js | 18 ++++++++++++++++++ package.json | 2 +- test/core/queue.js | 15 +++++++++++++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b9242fee..be5be274 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/queue.js b/lib/queue.js index 7a57c46e..0f464ae3 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -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 // ///////////// diff --git a/package.json b/package.json index 65fb2fbd..088c207e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/core/queue.js b/test/core/queue.js index 0e1369c8..bf95a2fc 100644 --- a/test/core/queue.js +++ b/test/core/queue.js @@ -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', @@ -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');