2016-10-12 01:01:39 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-12 23:53:03 +08:00
|
|
|
var QUEUE = require('../job_queue').QUEUE;
|
|
|
|
|
2016-10-12 01:01:39 +08:00
|
|
|
function QueueSeeker(pool) {
|
|
|
|
this.pool = pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = QueueSeeker;
|
|
|
|
|
2016-10-12 01:45:26 +08:00
|
|
|
QueueSeeker.prototype.seek = function (callback) {
|
2016-10-12 01:01:39 +08:00
|
|
|
var initialCursor = ['0'];
|
2016-10-13 03:32:29 +08:00
|
|
|
var users = {};
|
2016-10-12 01:01:39 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-10-12 23:53:03 +08:00
|
|
|
this.pool.acquire(QUEUE.DB, function(err, client) {
|
2016-10-12 01:01:39 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-10-14 18:33:37 +08:00
|
|
|
self._seek(client, initialCursor, users, function(err, users) {
|
2016-10-13 19:09:56 +08:00
|
|
|
self.pool.release(QUEUE.DB, client);
|
2016-10-17 18:27:06 +08:00
|
|
|
return callback(err, Object.keys(users));
|
2016-10-13 19:09:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-10-12 01:01:39 +08:00
|
|
|
|
2016-10-14 18:33:37 +08:00
|
|
|
QueueSeeker.prototype._seek = function (client, cursor, users, callback) {
|
2016-10-13 19:09:56 +08:00
|
|
|
var self = this;
|
|
|
|
var redisParams = [cursor[0], 'MATCH', QUEUE.PREFIX + '*'];
|
2016-10-12 01:01:39 +08:00
|
|
|
|
2016-10-13 19:09:56 +08:00
|
|
|
client.scan(redisParams, function(err, currentCursor) {
|
2016-10-17 18:27:06 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(null, users);
|
2016-10-13 19:09:56 +08:00
|
|
|
}
|
2016-10-12 01:01:39 +08:00
|
|
|
|
2016-10-13 19:09:56 +08:00
|
|
|
var queues = currentCursor[1];
|
2016-10-17 18:27:06 +08:00
|
|
|
if (queues) {
|
|
|
|
queues.forEach(function (queue) {
|
|
|
|
var user = queue.substr(QUEUE.PREFIX.length);
|
|
|
|
users[user] = true;
|
|
|
|
});
|
2016-10-13 19:09:56 +08:00
|
|
|
}
|
2016-10-12 01:01:39 +08:00
|
|
|
|
2016-10-17 18:27:06 +08:00
|
|
|
var hasMore = currentCursor[0] !== '0';
|
|
|
|
if (!hasMore) {
|
|
|
|
return callback(null, users);
|
|
|
|
}
|
2016-10-13 19:09:56 +08:00
|
|
|
|
2016-10-14 18:33:37 +08:00
|
|
|
self._seek(client, currentCursor, users, callback);
|
2016-10-12 01:01:39 +08:00
|
|
|
});
|
|
|
|
};
|