Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to ioredis package over node-redis #585

Merged
merged 7 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CommonServer/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,19 @@ export const HttpProtocol: Protocol = (

// Redis does not require password.
export const RedisHostname: string = process.env['REDIS_HOST'] || 'redis';
export const RedisPort: Port = new Port(process.env['REDIS_PORT'] || '6379');
export const RedisDb: number = Number(process.env['REDIS_DB']) || 0;
export const RedisUsername: string = process.env['REDIS_USERNAME'] || 'default';
export const RedisPassword: string =
process.env['REDIS_PASSWORD'] || 'password';
export const RedisPort: Port = new Port(process.env['REDIS_PORT'] || '6379');
export const RedisTlsCa: string | undefined =
process.env['REDIS_TLS_CA'] || undefined;
export const RedisTlsSentinelMode: boolean =
process.env['REDIS_TLS_SENTINEL_MODE'] === 'true';

export const ShouldRedisTlsEnable: boolean = Boolean(
RedisTlsCa || RedisTlsSentinelMode
);

export const DashboardApiRoute: Route = new Route(
process.env['DASHBOARD_API_ROUTE'] || '/dashboard-api'
Expand Down
46 changes: 31 additions & 15 deletions CommonServer/Infrastructure/Redis.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
import Sleep from 'Common/Types/Sleep';
import { createClient, RedisClientType } from 'redis';
import { RedisHostname, RedisPassword, RedisPort } from '../Config';
// import { createClient, RedisClientType } from 'redis';
import { Redis as RedisClient } from 'ioredis';
import {
RedisHostname,
RedisPort,
RedisUsername,
RedisPassword,
RedisDb,
RedisTlsCa,
RedisTlsSentinelMode,
ShouldRedisTlsEnable,
} from '../Config';
import logger from '../Utils/Logger';

export type ClientType = RedisClientType;
export type ClientType = RedisClient;

export default abstract class Redis {
private static client: RedisClientType | null = null;
private static client: RedisClient | null = null;

public static isConnected(): boolean {
if (!this.client) {
return false;
}

return this.client.isReady;
return this.client.status === 'ready';
}

public static getClient(): RedisClientType | null {
public static getClient(): RedisClient | null {
return this.client;
}

public static async connect(): Promise<RedisClientType> {
public static async connect(): Promise<RedisClient> {
let retry: number = 0;

try {
this.client = createClient({
const tlsOptions: object = ShouldRedisTlsEnable
? { ca: RedisTlsCa }
: {};
this.client = new RedisClient({
host: RedisHostname,
port: RedisPort.toNumber(),
username: RedisUsername,
password: RedisPassword,
socket: {
host: RedisHostname,
port: RedisPort.toNumber(),
},
db: RedisDb,
enableTLSForSentinelMode: RedisTlsSentinelMode,
tls: tlsOptions,
lazyConnect: true,
});

const connectToDatabase: Function = async (
client: RedisClientType
client: RedisClient
): Promise<void> => {
try {
await client.connect();
Expand Down Expand Up @@ -66,9 +82,9 @@ export default abstract class Redis {
}
}

public static async disconnect(): Promise<void> {
public static disconnect(): void {
if (this.isConnected()) {
await this.client?.disconnect();
this.client?.disconnect();
this.client = null;
}
}
Expand Down
1 change: 1 addition & 0 deletions CommonServer/Utils/StartServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import JSONFunctions from 'Common/Types/JSONFunctions';

const app: ExpressApplication = Express.getExpressApp();

app.disable('x-powered-by');
app.set('port', process.env['PORT']);
app.set('view engine', 'ejs');

Expand Down
20 changes: 18 additions & 2 deletions CommonServer/package-lock.json

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

2 changes: 1 addition & 1 deletion CommonServer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"express": "^4.17.3",
"gridfs-stream": "^1.1.1",
"handlebars": "^4.7.7",
"ioredis": "^5.3.2",
"json2csv": "^5.0.7",
"jsonwebtoken": "^9.0.0",
"markdown-it": "^13.0.1",
Expand All @@ -39,7 +40,6 @@
"nodemailer": "^6.7.3",
"nodemailer-express-handlebars": "^5.0.0",
"pg": "^8.7.3",
"redis": "^4.2.0",
"socket.io": "^4.4.1",
"stripe": "^10.17.0",
"twilio": "^4.13.0",
Expand Down
5 changes: 4 additions & 1 deletion config.example.env
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ DATABASE_SSL_CERT=

REDIS_HOST=redis
REDIS_PORT=6379

REDIS_DB=0
REDIS_USERNAME=default
REDIS_TLS_CA=
REDIS_TLS_SENTINEL_MODE=false

DISABLE_SIGNUP=false

Expand Down
4 changes: 4 additions & 0 deletions docker-compose.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ x-common-server-variables: &common-server-variables
DATABASE_SSL_CERT: ${DATABASE_SSL_CERT}
DATABASE_SSL_REJECT_UNAUTHORIZED: ${DATABASE_SSL_REJECT_UNAUTHORIZED}

REDIS_USERNAME: ${REDIS_USERNAME}
REDIS_PASSWORD: ${REDIS_PASSWORD}
REDIS_HOST: ${REDIS_HOST}
REDIS_PORT: ${REDIS_PORT}
REDIS_DB: ${REDIS_DB}
REDIS_TLS_CA: ${REDIS_TLS_CA}
REDIS_TLS_SENTINEL_MODE: ${REDIS_TLS_SENTINEL_MODE}

ENCRYPTION_SECRET: ${ENCRYPTION_SECRET}
DISABLE_SIGNUP: ${DISABLE_SIGNUP}
Expand Down