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

Allow more REDIS settings #1765

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions Client/src/Features/Settings/settingsSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export const updateAppSettings = createAsyncThunk(
dbConnectionString: settings.dbConnectionString,
redisHost: settings.redisHost,
redisPort: settings.redisPort,
redisUser: settings.redisUser,
redisPassword: settings.redisPassword,
redisDb: settings.redisDb,
redisPrefix: settings.redisPrefix,
Comment on lines +45 to +48
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ensure proper type conversion for Redis database number.

The redisDb value should be converted to a number before sending to the server.

 redisUser: settings.redisUser,
 redisPassword: settings.redisPassword,
-redisDb: settings.redisDb,
+redisDb: Number(settings.redisDb) || 0,
 redisPrefix: settings.redisPrefix,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
redisUser: settings.redisUser,
redisPassword: settings.redisPassword,
redisDb: settings.redisDb,
redisPrefix: settings.redisPrefix,
redisUser: settings.redisUser,
redisPassword: settings.redisPassword,
redisDb: Number(settings.redisDb) || 0,
redisPrefix: settings.redisPrefix,

jwtTTL: settings.jwtTTL,
pagespeedApiKey: settings.pagespeedApiKey,
systemEmailHost: settings.systemEmailHost,
Expand Down
14 changes: 14 additions & 0 deletions Server/db/models/AppSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ const AppSettingsSchema = mongoose.Schema(
type: Number,
default: "6379",
},
redisUser: {
type: String,
},
redisPassword: {
type: String,
},
redisDb: {
type: Number,
default: 0
},
redisPrefix: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this prefix for?

type: String,
default: "",
},
jwtTTL: {
type: String,
required: true,
Expand Down
13 changes: 12 additions & 1 deletion Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,21 @@ const shutdown = async () => {
const settings =
ServiceRegistry.get(SettingsService.SERVICE_NAME).getSettings() || {};

const { redisHost = "127.0.0.1", redisPort = 6379 } = settings;
const {
redisHost = "127.0.0.1",
redisPort = 6379,
redisUser = null,
redisPassword = null,
redisDb = 0,
redisPrefix = ""
} = settings;
const redis = new IORedis({
host: redisHost,
port: redisPort,
username: redisUser,
password: redisPassword,
db: redisDb,
keyPrefix: redisPrefix,
});
Comment on lines 119 to 126
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for Redis connection.

The Redis connection should handle potential connection errors and authentication failures.

 const redis = new IORedis({
   host: redisHost,
   port: redisPort,
   username: redisUser,
   password: redisPassword,
   db: redisDb,
   keyPrefix: redisPrefix,
+  retryStrategy(times) {
+    const delay = Math.min(times * 50, 2000);
+    return delay;
+  }
 });
+
+redis.on('error', (err) => {
+  logger.error({
+    message: 'Redis connection error',
+    error: err.message,
+    service: SERVICE_NAME,
+    method: 'shutdown'
+  });
+});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const redis = new IORedis({
host: redisHost,
port: redisPort,
username: redisUser,
password: redisPassword,
db: redisDb,
keyPrefix: redisPrefix,
});
const redis = new IORedis({
host: redisHost,
port: redisPort,
username: redisUser,
password: redisPassword,
db: redisDb,
keyPrefix: redisPrefix,
+ retryStrategy(times) {
+ const delay = Math.min(times * 50, 2000);
+ return delay;
+ }
});
+ redis.on('error', (err) => {
+ logger.error({
+ message: 'Redis connection error',
+ error: err.message,
+ service: SERVICE_NAME,
+ method: 'shutdown'
+ });
+ });

logger.info({ message: "Flushing Redis" });
await redis.flushall();
Expand Down
13 changes: 12 additions & 1 deletion Server/service/jobQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ class NewJobQueue {
Worker
) {
const settings = settingsService.getSettings() || {};
const { redisHost = "127.0.0.1", redisPort = 6379 } = settings;
const {
redisHost = "127.0.0.1",
redisPort = 6379,
redisUser = null,
redisPassword = null,
redisDb = 0,
redisPrefix = ""
} = settings;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will all potentially compatible redis-like databases work with these null and empty values?

If not or you don't know, then why not start with just Host and Port, and conditionally add other properties if they have been specified.

const connection = {
host: redisHost,
port: redisPort,
username: redisUser,
password: redisPassword,
db: redisDb,
keyPrefix: redisPrefix,
};

this.queues = {};
Expand Down
4 changes: 4 additions & 0 deletions Server/service/settingsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const envConfig = {
dbConnectionString: process.env.DB_CONNECTION_STRING,
redisHost: process.env.REDIS_HOST,
redisPort: process.env.REDIS_PORT,
redisUser: process.env.REDIS_USER,
redisPassword: process.env.REDIS_PASSWORD,
redisDb: process.env.REDIS_DB,
redisPrefix: process.env.REDIS_PREFIX,
jwtTTL: process.env.TOKEN_TTL,
refreshTokenTTL: process.env.REFRESH_TOKEN_TTL,
pagespeedApiKey: process.env.PAGESPEED_API_KEY,
Expand Down
4 changes: 4 additions & 0 deletions Server/validation/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ const updateAppSettingsBodyValidation = joi.object({
dbConnectionString: joi.string().allow(""),
redisHost: joi.string().allow(""),
redisPort: joi.number().allow(null, ""),
redisUser: joi.string().allow(null, ""),
redisPassword: joi.string().allow(null, ""),
redisDb: joi.number().allow(null, 0),
redisPrefix: joi.string().allow(""),
jwtTTL: joi.string().allow(""),
pagespeedApiKey: joi.string().allow(""),
systemEmailHost: joi.string().allow(""),
Expand Down