CartoDB-SQL-API/batch/batch.js

142 lines
3.6 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 jobStatus = require('./job_status');
2015-12-09 07:02:08 +08:00
2016-05-14 00:50:55 +08:00
function Batch(jobSubscriber, jobQueuePool, jobRunner, jobService) {
EventEmitter.call(this);
this.jobSubscriber = jobSubscriber;
this.jobQueuePool = jobQueuePool;
this.jobRunner = jobRunner;
2016-05-14 00:50:55 +08:00
this.jobService = jobService;
}
util.inherits(Batch, EventEmitter);
2016-05-14 00:50:55 +08:00
module.exports = Batch;
Batch.prototype.start = function () {
this._subscribe();
};
Batch.prototype._subscribe = function () {
var self = this;
2015-12-09 07:02:08 +08:00
this.jobSubscriber.subscribe(function (channel, host) {
var queue = self.jobQueuePool.getQueue(host);
// there is nothing to do. It is already running jobs
if (queue) {
return;
}
queue = self.jobQueuePool.createQueue(host);
2016-01-08 23:24:53 +08:00
// do forever, it does not throw a stack overflow
forever(function (next) {
self._consumeJobs(host, queue, next);
}, function (err) {
self.jobQueuePool.removeQueue(host);
if (err.name === 'EmptyQueue') {
2016-05-18 17:06:49 +08:00
return debug(err.message);
}
2016-05-18 17:06:49 +08:00
debug(err);
});
});
};
2016-05-14 00:50:55 +08:00
Batch.prototype._consumeJobs = function (host, queue, callback) {
var self = this;
2016-05-14 00:50:55 +08:00
queue.dequeue(host, function (err, job_id) {
if (err) {
return callback(err);
}
if (!job_id) {
var emptyQueueError = new Error('Queue ' + host + ' is empty');
emptyQueueError.name = 'EmptyQueue';
return callback(emptyQueueError);
}
self.jobQueuePool.setCurrentJobId(host, job_id);
self.jobRunner.run(job_id, function (err, job) {
self.jobQueuePool.removeCurrentJobId(host);
if (err && err.name === 'JobNotRunnable') {
2016-05-18 17:06:49 +08:00
debug(err.message);
2016-05-14 00:50:55 +08:00
return callback();
}
if (err) {
return callback(err);
}
if (job.data.status === jobStatus.FAILED) {
2016-05-18 17:06:49 +08:00
debug('Job %s %s in %s due to: %s', job_id, job.data.status, host, job.failed_reason);
2016-05-14 00:50:55 +08:00
} else {
2016-05-18 17:06:49 +08:00
debug('Job %s %s in %s', job_id, job.data.status, host);
2016-05-14 00:50:55 +08:00
}
self.emit('job:' + job.data.status, job_id);
callback();
});
});
};
2016-05-14 00:50:55 +08:00
Batch.prototype.drain = function (callback) {
var self = this;
var queues = this.jobQueuePool.list();
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);
} else {
2016-05-18 17:06:49 +08:00
debug('Drain complete');
}
callback();
});
};
Batch.prototype._drainJob = function (host, callback) {
var self = this;
var job_id = self.jobQueuePool.getCurrentJobId(host);
if (!job_id) {
return process.nextTick(function () {
return callback();
});
}
var queue = self.jobQueuePool.getQueue(host);
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);
}
queue.enqueueFirst(job_id, host, callback);
});
};
Batch.prototype.stop = function () {
this.jobSubscriber.unsubscribe();
};