2015-12-09 07:02:08 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-23 06:13:33 +08:00
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2016-05-18 17:06:49 +08:00
|
|
|
var debug = require('./util/debug')('batch');
|
2016-05-18 17:55:58 +08:00
|
|
|
var forever = require('./util/forever');
|
2016-01-13 23:25:25 +08:00
|
|
|
var queue = require('queue-async');
|
2016-10-11 01:54:31 +08:00
|
|
|
var Locker = require('./leader/locker');
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2016-10-11 01:47:50 +08:00
|
|
|
function Batch(name, jobSubscriber, jobQueuePool, jobRunner, jobService, jobPublisher, redisConfig, logger) {
|
2015-12-23 03:12:10 +08:00
|
|
|
EventEmitter.call(this);
|
2016-10-11 01:46:07 +08:00
|
|
|
this.name = name || 'batch';
|
2016-01-08 18:32:01 +08:00
|
|
|
this.jobSubscriber = jobSubscriber;
|
|
|
|
this.jobQueuePool = jobQueuePool;
|
|
|
|
this.jobRunner = jobRunner;
|
2016-05-14 00:50:55 +08:00
|
|
|
this.jobService = jobService;
|
2016-10-11 01:47:50 +08:00
|
|
|
this.jobPublisher = jobPublisher;
|
2016-09-29 21:09:36 +08:00
|
|
|
this.logger = logger;
|
2016-10-11 01:54:31 +08:00
|
|
|
this.locker = Locker.create('redis-distlock', { redisConfig: redisConfig });
|
2015-12-23 03:12:10 +08:00
|
|
|
}
|
|
|
|
util.inherits(Batch, EventEmitter);
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
module.exports = Batch;
|
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
Batch.prototype.start = function () {
|
2016-01-13 23:25:25 +08:00
|
|
|
this._subscribe();
|
|
|
|
};
|
|
|
|
|
|
|
|
Batch.prototype._subscribe = function () {
|
|
|
|
var self = this;
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2016-10-12 01:45:26 +08:00
|
|
|
this.jobSubscriber.subscribe(function onJobHandler(host) {
|
2016-10-11 01:54:31 +08:00
|
|
|
self.locker.lock(host, 5000, function(err) {
|
|
|
|
if (err) {
|
|
|
|
debug('On message could not lock host=%s from %s. Reason: %s', host, self.name, err.message);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-31 19:33:11 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
debug('On message locked host=%s from %s', host, self.name);
|
|
|
|
var queue = self.jobQueuePool.getQueue(host);
|
2015-12-31 19:33:11 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
// there is nothing to do. It is already running jobs
|
|
|
|
if (queue) {
|
|
|
|
return;
|
2015-12-31 19:33:11 +08:00
|
|
|
}
|
2016-10-11 01:54:31 +08:00
|
|
|
queue = self.jobQueuePool.createQueue(host);
|
|
|
|
|
|
|
|
// do forever, it does not throw a stack overflow
|
|
|
|
forever(function (next) {
|
|
|
|
self._consumeJobs(host, queue, next);
|
|
|
|
}, function (err) {
|
|
|
|
self.jobQueuePool.removeQueue(host);
|
2015-12-31 19:33:11 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
if (err.name === 'EmptyQueue') {
|
|
|
|
return debug(err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(err);
|
|
|
|
});
|
2015-12-31 19:33:11 +08:00
|
|
|
});
|
2016-07-22 19:47:14 +08:00
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('ready');
|
2015-12-31 19:33:11 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
|
|
|
|
Batch.prototype._consumeJobs = function (host, queue, callback) {
|
2016-01-13 23:25:25 +08:00
|
|
|
var self = this;
|
2016-10-11 01:54:31 +08:00
|
|
|
this.locker.lock(host, 5000, function(err) {
|
|
|
|
// we didn't get the lock for the host
|
2016-05-14 00:50:55 +08:00
|
|
|
if (err) {
|
2016-10-11 01:54:31 +08:00
|
|
|
debug('On de-queue could not lock host=%s from %s. Reason: %s', host, self.name, err.message);
|
|
|
|
// In case we have lost the lock but there are pending jobs we re-announce the host
|
|
|
|
self.jobPublisher.publish(host);
|
|
|
|
return callback(new Error('Could not acquire lock for host=' + host));
|
2016-05-14 00:50:55 +08:00
|
|
|
}
|
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
debug('On de-queue locked host=%s from %s', host, self.name);
|
|
|
|
|
|
|
|
var lockRenewalIntervalId = setInterval(function() {
|
|
|
|
debug('Trying to extend lock host=%s', host);
|
|
|
|
self.locker.lock(host, 5000, function(err, _lock) {
|
|
|
|
if (err) {
|
|
|
|
clearInterval(lockRenewalIntervalId);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
if (!err && _lock) {
|
|
|
|
debug('Extended lock host=%s', host);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
queue.dequeue(host, function (err, job_id) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
if (!job_id) {
|
|
|
|
clearInterval(lockRenewalIntervalId);
|
|
|
|
return self.locker.unlock(host, function() {
|
|
|
|
var emptyQueueError = new Error('Queue ' + host + ' is empty');
|
|
|
|
emptyQueueError.name = 'EmptyQueue';
|
|
|
|
return callback(emptyQueueError);
|
|
|
|
});
|
|
|
|
}
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
self.jobQueuePool.setCurrentJobId(host, job_id);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
self.jobRunner.run(job_id, function (err, job) {
|
|
|
|
self.jobQueuePool.removeCurrentJobId(host);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
if (err && err.name === 'JobNotRunnable') {
|
|
|
|
debug(err.message);
|
|
|
|
clearInterval(lockRenewalIntervalId);
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
clearInterval(lockRenewalIntervalId);
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
debug('Job[%s] status=%s in host=%s (error=%s)', job_id, job.data.status, host, job.failed_reason);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
self.logger.log(job);
|
2016-09-29 21:09:36 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
self.emit('job:' + job.data.status, job_id);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-11 01:54:31 +08:00
|
|
|
clearInterval(lockRenewalIntervalId);
|
|
|
|
callback();
|
|
|
|
});
|
2016-05-14 00:50:55 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-01-13 23:25:25 +08:00
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
Batch.prototype.drain = function (callback) {
|
|
|
|
var self = this;
|
|
|
|
var queues = this.jobQueuePool.list();
|
2016-01-13 23:25:25 +08:00
|
|
|
var batchQueues = queue(queues.length);
|
|
|
|
|
|
|
|
queues.forEach(function (host) {
|
|
|
|
batchQueues.defer(self._drainJob.bind(self), host);
|
|
|
|
});
|
|
|
|
|
|
|
|
batchQueues.awaitAll(function (err) {
|
|
|
|
if (err) {
|
2016-05-18 17:06:49 +08:00
|
|
|
debug('Something went wrong draining', err);
|
2016-01-13 23:25:25 +08:00
|
|
|
} else {
|
2016-05-18 17:06:49 +08:00
|
|
|
debug('Drain complete');
|
2016-01-13 23:25:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Batch.prototype._drainJob = function (host, callback) {
|
|
|
|
var self = this;
|
2016-01-25 21:51:37 +08:00
|
|
|
var job_id = self.jobQueuePool.getCurrentJobId(host);
|
|
|
|
|
|
|
|
if (!job_id) {
|
|
|
|
return process.nextTick(function () {
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var queue = self.jobQueuePool.getQueue(host);
|
2016-01-13 23:25:25 +08:00
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
this.jobService.drain(job_id, function (err) {
|
2016-01-26 03:07:41 +08:00
|
|
|
if (err && err.name === 'CancelNotAllowedError') {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
2016-01-13 23:25:25 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.enqueueFirst(job_id, host, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-01-25 21:51:37 +08:00
|
|
|
Batch.prototype.stop = function () {
|
2016-01-06 00:42:28 +08:00
|
|
|
this.jobSubscriber.unsubscribe();
|
|
|
|
};
|