CartoDB-SQL-API/batch/database_dequeuer.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

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();
}
self.metadataBackend.getAllUserDBParams(userDatabaseName, function (err, userDatabase) {
console.log('>>>>', userDatabaseName, userDatabase);
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;