Skip to content

Commit 6682760

Browse files
authored
feat: Adds Redis Sentinel support. (#74)
1 parent c26fea3 commit 6682760

File tree

11 files changed

+371
-40
lines changed

11 files changed

+371
-40
lines changed

package-lock.json

Lines changed: 192 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"@learninglocker/xapi-activities": "^4.0.2",
3838
"@learninglocker/xapi-agents": "^4.0.3",
3939
"@learninglocker/xapi-state": "^4.0.2",
40-
"@learninglocker/xapi-statements": "^6.0.4",
40+
"@learninglocker/xapi-statements": "^7.0.0",
4141
"boolean": "^0.1.2",
4242
"dotenv": "^5.0.0",
4343
"express": "^4.14.1",
@@ -54,6 +54,7 @@
5454
"@types/dotenv": "4.0.2",
5555
"@types/express": "4.11.1",
5656
"@types/google-cloud__storage": "1.1.7",
57+
"@types/ioredis": "3.2.5",
5758
"@types/lodash": "4.14.104",
5859
"@types/mongodb": "3.0.5",
5960
"@types/node": "9.4.6",

src/apps/AppConfig.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { S3 } from 'aws-sdk';
2+
import { Redis } from 'ioredis';
23
import Tracker from 'jscommons/dist/tracker/Tracker';
34
import { Db } from 'mongodb';
45
import { LoggerInstance } from 'winston';
@@ -35,7 +36,11 @@ export default interface AppConfig {
3536
};
3637
readonly redis: {
3738
readonly prefix: string;
38-
readonly url: string;
39+
readonly client: () => Promise<Redis>;
40+
};
41+
readonly sentinel: {
42+
readonly prefix: string;
43+
readonly client: () => Promise<Redis>;
3944
};
4045
};
4146
readonly service: {

src/apps/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default (appConfig: AppConfig): Router => {
7878
mongo: appConfig.repo.mongo,
7979
redis: appConfig.repo.redis,
8080
s3: appConfig.repo.s3,
81+
sentinel: appConfig.repo.sentinel,
8182
storageSubFolder: appConfig.repo.storageSubFolders.statements,
8283
},
8384
service: appConfig.service.statements,

src/apps/statements/AppConfig.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as S3 from 'aws-sdk/clients/s3';
2+
import { Redis } from 'ioredis';
23
import Tracker from 'jscommons/dist/tracker/Tracker';
34
import { Db } from 'mongodb';
45
import { LoggerInstance } from 'winston';
@@ -51,7 +52,11 @@ export default interface AppConfig {
5152
};
5253
readonly redis: {
5354
readonly prefix: string;
54-
readonly url: string;
55+
readonly client: () => Promise<Redis>;
56+
};
57+
readonly sentinel: {
58+
readonly prefix: string;
59+
readonly client: () => Promise<Redis>;
5560
};
5661
};
5762
}

src/apps/statements/app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ export default (appConfig: AppConfig): Result => {
1818
events: {
1919
facade: appConfig.repo.factory.eventsRepoName,
2020
redis: {
21+
client: appConfig.repo.redis.client,
2122
prefix: appConfig.repo.redis.prefix,
22-
url: appConfig.repo.redis.url,
23+
},
24+
sentinel: {
25+
client: appConfig.repo.sentinel.client,
26+
prefix: appConfig.repo.sentinel.prefix,
2327
},
2428
},
2529
models: {

src/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import getDbFromUrl from 'jscommons/dist/mongoRepo/utils/getDbFromUrl';
1010
import { defaultTo } from 'lodash';
1111
import * as os from 'os';
1212

13+
const DEFAULT_REDIS_PORT = 6379;
1314
const DEFAULT_EXPRESS_PORT = 8081;
1415
const DEFAULT_TIMEOUT_MS = 300000; // 5 minutes.
1516

@@ -71,6 +72,16 @@ export default {
7172
} as S3.ClientConfiguration,
7273
bucketName: getStringOption(process.env.FS_S3_BUCKET, 'xapi-service'),
7374
},
75+
sentinel: {
76+
name: getStringOption(process.env.SENTINEL_NAME, 'mymaster'),
77+
prefix: getStringOption(process.env.SENTINEL_PREFIX, 'LEARNINGLOCKER'),
78+
sentinels: (
79+
getStringOption(process.env.SENTINEL_CONNECTIONS, '127.0.0.1:6379').split(' ').map((conn) => {
80+
const [host, port] = conn.split(':');
81+
return { host, port: getNumberOption(port, DEFAULT_REDIS_PORT) };
82+
})
83+
),
84+
},
7485
statementsService: {
7586
awaitUpdates: getBooleanOption(defaultTo<any>(
7687
process.env.SERVICE_AWAIT_UPDATES,

src/server.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import app from './apps/app';
88
import config from './config';
99
import logger from './logger';
1010
import connectToMongoDb from './utils/connectToMongoDb';
11+
import connectToRedis from './utils/connectToRedis';
12+
import connectToSentinel from './utils/connectToSentinel';
1113

1214
const expressApp = express();
1315

@@ -20,9 +22,16 @@ expressApp.use(app({
2022
google: config.googleStorageRepo,
2123
local: config.localStorageRepo,
2224
mongo: { db: connectToMongoDb() },
23-
redis: config.redis,
25+
redis: {
26+
client: connectToRedis(),
27+
prefix: config.redis.prefix,
28+
},
2429
repoFactory: config.repoFactory,
2530
s3: config.s3StorageRepo,
31+
sentinel: {
32+
client: connectToSentinel(),
33+
prefix: config.redis.prefix,
34+
},
2635
storageSubFolders: config.storageSubFolders,
2736
},
2837
service: {

src/utils/connectToRedis.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Ioredis from 'ioredis';
2+
import { defaultTo, once } from 'lodash';
3+
import config from '../config';
4+
import logger from '../logger';
5+
6+
export default once((): () => Promise<Ioredis.Redis> => {
7+
return once(async () => {
8+
logger.info('Creating redis connection');
9+
return new Ioredis(
10+
defaultTo(config.redis.url, 'redis://127.0.0.1:6379/0'),
11+
);
12+
});
13+
});

src/utils/connectToSentinel.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as Ioredis from 'ioredis';
2+
import { defaultTo, once } from 'lodash';
3+
import config from '../config';
4+
import logger from '../logger';
5+
6+
export default once((): () => Promise<Ioredis.Redis> => {
7+
return once(async () => {
8+
logger.info('Creating sentinel connection');
9+
return new Ioredis({
10+
name: defaultTo(config.sentinel.name, 'mymaster'),
11+
sentinels: defaultTo(config.sentinel.sentinels, [{ host: '127.0.0.1', port: 6379 }]),
12+
});
13+
});
14+
});

0 commit comments

Comments
 (0)