import bluebird from "bluebird"; import redis from "redis"; import logger from "./logger"; bluebird.promisifyAll(redis.RedisClient.prototype); bluebird.promisifyAll(redis.Multi.prototype); let client = null; const RedisClient = () => { try { if (!client) { client = redis.createClient({ host: process.env.REDIS_HOST, port: process.env.REDIS_PORT, password: process.env.REDIS_PASS, retry_strategy: (options) => { if (options.error && options.error.code === "ECONNREFUSED") { // End reconnecting if there is any specific error return new Error("The server refused the connection"); } if (options.total_retry_time > 1000 * 60 * 5) { // End reconnecting after 5 minues and flush all commands with a individual error return new Error("Retry time exhausted"); } if (options.attempt > 10) { // End reconnecting with built in error return undefined; } // reconnect after return Math.min(options.attempt * 100, 3000); }, }); } return client; } catch (e) { logger.error("::: failed to connect to redis :::"); return null; } }; export default { getInstance: RedisClient };