2015-12-09 07:02:08 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-10 03:17:45 +08:00
|
|
|
function BatchManager(usernameQueue, userDatabaseMetadataService, jobService, jobCounterService) {
|
|
|
|
this.usernameQueue = usernameQueue;
|
|
|
|
this.userDatabaseMetadataService = userDatabaseMetadataService;
|
|
|
|
this.jobService = jobService;
|
|
|
|
this.jobCounterService = jobCounterService;
|
2015-12-09 07:02:08 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 03:17:45 +08:00
|
|
|
BatchManager.prototype.run = function (callback) {
|
2015-12-09 07:02:08 +08:00
|
|
|
var self = this;
|
|
|
|
|
2015-12-10 03:17:45 +08:00
|
|
|
this.usernameQueue.dequeue(function (err, username) {
|
2015-12-09 07:02:08 +08:00
|
|
|
if (err) {
|
2015-12-10 03:17:45 +08:00
|
|
|
return callback(err);
|
2015-12-09 07:02:08 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 03:17:45 +08:00
|
|
|
if (!username) {
|
2015-12-10 22:08:31 +08:00
|
|
|
return callback(); // no jobs scheduled
|
2015-12-09 07:02:08 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 03:17:45 +08:00
|
|
|
self.userDatabaseMetadataService.getUserMetadata(username, function (err, userDatabaseMetadata) {
|
2015-12-09 07:02:08 +08:00
|
|
|
if (err) {
|
2015-12-10 03:17:45 +08:00
|
|
|
return callback(err);
|
2015-12-09 07:02:08 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 03:17:45 +08:00
|
|
|
self.jobCounterService.increment(userDatabaseMetadata.host, function (err) {
|
2015-12-12 01:08:19 +08:00
|
|
|
if (err && err.name === 'JobLimitReachedError') {
|
|
|
|
self.usernameQueue.enqueue(username, function (err) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
} else if (err) {
|
2015-12-10 03:17:45 +08:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobService.run(userDatabaseMetadata, function (err) {
|
|
|
|
if (err) {
|
|
|
|
self.usernameQueue.enqueue(username, function (err) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
}
|
2015-12-10 22:08:31 +08:00
|
|
|
callback();
|
2015-12-10 03:17:45 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobCounterService.decrement(userDatabaseMetadata.host, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-12-09 07:02:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = BatchManager;
|