CartoDB-SQL-API/batch/batch.js

63 lines
2.1 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;
var JobRunner = require('./job_runner');
var JobQueuePool = require('./job_queue_pool');
var JobSubscriber = require('./job_subscriber');
var UserDatabaseMetadataService = require('./user_database_metadata_service');
2015-12-09 07:02:08 +08:00
function Batch(metadataBackend) {
EventEmitter.call(this);
this.jobSubscriber = new JobSubscriber();
this.jobQueuePool = new JobQueuePool(metadataBackend);
this.jobRunner = new JobRunner(metadataBackend, new UserDatabaseMetadataService(metadataBackend));
}
util.inherits(Batch, EventEmitter);
Batch.prototype.start = function () {
var self = this;
2015-12-09 07:02:08 +08:00
this.jobSubscriber.subscribe(function (channel, host) {
var queue = self.jobQueuePool.get(host);
if (!queue) {
queue = self.jobQueuePool.add(host);
consume(queue);
}
function consume(queue) {
queue.dequeue(host, function (err, job_id) {
if (err) {
self.jobQueuePool.remove(host);
return console.error(err);
}
if (!job_id) {
self.jobQueuePool.remove(host);
return console.log('Queue %s is empty', host);
}
self.jobRunner.run(job_id)
.on('done', function (job) {
console.log('Job %s done in %s', job_id, host);
self.emit('job:done', job.job_id);
consume(queue); // recursive call
})
.on('failed', function (job) {
console.log('Job %s done in %s', job_id, host);
self.emit('job:failed', job.job_id);
consume(queue); // recursive call
})
.on('error', function (err) {
console.error('Error in job ', err.message || err);
self.emit('job:failed', job_id);
self.jobQueuePool.remove(host);
});
});
}
});
};
module.exports = Batch;