CartoDB-SQL-API/batch/batch.js

217 lines
6.3 KiB
JavaScript
Raw Normal View History

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');
var queue = require('queue-async');
var Locker = require('./leader/locker');
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) {
EventEmitter.call(this);
this.name = name || 'batch';
this.jobSubscriber = jobSubscriber;
this.jobQueue = jobQueue;
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;
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);
// 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 = {};
}
util.inherits(Batch, EventEmitter);
2016-05-14 00:50:55 +08:00
module.exports = Batch;
Batch.prototype.start = function () {
this.hostUserQueueMover.moveOldJobs(function() {
this.subscribe();
}.bind(this));
};
Batch.prototype.subscribe = function () {
var self = this;
2015-12-09 07:02:08 +08:00
this.jobSubscriber.subscribe(
function onJobHandler(user, host) {
debug('onJobHandler(%s, %s)', user, host);
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);
}
self.setProcessing(host, user);
// do forever, it does not throw a stack overflow
forever(
function (next) {
self.locker.lock(host, function(err) {
// 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);
return next(err);
}
2016-10-18 17:18:11 +08:00
debug('Locked host=%s from %s', host, user, self.name);
self.processNextJob(user, next);
});
},
function (err) {
2016-10-12 23:42:46 +08:00
if (err) {
debug(err.name === 'EmptyQueue' ? err.message : err);
}
self.clearProcessing(host, user);
if (!self.hasPendingJobs(host)) {
self.locker.unlock(host, debug);
}
}
);
},
function onJobSubscriberReady(err) {
if (err) {
return self.emit('error', err);
2016-10-12 06:10:40 +08:00
}
self.emit('ready');
}
);
};
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.
var self = this;
self.jobQueue.dequeue(user, function (err, jobId) {
2016-05-14 00:50:55 +08:00
if (err) {
return callback(err);
2016-05-14 00:50:55 +08:00
}
if (!jobId) {
var emptyQueueError = new Error('Queue for user="' + user + '" is empty');
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);
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
if (err) {
debug(err);
if (err.name === 'JobNotRunnable') {
return callback();
}
return callback(err);
}
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
self.logger.log(job);
callback();
2016-05-14 00:50:55 +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();
var batchQueues = queue(workingUsers.length);
workingUsers.forEach(function (user) {
batchQueues.defer(self._drainJob.bind(self), user);
});
batchQueues.awaitAll(function (err) {
if (err) {
2016-05-18 17:06:49 +08:00
debug('Something went wrong draining', err);
} else {
2016-05-18 17:06:49 +08:00
debug('Drain complete');
}
callback();
});
};
Batch.prototype._drainJob = function (user, callback) {
var self = this;
2016-10-18 00:42:29 +08:00
var job_id = this.getWorkInProgressJob(user);
if (!job_id) {
return process.nextTick(function () {
return callback();
});
}
2016-05-14 00:50:55 +08:00
this.jobService.drain(job_id, function (err) {
if (err && err.name === 'CancelNotAllowedError') {
return callback();
}
if (err) {
return callback(err);
}
self.jobQueue.enqueueFirst(user, job_id, callback);
});
};
Batch.prototype.stop = function (callback) {
2016-10-12 22:43:18 +08:00
this.removeAllListeners();
this.jobSubscriber.unsubscribe(callback);
};
/* 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];
}
}
};
Batch.prototype.isProcessing = function(host, user) {
return this.workingHosts.hasOwnProperty(host) && this.workingHosts[host].hasOwnProperty(user);
};
Batch.prototype.hasPendingJobs = function(host) {
return this.workingHosts.hasOwnProperty(host) && Object.keys(this.workingHosts[host]).length > 0;
};
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);
};