Skip to content

Commit

Permalink
Removed ioredis/ioredis-mock, added Redis from Satellite instead (Sen…
Browse files Browse the repository at this point in the history
  • Loading branch information
TueeNguyen authored Jan 18, 2022
1 parent 837a847 commit 0ddaa15
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 46 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@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 @@ -64,8 +65,6 @@
"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: 2 additions & 8 deletions pnpm-lock.yaml

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

2 changes: 0 additions & 2 deletions src/api/posts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"@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: 3 additions & 34 deletions src/backend/lib/redis.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
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');
}
const { Redis } = require('@senecacdot/satellite');

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

0 comments on commit 0ddaa15

Please sign in to comment.