Skip to content

Commit

Permalink
Revert "Removed ioredis/ioredis-mock, added Redis from Satellite inst…
Browse files Browse the repository at this point in the history
…ead (#2615)"

This reverts commit 0ddaa15.
  • Loading branch information
TDDR authored and DukeManh committed Jan 20, 2022
1 parent cc3aef1 commit dece55b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@bull-board/express": "3.8.1",
"@elastic/elasticsearch": "7.16.0",
"@elastic/elasticsearch-mock": "0.3.1",
"@senecacdot/satellite": "^1.x",
"@wordpress/wordcount": "2.15.2",
"babel-jest": "27.4.6",
"bull": "3.29.3",
Expand All @@ -65,6 +64,8 @@
"helmet": "4.6.0",
"highlight.js": "11.3.1",
"http-proxy-middleware": "2.0.1",
"ioredis": "4.28.2",
"ioredis-mock": "5.8.1",
"jsdom": "18.1.1",
"node-fetch": "2.6.6",
"normalize-url": "6.1.0",
Expand Down
10 changes: 8 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/api/posts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@senecacdot/satellite": "^1.x",
"bull": "3.29.3",
"express-validator": "6.14.0",
"ioredis": "4.28.2",
"ioredis-mock": "5.8.1",
"jsdom": "18.1.1",
"normalize-url": "6.1.0"
},
Expand Down
37 changes: 34 additions & 3 deletions src/backend/lib/redis.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
const { Redis } = require('@senecacdot/satellite');
require('./config');
const Redis = require('ioredis');
const MockRedis = require('ioredis-mock');
const { logger } = require('../utils/logger');
const parseUrl = require('../utils/url-parser');

// If you need to set the Redis URL, do it in REDIS_URL
const redisUrl =
parseUrl(process.env.REDIS_URL, process.env.REDIS_PORT) || 'redis://127.0.0.1:6379';

// Set MOCK_REDIS=1 to mock, MOCK_REDIS= to use real redis
const useMockRedis = process.env.MOCK_REDIS;

// RedisConstructor is one of Redis or MockRedis
const RedisConstructor = useMockRedis ? MockRedis : Redis;

function createRedisClient() {
try {
const { port, host } = new URL(redisUrl);
return new RedisConstructor(port, host, { password: process.env.REDIS_PASSWORD });
} catch (error) {
const message = `Unable to parse port and host from "${redisUrl}"`;
logger.error({ error }, message);
throw new Error(message);
}
}

// If using MockRedis, shim info() until https://github.com/stipsan/ioredis-mock/issues/841 ships
if (useMockRedis && typeof MockRedis.prototype.info !== 'function') {
logger.debug('Shimming MockRedis info() method');
MockRedis.prototype.info = () => Promise.resolve('redis_version:999.999.999');
}

module.exports = {
// If callers need to create a new redis instance, they'll use the ctor
createRedisClient: Redis,
createRedisClient,
// Otherwise they can use this shared instance (most should use this)
redis: Redis(),
redis: createRedisClient(),
};

0 comments on commit dece55b

Please sign in to comment.