From d0787d03f79e4f222772bff3f20eb3ea90b30eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 14 Dec 2015 10:36:16 +0100 Subject: [PATCH] Reused matadata backend instance for batch service --- app/app.js | 2 +- batch/batch_manager_factory.js | 11 +---------- batch/index.js | 4 ++-- batch/username_queue.js | 16 ++------------- test/acceptance/batch.test.js | 11 ++++++++++- test/unit/batch/username_queue.js | 33 +++++++++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 test/unit/batch/username_queue.js diff --git a/app/app.js b/app/app.js index 655373e4..17e8e798 100644 --- a/app/app.js +++ b/app/app.js @@ -181,7 +181,7 @@ function App() { versionController.route(app); if (global.settings.environment !== 'test') { - batchService(5000, 100); + batchService(metadataBackend, 5000, 100); } return app; diff --git a/batch/batch_manager_factory.js b/batch/batch_manager_factory.js index 7843bcd3..8eaf8d66 100644 --- a/batch/batch_manager_factory.js +++ b/batch/batch_manager_factory.js @@ -1,21 +1,12 @@ 'use strict'; -var cartoDBRedis = require('cartodb-redis'); var UserDatabaseMetadataService = require('./user_database_metadata_service'); var UsernameQueue = require('./username_queue'); var JobService = require('./job_service'); var JobCounterService = require('./job_counter_service'); var BatchManager = require('./batch_manager'); -module.exports = function (maxJobsPerHost) { - var metadataBackend = cartoDBRedis({ - host: global.settings.redis_host, - port: global.settings.redis_port, - max: global.settings.redisPool, - idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, - reapIntervalMillis: global.settings.redisReapIntervalMillis - }); - +module.exports = function (metadataBackend ,maxJobsPerHost) { var usernameQueue = new UsernameQueue(metadataBackend); var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend); var jobService = new JobService(metadataBackend); diff --git a/batch/index.js b/batch/index.js index b9d9635b..89373233 100644 --- a/batch/index.js +++ b/batch/index.js @@ -3,8 +3,8 @@ var BatchLauncher = require('./batch_launcher'); var batchManagerFactory = require('./batch_manager_factory'); -module.exports = function (interval, maxJobsPerHost) { - var batchManager = batchManagerFactory(maxJobsPerHost); +module.exports = function (metadataBackend, interval, maxJobsPerHost) { + var batchManager = batchManagerFactory(metadataBackend, maxJobsPerHost); var batchLauncher = new BatchLauncher(batchManager); // here we go! diff --git a/batch/username_queue.js b/batch/username_queue.js index 8cbb97ff..380a4df6 100644 --- a/batch/username_queue.js +++ b/batch/username_queue.js @@ -11,26 +11,14 @@ UsernameQueue.prototype.enqueue = function (cdbUsername, callback) { var db = this.db; var queue = this.queueName; - this.metadataBackend.redisCmd(db, 'LPUSH', [queue, cdbUsername], function (err, cdbUsername) { - if (err) { - return callback(err); - } - - callback(null, cdbUsername); - }); + this.metadataBackend.redisCmd(db, 'LPUSH', [queue, cdbUsername], callback); }; UsernameQueue.prototype.dequeue = function (callback) { var db = this.db; var queue = this.queueName; - this.metadataBackend.redisCmd(db, 'RPOP', [queue], function (err, cdbUsername) { - if (err) { - return callback(err); - } - - callback(null, cdbUsername); - }); + this.metadataBackend.redisCmd(db, 'RPOP', [queue], callback); }; module.exports = UsernameQueue; diff --git a/test/acceptance/batch.test.js b/test/acceptance/batch.test.js index 6f9e8be9..73992276 100644 --- a/test/acceptance/batch.test.js +++ b/test/acceptance/batch.test.js @@ -3,7 +3,16 @@ var batchManagerFactory = require('../../batch/batch_manager_factory'); describe('batch manager', function() { it('run', function (done) { - batchManagerFactory().run(function (err) { + var metadataBackend = require('cartodb-redis')({ + host: global.settings.redis_host, + port: global.settings.redis_port, + max: global.settings.redisPool, + idleTimeoutMillis: global.settings.redisIdleTimeoutMillis, + reapIntervalMillis: global.settings.redisReapIntervalMillis + }); + var maxJobsPerHost = 100; + + batchManagerFactory(metadataBackend, maxJobsPerHost).run(function (err) { done(err); }); }); diff --git a/test/unit/batch/username_queue.js b/test/unit/batch/username_queue.js new file mode 100644 index 00000000..3a856764 --- /dev/null +++ b/test/unit/batch/username_queue.js @@ -0,0 +1,33 @@ +var UsernameQueue = require('../../../batch/username_queue'); +var assert = require('assert'); + +describe('batch API username queue', function () { + beforeEach(function () { + this.metadataBackend = { + redisCmd: function () { + var callback = arguments[arguments.length -1]; + process.nextTick(function () { + callback(null, 'irrelevantUsername'); + }); + } + }; + this.usernameQueue = new UsernameQueue(this.metadataBackend); + }); + + it('.enqueue(username) should enqueue the provided username', function (done) { + this.usernameQueue.enqueue('irrelevantUsername', function (err, username) { + assert.ok(!err); + assert.equal(username, 'irrelevantUsername'); + done(); + }); + }); + + it('.dequeue(username) should dequeue the next username', function (done) { + this.usernameQueue.dequeue(function (err, username) { + assert.ok(!err); + assert.equal(username, 'irrelevantUsername'); + done(); + }); + }); + +});