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;
|
2015-12-22 02:57:10 +08:00
|
|
|
var JobRunner = require('./job_runner');
|
2015-12-16 22:57:58 +08:00
|
|
|
var JobQueuePool = require('./job_queue_pool');
|
|
|
|
var JobSubscriber = require('./job_subscriber');
|
|
|
|
var UserDatabaseMetadataService = require('./user_database_metadata_service');
|
2015-12-31 19:33:11 +08:00
|
|
|
var forever = require('./forever');
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
function Batch(metadataBackend) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.jobSubscriber = new JobSubscriber();
|
2015-12-29 17:19:10 +08:00
|
|
|
this.jobQueuePool = new JobQueuePool(metadataBackend);
|
|
|
|
this.jobRunner = new JobRunner(metadataBackend, new UserDatabaseMetadataService(metadataBackend));
|
2015-12-23 03:12:10 +08:00
|
|
|
}
|
|
|
|
util.inherits(Batch, EventEmitter);
|
|
|
|
|
|
|
|
Batch.prototype.start = function () {
|
|
|
|
var self = this;
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2015-12-29 17:19:10 +08:00
|
|
|
this.jobSubscriber.subscribe(function (channel, host) {
|
|
|
|
var queue = self.jobQueuePool.get(host);
|
2015-12-16 22:57:58 +08:00
|
|
|
|
2015-12-31 19:33:11 +08:00
|
|
|
// there is nothing to do. It is already running jobs
|
|
|
|
if (queue) {
|
|
|
|
return;
|
2015-12-29 17:19:10 +08:00
|
|
|
}
|
2015-12-22 02:57:10 +08:00
|
|
|
|
2015-12-31 19:33:11 +08:00
|
|
|
queue = self.jobQueuePool.add(host);
|
|
|
|
|
|
|
|
// do forever, it does not cause a stack overflow
|
|
|
|
forever(function (next) {
|
|
|
|
self._consume(host, queue, next);
|
|
|
|
}, function (err) {
|
|
|
|
self.jobQueuePool.remove(host);
|
|
|
|
|
|
|
|
if (err.name === 'EmptyQueue') {
|
|
|
|
return console.log(err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Batch.prototype._consume = function consume(host, queue, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
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);
|
2015-12-16 22:57:58 +08:00
|
|
|
}
|
2015-12-31 19:33:11 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
callback();
|
|
|
|
})
|
|
|
|
.on('failed', function (job) {
|
|
|
|
console.log('Job %s failed in %s', job_id, host);
|
|
|
|
self.emit('job:failed', job.job_id);
|
|
|
|
callback();
|
|
|
|
})
|
|
|
|
.on('error', function (err) {
|
|
|
|
console.error('Error in job %s due to', job_id, err.message || err);
|
|
|
|
self.emit('job:failed', job_id);
|
|
|
|
callback(err);
|
|
|
|
});
|
2015-12-16 22:57:58 +08:00
|
|
|
});
|
2015-12-23 03:12:10 +08:00
|
|
|
};
|
2015-12-17 01:13:48 +08:00
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
module.exports = Batch;
|