-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff7c212
commit 7def2c9
Showing
39 changed files
with
635 additions
and
621 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,24 @@ | ||
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 fs = require('fs'); | ||
let db; | ||
const config = require('./config'); | ||
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 | ||
let emulate = process.argv[2] === 'emulate' || redisOptions.emulate; | ||
|
||
// 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; | ||
if (process.env.EG_DB_EMULATE) { | ||
emulate = !!parseInt(process.env.EG_DB_EMULATE); | ||
} | ||
|
||
const redis = require(emulate ? 'fakeredis' : 'redis'); | ||
promisify(redis.RedisClient.prototype, redisCommands.list); | ||
promisify(redis.Multi.prototype, ['exec', 'execAtomic']); | ||
const Redis = require(emulate ? 'ioredis-mock' : 'ioredis'); | ||
const db = new Redis(redisOptions); | ||
|
||
function promisify (obj, methods) { | ||
methods.forEach((method) => { | ||
if (obj[method]) { | ||
obj[method + 'Async'] = util.promisify(obj[method]); | ||
} | ||
}); | ||
} | ||
db.on('ready', function () { | ||
logger.debug('Redis is ready'); | ||
}); | ||
|
||
// 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); | ||
} | ||
db.on('error', function (err) { | ||
logger.error('Error in Redis: ', err); | ||
}); | ||
|
||
if (redisOptions.tls.certFile) { | ||
redisOptions.tls.cert = fs.readFileSync(redisOptions.tls.certFile); | ||
} | ||
|
||
if (redisOptions.tls.caFile) { | ||
redisOptions.tls.ca = fs.readFileSync(redisOptions.tls.caFile); | ||
} | ||
} | ||
|
||
db = redis.createClient(redisOptions); | ||
|
||
db.on('ready', function () { | ||
logger.debug('Redis is ready'); | ||
}); | ||
|
||
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
Oops, something went wrong.