From cffcb458391517dc3f8b3ab92ac7292abb6caa44 Mon Sep 17 00:00:00 2001 From: jcw- Date: Tue, 3 Mar 2020 01:59:54 -0700 Subject: [PATCH] Use options pattern in constructor --- .../orchestrator/PreAggregations.js | 2 +- .../orchestrator/QueryCache.js | 2 +- .../orchestrator/RedisCacheDriver.js | 4 ++-- .../orchestrator/RedisPool.js | 11 ++++++----- .../cubejs-query-orchestrator/test/QueryQueue.test.js | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/cubejs-query-orchestrator/orchestrator/PreAggregations.js b/packages/cubejs-query-orchestrator/orchestrator/PreAggregations.js index 3a834b48bd227..c73649a4bc9fd 100644 --- a/packages/cubejs-query-orchestrator/orchestrator/PreAggregations.js +++ b/packages/cubejs-query-orchestrator/orchestrator/PreAggregations.js @@ -523,7 +523,7 @@ class PreAggregations { this.logger = logger; this.queryCache = queryCache; this.cacheDriver = options.cacheAndQueueDriver === 'redis' ? - new RedisCacheDriver(options.redisPool) : + new RedisCacheDriver({ pool: options.redisPool }) : new LocalCacheDriver(); this.externalDriverFactory = options.externalDriverFactory; } diff --git a/packages/cubejs-query-orchestrator/orchestrator/QueryCache.js b/packages/cubejs-query-orchestrator/orchestrator/QueryCache.js index 9862586baf54d..3e533a85cc3e2 100644 --- a/packages/cubejs-query-orchestrator/orchestrator/QueryCache.js +++ b/packages/cubejs-query-orchestrator/orchestrator/QueryCache.js @@ -12,7 +12,7 @@ class QueryCache { this.externalDriverFactory = options.externalDriverFactory; this.logger = logger; this.cacheDriver = options.cacheAndQueueDriver === 'redis' ? - new RedisCacheDriver(options.redisPool) : + new RedisCacheDriver({ pool: options.redisPool }) : new LocalCacheDriver(); } diff --git a/packages/cubejs-query-orchestrator/orchestrator/RedisCacheDriver.js b/packages/cubejs-query-orchestrator/orchestrator/RedisCacheDriver.js index ed2a8cccd44e6..85a88fc600f21 100644 --- a/packages/cubejs-query-orchestrator/orchestrator/RedisCacheDriver.js +++ b/packages/cubejs-query-orchestrator/orchestrator/RedisCacheDriver.js @@ -1,10 +1,10 @@ class RedisCacheDriver { - constructor(pool) { + constructor({ pool }) { this.redisPool = pool; } async getClient() { - return await this.redisPool.getClient(); + return this.redisPool.getClient(); } async get(key) { diff --git a/packages/cubejs-query-orchestrator/orchestrator/RedisPool.js b/packages/cubejs-query-orchestrator/orchestrator/RedisPool.js index f1f0c7ada1d36..ce34de6148c2e 100644 --- a/packages/cubejs-query-orchestrator/orchestrator/RedisPool.js +++ b/packages/cubejs-query-orchestrator/orchestrator/RedisPool.js @@ -2,13 +2,14 @@ const genericPool = require('generic-pool'); const createRedisClient = require('./RedisFactory'); class RedisPool { - constructor(poolMin, poolMax, createClient, destroyClient) { + constructor(options) { + options = options || {}; const defaultMin = process.env.CUBEJS_REDIS_POOL_MIN ? parseInt(process.env.CUBEJS_REDIS_POOL_MIN, 10) : 2; const defaultMax = process.env.CUBEJS_REDIS_POOL_MAX ? parseInt(process.env.CUBEJS_REDIS_POOL_MAX, 10) : 1000; - const min = (typeof poolMin !== 'undefined') ? poolMin : defaultMin; - const max = (typeof poolMax !== 'undefined') ? poolMax : defaultMax; - const create = createClient || (() => createRedisClient(process.env.REDIS_URL)); - const destroy = destroyClient || (client => client.end(true)); + const min = (typeof options.poolMin !== 'undefined') ? options.poolMin : defaultMin; + const max = (typeof options.poolMax !== 'undefined') ? options.poolMax : defaultMax; + const create = options.createClient || (() => createRedisClient(process.env.REDIS_URL)); + const destroy = options.destroyClient || (client => client.end(true)); const opts = { min, max, diff --git a/packages/cubejs-query-orchestrator/test/QueryQueue.test.js b/packages/cubejs-query-orchestrator/test/QueryQueue.test.js index e4154cfdf6fb9..9c9a2c9c65691 100644 --- a/packages/cubejs-query-orchestrator/test/QueryQueue.test.js +++ b/packages/cubejs-query-orchestrator/test/QueryQueue.test.js @@ -141,4 +141,4 @@ const QueryQueueTest = (name, options) => { QueryQueueTest('Local'); QueryQueueTest('RedisPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool() }); -QueryQueueTest('RedisNoPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool(0, 0) }); +QueryQueueTest('RedisNoPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool({ poolMin: 0, poolMax: 0 }) });