CartoDB-SQL-API/batch/job_queue.js

36 lines
912 B
JavaScript
Raw Normal View History

'use strict';
function JobQueue(metadataBackend, jobPublisher) {
this.metadataBackend = metadataBackend;
this.jobPublisher = jobPublisher;
}
2016-10-12 23:53:03 +08:00
module.exports = JobQueue;
var QUEUE = {
DB: 5,
PREFIX: 'batch:queues:'
};
module.exports.QUEUE = QUEUE;
JobQueue.prototype.enqueue = function (job_id, host, callback) {
var self = this;
2016-10-12 23:53:03 +08:00
this.metadataBackend.redisCmd(QUEUE.DB, 'LPUSH', [ QUEUE.PREFIX + host, job_id ], function (err) {
if (err) {
return callback(err);
}
self.jobPublisher.publish(host);
callback();
});
};
JobQueue.prototype.dequeue = function (host, callback) {
2016-10-12 23:53:03 +08:00
this.metadataBackend.redisCmd(QUEUE.DB, 'RPOP', [ QUEUE.PREFIX + host ], callback);
};
JobQueue.prototype.enqueueFirst = function (job_id, host, callback) {
2016-10-12 23:53:03 +08:00
this.metadataBackend.redisCmd(QUEUE.DB, 'RPUSH', [ QUEUE.PREFIX + host, job_id ], callback);
};