Skip to content

Commit

Permalink
fix: update cached setting immediately at the time of updating the db (
Browse files Browse the repository at this point in the history
…#32541)

* update cache when inserting setting into model, don't wait for
changestream

* add changeset

---------

Co-authored-by: Gabriel Casals <[email protected]>
  • Loading branch information
debdutdeb and casalsgh authored Jul 3, 2024
1 parent eb5f93c commit 06707d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-ravens-swim.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 14 additions & 8 deletions apps/meteor/app/settings/server/SettingsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -166,6 +171,9 @@ export class SettingsRegistry {
})();

await this.saveUpdatedSetting(_id, updatedProps, removedKeys);

this.store.set(updatedSettingAfterApplyingOverwrite);

return;
}

Expand All @@ -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;
}
Expand All @@ -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);
}

/*
Expand Down

0 comments on commit 06707d8

Please sign in to comment.