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');
|
2016-10-13 03:32:29 +08:00
|
|
|
var HostUserQueueMover = require('./maintenance/host-user-queue-mover');
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2016-10-17 21:02:34 +08:00
|
|
|
function Batch(name, jobSubscriber, jobQueue, jobRunner, jobService, jobPublisher, redisPool, 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;
|
2016-10-12 18:26:50 +08:00
|
|
|
this.jobQueue = jobQueue;
|
2016-01-08 18:32:01 +08:00
|
|
|
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-17 21:02:34 +08:00
|
|
|
this.locker = Locker.create('redis-distlock', { pool: redisPool });
|
|
|
|
this.hostUserQueueMover = new HostUserQueueMover(jobQueue, jobService, this.locker, redisPool);
|
2016-10-12 18:26:50 +08:00
|
|
|
|
2016-10-18 01:03:55 +08:00
|
|
|
// map: host => map{user}. Useful to determine pending queued users.
|
|
|
|
this.workingHosts = {};
|
2016-10-18 00:42:29 +08:00
|
|
|
|
|
|
|
// map: user => jobId. Will be used for draining jobs.
|
|
|
|
this.workInProgressJobs = {};
|
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-10-13 03:32:29 +08:00
|
|
|
this.hostUserQueueMover.moveOldJobs(function() {
|
|
|
|
this.subscribe();
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
Batch.prototype.subscribe = function () {
|
2016-01-13 23:25:25 +08:00
|
|
|
var self = this;
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2016-10-12 18:26:50 +08:00
|
|
|
this.jobSubscriber.subscribe(
|
2016-10-13 03:32:29 +08:00
|
|
|
function onJobHandler(user, host) {
|
|
|
|
debug('onJobHandler(%s, %s)', user, host);
|
2016-10-18 01:03:55 +08:00
|
|
|
if (self.isProcessing(host, user)) {
|
2016-10-18 17:18:11 +08:00
|
|
|
return debug('%s is already processing host=%s user=%s', self.name, host, user);
|
2016-10-12 18:26:50 +08:00
|
|
|
}
|
2015-12-31 19:33:11 +08:00
|
|
|
|
2016-10-18 01:03:55 +08:00
|
|
|
self.setProcessing(host, user);
|
|
|
|
|
2016-10-12 18:26:50 +08:00
|
|
|
// do forever, it does not throw a stack overflow
|
|
|
|
forever(
|
|
|
|
function (next) {
|
2016-10-18 01:03:55 +08:00
|
|
|
self.locker.lock(host, function(err) {
|
2016-10-12 18:26:50 +08:00
|
|
|
// we didn't get the lock for the host
|
|
|
|
if (err) {
|
2016-10-18 17:18:11 +08:00
|
|
|
debug('Could not lock host=%s from %s. Reason: %s', host, self.name, err.message);
|
2016-10-12 18:26:50 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
2016-10-18 17:18:11 +08:00
|
|
|
debug('Locked host=%s from %s', host, user, self.name);
|
2016-10-13 03:32:29 +08:00
|
|
|
self.processNextJob(user, next);
|
2016-10-12 18:26:50 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err) {
|
2016-10-12 23:42:46 +08:00
|
|
|
if (err) {
|
|
|
|
debug(err.name === 'EmptyQueue' ? err.message : err);
|
|
|
|
}
|
|
|
|
|
2016-10-18 01:03:55 +08:00
|
|
|
self.clearProcessing(host, user);
|
|
|
|
if (!self.hasPendingJobs(host)) {
|
|
|
|
self.locker.unlock(host, debug);
|
|
|
|
}
|
2016-10-12 18:26:50 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function onJobSubscriberReady(err) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
2016-10-12 06:10:40 +08:00
|
|
|
}
|
2016-10-11 01:54:31 +08:00
|
|
|
|
2016-10-12 18:26:50 +08:00
|
|
|
self.emit('ready');
|
2016-07-22 19:47:14 +08:00
|
|
|
}
|
2016-10-12 18:26:50 +08:00
|
|
|
);
|
2015-12-31 19:33:11 +08:00
|
|
|
};
|
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
Batch.prototype.processNextJob = function (user, callback) {
|
|
|
|
// This is missing the logic for processing several users within the same host
|
|
|
|
// It requires to:
|
|
|
|
// - Take care of number of jobs running at the same time per host.
|
|
|
|
// - Execute user jobs in order.
|
2016-01-13 23:25:25 +08:00
|
|
|
var self = this;
|
2016-10-13 03:32:29 +08:00
|
|
|
self.jobQueue.dequeue(user, function (err, jobId) {
|
2016-05-14 00:50:55 +08:00
|
|
|
if (err) {
|
2016-10-12 18:26:50 +08:00
|
|
|
return callback(err);
|
2016-05-14 00:50:55 +08:00
|
|
|
}
|
|
|
|
|
2016-10-12 18:26:50 +08:00
|
|
|
if (!jobId) {
|
2016-10-13 03:32:29 +08:00
|
|
|
var emptyQueueError = new Error('Queue for user="' + user + '" is empty');
|
2016-10-12 18:26:50 +08:00
|
|
|
emptyQueueError.name = 'EmptyQueue';
|
|
|
|
return callback(emptyQueueError);
|
|
|
|
}
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-18 00:42:29 +08:00
|
|
|
self.setWorkInProgressJob(user, jobId);
|
2016-10-12 18:26:50 +08:00
|
|
|
self.jobRunner.run(jobId, function (err, job) {
|
2016-10-18 00:42:29 +08:00
|
|
|
self.clearWorkInProgressJob(user);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-12 18:26:50 +08:00
|
|
|
if (err) {
|
|
|
|
debug(err);
|
|
|
|
if (err.name === 'JobNotRunnable') {
|
2016-10-11 01:54:31 +08:00
|
|
|
return callback();
|
|
|
|
}
|
2016-10-12 18:26:50 +08:00
|
|
|
return callback(err);
|
|
|
|
}
|
2016-10-11 01:54:31 +08:00
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
debug('Job=%s status=%s user=%s (failed_reason=%s)', jobId, job.data.status, user, job.failed_reason);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-12 18:26:50 +08:00
|
|
|
self.logger.log(job);
|
2016-09-29 21:09:36 +08:00
|
|
|
|
2016-10-12 18:26:50 +08:00
|
|
|
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;
|
2016-10-18 00:42:29 +08:00
|
|
|
var workingUsers = this.getWorkInProgressUsers();
|
2016-10-13 03:32:29 +08:00
|
|
|
var batchQueues = queue(workingUsers.length);
|
2016-01-13 23:25:25 +08:00
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
workingUsers.forEach(function (user) {
|
|
|
|
batchQueues.defer(self._drainJob.bind(self), user);
|
2016-01-13 23:25:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
Batch.prototype._drainJob = function (user, callback) {
|
2016-01-13 23:25:25 +08:00
|
|
|
var self = this;
|
2016-10-18 00:42:29 +08:00
|
|
|
var job_id = this.getWorkInProgressJob(user);
|
2016-01-25 21:51:37 +08:00
|
|
|
|
|
|
|
if (!job_id) {
|
|
|
|
return process.nextTick(function () {
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
self.jobQueue.enqueueFirst(user, job_id, callback);
|
2016-01-13 23:25:25 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-12 18:26:50 +08:00
|
|
|
Batch.prototype.stop = function (callback) {
|
2016-10-12 22:43:18 +08:00
|
|
|
this.removeAllListeners();
|
2016-10-12 18:26:50 +08:00
|
|
|
this.jobSubscriber.unsubscribe(callback);
|
|
|
|
};
|
|
|
|
|
2016-10-18 01:03:55 +08:00
|
|
|
|
|
|
|
/* Processing hosts => users */
|
|
|
|
|
|
|
|
Batch.prototype.setProcessing = function(host, user) {
|
|
|
|
if (!this.workingHosts.hasOwnProperty(host)) {
|
|
|
|
this.workingHosts[host] = {};
|
|
|
|
}
|
|
|
|
this.workingHosts[host][user] = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Batch.prototype.clearProcessing = function(host, user) {
|
|
|
|
if (this.workingHosts.hasOwnProperty(host)) {
|
|
|
|
delete this.workingHosts[host][user];
|
|
|
|
if (!this.hasPendingJobs(host)) {
|
|
|
|
delete this.workingHosts[host];
|
|
|
|
}
|
|
|
|
}
|
2016-10-12 18:26:50 +08:00
|
|
|
};
|
|
|
|
|
2016-10-18 01:03:55 +08:00
|
|
|
Batch.prototype.isProcessing = function(host, user) {
|
|
|
|
return this.workingHosts.hasOwnProperty(host) && this.workingHosts[host].hasOwnProperty(user);
|
2016-10-12 18:26:50 +08:00
|
|
|
};
|
|
|
|
|
2016-10-18 01:03:55 +08:00
|
|
|
Batch.prototype.hasPendingJobs = function(host) {
|
|
|
|
return this.workingHosts.hasOwnProperty(host) && Object.keys(this.workingHosts[host]).length > 0;
|
2016-01-06 00:42:28 +08:00
|
|
|
};
|
2016-10-18 00:42:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Work in progress jobs */
|
|
|
|
|
|
|
|
Batch.prototype.setWorkInProgressJob = function(user, jobId) {
|
|
|
|
this.workInProgressJobs[user] = jobId;
|
|
|
|
};
|
|
|
|
|
|
|
|
Batch.prototype.getWorkInProgressJob = function(user) {
|
|
|
|
return this.workInProgressJobs[user];
|
|
|
|
};
|
|
|
|
|
|
|
|
Batch.prototype.clearWorkInProgressJob = function(user) {
|
|
|
|
delete this.workInProgressJobs[user];
|
|
|
|
};
|
|
|
|
|
|
|
|
Batch.prototype.getWorkInProgressUsers = function() {
|
|
|
|
return Object.keys(this.workInProgressJobs);
|
|
|
|
};
|