2015-12-29 17:19:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-18 00:16:52 +08:00
|
|
|
var debug = require('./util/debug')('queue');
|
|
|
|
|
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,
|
2016-10-13 03:32:29 +08:00
|
|
|
PREFIX: 'batch:queue:'
|
2016-10-12 23:53:03 +08:00
|
|
|
};
|
|
|
|
module.exports.QUEUE = QUEUE;
|
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
JobQueue.prototype.enqueue = function (user, jobId, callback) {
|
2016-10-18 00:16:52 +08:00
|
|
|
debug('JobQueue.enqueue user=%s, jobId=%s', user, jobId);
|
2016-10-13 03:32:29 +08:00
|
|
|
this.metadataBackend.redisCmd(QUEUE.DB, 'LPUSH', [ QUEUE.PREFIX + user, jobId ], function (err) {
|
2016-06-30 00:29:53 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:16:52 +08:00
|
|
|
this.jobPublisher.publish(user);
|
2016-06-30 00:29:53 +08:00
|
|
|
callback();
|
2016-10-18 00:16:52 +08:00
|
|
|
}.bind(this));
|
2015-12-29 17:19:10 +08:00
|
|
|
};
|
|
|
|
|
2016-10-13 04:40:09 +08:00
|
|
|
JobQueue.prototype.size = function (user, callback) {
|
|
|
|
this.metadataBackend.redisCmd(QUEUE.DB, 'LLEN', [ QUEUE.PREFIX + user ], callback);
|
|
|
|
};
|
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
JobQueue.prototype.dequeue = function (user, callback) {
|
2016-10-18 00:16:52 +08:00
|
|
|
this.metadataBackend.redisCmd(QUEUE.DB, 'RPOP', [ QUEUE.PREFIX + user ], function(err, jobId) {
|
|
|
|
debug('JobQueue.dequeued user=%s, jobId=%s', user, jobId);
|
|
|
|
return callback(err, jobId);
|
|
|
|
});
|
2015-12-29 17:19:10 +08:00
|
|
|
};
|
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
JobQueue.prototype.enqueueFirst = function (user, jobId, callback) {
|
2016-10-18 00:16:52 +08:00
|
|
|
debug('JobQueue.enqueueFirst user=%s, jobId=%s', user, jobId);
|
2016-10-13 03:32:29 +08:00
|
|
|
this.metadataBackend.redisCmd(QUEUE.DB, 'RPUSH', [ QUEUE.PREFIX + user, jobId ], callback);
|
2016-01-13 23:25:25 +08:00
|
|
|
};
|