CartoDB-SQL-API/batch/batch.js

162 lines
4.5 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');
var queue = require('queue-async');
var HostScheduler = require('./scheduler/host-scheduler');
var EMPTY_QUEUE = true;
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-27 22:55:41 +08:00
this.hostScheduler = new HostScheduler(this.name, { run: this.processJob.bind(this) }, redisPool);
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 () {
var self = this;
2015-12-09 07:02:08 +08:00
this.jobSubscriber.subscribe(
function onJobHandler(user, host) {
2016-10-19 22:58:00 +08:00
debug('[%s] onJobHandler(%s, %s)', self.name, user, host);
2016-10-19 16:36:13 +08:00
self.hostScheduler.add(host, user, function(err) {
if (err) {
return debug(
'Could not schedule host=%s user=%s from %s. Reason: %s',
host, self.name, user, err.message
);
}
});
},
function onJobSubscriberReady(err) {
if (err) {
return self.emit('error', err);
2016-10-12 06:10:40 +08:00
}
self.emit('ready');
}
);
};
Batch.prototype.processJob = function (user, callback) {
var self = this;
self.jobQueue.dequeue(user, function (err, jobId) {
2016-05-14 00:50:55 +08:00
if (err) {
2016-10-19 22:58:31 +08:00
return callback(new Error('Could not get job from "' + user + '". Reason: ' + err.message), !EMPTY_QUEUE);
2016-05-14 00:50:55 +08:00
}
if (!jobId) {
debug('Queue empty user=%s', user);
return callback(null, EMPTY_QUEUE);
}
2016-05-14 00:50:55 +08:00
self.setWorkInProgressJob(user, jobId, function (err) {
if (err) {
return callback(new Error('Could not add job to work-in-progress list. Reason: ' + err.message));
}
self.jobRunner.run(jobId, function (err, job) {
self.clearWorkInProgressJob(user);
2016-05-14 00:50:55 +08:00
if (err) {
debug(err);
if (err.name === 'JobNotRunnable') {
return callback(null, !EMPTY_QUEUE);
}
return callback(err, !EMPTY_QUEUE);
}
debug(
'[%s] Job=%s status=%s user=%s (failed_reason=%s)',
self.name, jobId, job.data.status, user, job.failed_reason
);
self.logger.log(job);
return callback(null, !EMPTY_QUEUE);
});
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);
};
2016-10-18 00:42:29 +08:00
/* Work in progress jobs */
Batch.prototype.setWorkInProgressJob = function(user, jobId, callback) {
2016-10-18 00:42:29 +08:00
this.workInProgressJobs[user] = jobId;
this.jobService.addWorkInProgressJob(user, jobId, callback);
2016-10-18 00:42:29 +08:00
};
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);
};