-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Replacing redis client to ioredis that support Sentinel; replace in-memory to ioredis-mock * A lot of test fixes
- Loading branch information
Showing
48 changed files
with
1,198 additions
and
994 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,53 @@ | ||
const logger = require('./logger').db; | ||
const redisCommands = require('redis-commands'); | ||
require('util.promisify/shim')(); // NOTE: shim for native node 8.0 uril.promisify | ||
const util = require('util'); | ||
const config = require('./config'); | ||
const fs = require('fs'); | ||
let db; | ||
const redisOptions = config.systemConfig.db && config.systemConfig.db.redis; | ||
|
||
module.exports = function () { | ||
if (db) { | ||
return db; | ||
} | ||
const config = require('./config'); | ||
const redisOptions = config.systemConfig.db && config.systemConfig.db.redis; | ||
|
||
// special mode, will emulate all redis commands. | ||
// designed for demo and test scenarious to avoid having real Redis instance | ||
const emulate = process.argv[2] === 'emulate' || redisOptions.emulate; | ||
|
||
const redis = require(emulate ? 'fakeredis' : 'redis'); | ||
promisify(redis.RedisClient.prototype, redisCommands.list); | ||
promisify(redis.Multi.prototype, ['exec', 'execAtomic']); | ||
|
||
function promisify (obj, methods) { | ||
methods.forEach((method) => { | ||
if (obj[method]) { | ||
obj[method + 'Async'] = util.promisify(obj[method]); | ||
} | ||
}); | ||
} | ||
// special mode, will emulate all redis commands. | ||
// designed for demo and test scenarious to avoid having real Redis instance | ||
let emulate = process.argv[2] === 'emulate' || redisOptions.emulate; | ||
|
||
// TLS for redis, allowing for TLS options to be specified as file paths. | ||
if (redisOptions.tls) { | ||
if (redisOptions.tls.keyFile) { | ||
redisOptions.tls.key = fs.readFileSync(redisOptions.tls.keyFile); | ||
} | ||
if (process.env.EG_DB_EMULATE) { | ||
emulate = !!parseInt(process.env.EG_DB_EMULATE); | ||
} | ||
|
||
if (redisOptions.tls.certFile) { | ||
redisOptions.tls.cert = fs.readFileSync(redisOptions.tls.certFile); | ||
} | ||
if (redisOptions.tls) { | ||
if (redisOptions.tls.keyFile) { | ||
redisOptions.tls.key = fs.readFileSync(redisOptions.tls.keyFile); | ||
}; | ||
|
||
if (redisOptions.tls.caFile) { | ||
redisOptions.tls.ca = fs.readFileSync(redisOptions.tls.caFile); | ||
} | ||
if (redisOptions.tls.certFile) { | ||
redisOptions.tls.cert = fs.readFileSync(redisOptions.tls.certFile); | ||
} | ||
|
||
db = redis.createClient(redisOptions); | ||
if (redisOptions.tls.caFile) { | ||
redisOptions.tls.ca = fs.readFileSync(redisOptions.tls.caFile); | ||
} | ||
} | ||
|
||
const Redis = require(emulate ? 'ioredis-mock' : 'ioredis'); | ||
const db = new Redis(redisOptions); | ||
|
||
// TODO: ALERT: this is temporary fix before transformers will land into ioredis-mock | ||
db.prepareHMSET = function (hashKey, obj) { | ||
const result = []; | ||
let pos = 0; | ||
for (const key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
result[pos] = key; | ||
result[pos + 1] = obj[key]; | ||
} | ||
pos += 2; | ||
} | ||
return [hashKey].concat(result); | ||
}; | ||
|
||
db.on('ready', function () { | ||
logger.debug('Redis is ready'); | ||
}); | ||
db.on('ready', function () { | ||
logger.debug('Redis is ready'); | ||
}); | ||
|
||
db.on('error', function (err) { | ||
logger.error('Error in Redis: ', err); | ||
}); | ||
db.on('error', function (err) { | ||
logger.error('Error in Redis: ', err); | ||
}); | ||
|
||
return db; | ||
}; | ||
module.exports = db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.