From 06707d86e14b6ac3122782b3e6ba6ef1ff8cd449 Mon Sep 17 00:00:00 2001 From: Debdut Chakraborty Date: Wed, 3 Jul 2024 20:32:49 +0530 Subject: [PATCH] fix: update cached setting immediately at the time of updating the db (#32541) * update cache when inserting setting into model, don't wait for changestream * add changeset --------- Co-authored-by: Gabriel Casals <83978645+casalsgh@users.noreply.github.com> --- .changeset/cuddly-ravens-swim.md | 5 +++++ .../app/settings/server/SettingsRegistry.ts | 22 ++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 .changeset/cuddly-ravens-swim.md diff --git a/.changeset/cuddly-ravens-swim.md b/.changeset/cuddly-ravens-swim.md new file mode 100644 index 000000000000..5774ef48202d --- /dev/null +++ b/.changeset/cuddly-ravens-swim.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +Fixed an issue where adding `OVERWRITE_SETTING_` for any setting wasn't immediately taking effect sometimes, and needed a server restart to reflect. diff --git a/apps/meteor/app/settings/server/SettingsRegistry.ts b/apps/meteor/app/settings/server/SettingsRegistry.ts index 5783e2946dc1..443e38ce5d63 100644 --- a/apps/meteor/app/settings/server/SettingsRegistry.ts +++ b/apps/meteor/app/settings/server/SettingsRegistry.ts @@ -138,17 +138,22 @@ export class SettingsRegistry { const settingFromCodeOverwritten = overwriteSetting(settingFromCode); + const settingOverwrittenDefault = overrideSetting(settingFromCode); + const settingStored = this.store.getSetting(_id); + const settingStoredOverwritten = settingStored && overwriteSetting(settingStored); + const isOverwritten = settingFromCode !== settingFromCodeOverwritten || (settingStored && settingStored !== settingStoredOverwritten); + + const updatedSettingAfterApplyingOverwrite = isOverwritten ? settingFromCodeOverwritten : settingOverwrittenDefault; + try { validateSetting(settingFromCode._id, settingFromCode.type, settingFromCode.value); } catch (e) { IS_DEVELOPMENT && SystemLogger.error(`Invalid setting code ${_id}: ${(e as Error).message}`); } - const isOverwritten = settingFromCode !== settingFromCodeOverwritten || (settingStored && settingStored !== settingStoredOverwritten); - const { _id: _, ...settingProps } = settingFromCodeOverwritten; if (settingStored && !compareSettings(settingStored, settingFromCodeOverwritten)) { @@ -166,6 +171,9 @@ export class SettingsRegistry { })(); await this.saveUpdatedSetting(_id, updatedProps, removedKeys); + + this.store.set(updatedSettingAfterApplyingOverwrite); + return; } @@ -175,6 +183,8 @@ export class SettingsRegistry { const removedKeys = Object.keys(settingStored).filter((key) => !['_updatedAt'].includes(key) && !overwrittenKeys.includes(key)); await this.saveUpdatedSetting(_id, settingProps, removedKeys); + + this.store.set(updatedSettingAfterApplyingOverwrite); } return; } @@ -188,13 +198,9 @@ export class SettingsRegistry { return; } - const settingOverwrittenDefault = overrideSetting(settingFromCode); - - const setting = isOverwritten ? settingFromCodeOverwritten : settingOverwrittenDefault; - - await this.model.insertOne(setting); // no need to emit unless we remove the oplog + await this.model.insertOne(updatedSettingAfterApplyingOverwrite); // no need to emit unless we remove the oplog - this.store.set(setting); + this.store.set(updatedSettingAfterApplyingOverwrite); } /*