2015-12-09 07:02:08 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function DatabaseDequeuer(userDatabaseQueue, metadataBackend, jobCounter) {
|
|
|
|
this.userDatabaseQueue = userDatabaseQueue;
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.jobCounter = jobCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabaseDequeuer.prototype.dequeue = function (callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.userDatabaseQueue.dequeue(function (err, userDatabaseName) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userDatabaseName) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:18:33 +08:00
|
|
|
console.log('>>>>>>>>>>>>>>>>>>>>>>>> 1');
|
|
|
|
|
2015-12-09 07:02:08 +08:00
|
|
|
self.metadataBackend.getAllUserDBParams(userDatabaseName, function (err, userDatabase) {
|
2015-12-09 19:18:33 +08:00
|
|
|
console.log('>>>>>>>>>>>>>>>>>>>>>>>> 2');
|
2015-12-09 07:02:08 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.jobCounter.increment(userDatabase.dbHost)) {
|
|
|
|
return callback(null, userDatabase);
|
|
|
|
}
|
|
|
|
|
|
|
|
// host is busy, enqueue job again!
|
|
|
|
this.userDatabaseQueue.enqueue(userDatabaseName, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = DatabaseDequeuer;
|