2015-12-29 17:19:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-30 00:29:53 +08:00
|
|
|
function JobQueue(metadataBackend, jobPublisher) {
|
2015-12-29 17:19:10 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
2016-06-30 00:29:53 +08:00
|
|
|
this.jobPublisher = jobPublisher;
|
2015-12-29 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2016-10-12 23:53:03 +08:00
|
|
|
module.exports = JobQueue;
|
|
|
|
|
|
|
|
var QUEUE = {
|
|
|
|
DB: 5,
|
|
|
|
PREFIX: 'batch:queues:'
|
|
|
|
};
|
|
|
|
module.exports.QUEUE = QUEUE;
|
|
|
|
|
2015-12-29 17:19:10 +08:00
|
|
|
JobQueue.prototype.enqueue = function (job_id, host, callback) {
|
2016-06-30 00:29:53 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-10-12 23:53:03 +08:00
|
|
|
this.metadataBackend.redisCmd(QUEUE.DB, 'LPUSH', [ QUEUE.PREFIX + host, job_id ], function (err) {
|
2016-06-30 00:29:53 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobPublisher.publish(host);
|
|
|
|
callback();
|
|
|
|
});
|
2015-12-29 17:19:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JobQueue.prototype.dequeue = function (host, callback) {
|
2016-10-12 23:53:03 +08:00
|
|
|
this.metadataBackend.redisCmd(QUEUE.DB, 'RPOP', [ QUEUE.PREFIX + host ], callback);
|
2015-12-29 17:19:10 +08:00
|
|
|
};
|
|
|
|
|
2016-01-13 23:25:25 +08:00
|
|
|
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);
|
2016-01-13 23:25:25 +08:00
|
|
|
};
|