CartoDB-SQL-API/batch/pubsub/queue-seeker.js
Raul Ochoa 1f038ac1f4 Moves from host queues to user queues
- Existing jobs are moved before start processing them.
 - Uses a new queue prefix to avoid collisions.
 - Pub/Sub also changes communication channel.
 - Job subscriber emits user+host on new jobs.
 - Batch processor is faulty. See TODO in batch.js.
2016-10-12 21:32:29 +02:00

48 lines
1.2 KiB
JavaScript

'use strict';
var QUEUE = require('../job_queue').QUEUE;
function QueueSeeker(pool) {
this.pool = pool;
}
module.exports = QueueSeeker;
QueueSeeker.prototype.seek = function (callback) {
var initialCursor = ['0'];
var users = {};
this._seek(initialCursor, users, callback);
};
QueueSeeker.prototype._seek = function (cursor, users, callback) {
var self = this;
var redisParams = [cursor[0], 'MATCH', QUEUE.PREFIX + '*'];
this.pool.acquire(QUEUE.DB, function(err, client) {
if (err) {
return callback(err);
}
client.scan(redisParams, function(err, currentCursor) {
// checks if iteration has ended
if (currentCursor[0] === '0') {
self.pool.release(QUEUE.DB, client);
return callback(null, Object.keys(users));
}
var queues = currentCursor[1];
if (!queues) {
return callback(null);
}
queues.forEach(function (queue) {
var user = queue.substr(QUEUE.PREFIX.length);
users[user] = true;
});
self._seek(currentCursor, users, callback);
});
});
};