Skip to content

Commit

Permalink
chore: improve license sync (#31011)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Nov 17, 2023
1 parent bc991fa commit 935d650
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
37 changes: 31 additions & 6 deletions apps/meteor/ee/app/license/server/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,29 @@ const applyLicense = async (license: string, isNewLicense: boolean): Promise<boo
}
};

const syncByTrigger = async (context: string) => {
/**
* This is a debounced function that will sync the workspace data to the cloud.
* it caches the context, waits for a second and then syncs the data.
*/

const syncByTriggerDebounced = (() => {
let timeout: NodeJS.Timeout | undefined;
const contexts: Set<string> = new Set();
return async (context: string) => {
contexts.add(context);
if (timeout) {
clearTimeout(timeout);
}

timeout = setTimeout(() => {
timeout = undefined;
void syncByTrigger([...contexts]);
contexts.clear();
}, 1000);
};
})();

const syncByTrigger = async (contexts: string[]) => {
if (!License.encryptedLicense) {
return;
}
Expand All @@ -60,16 +82,19 @@ const syncByTrigger = async (context: string) => {
const [, , signed] = License.encryptedLicense.split('.');

// Check if this sync has already been done. Based on License, behavior.
if (existingData.signed === signed && existingData[context] === period) {

if ([...contexts.values()].every((context) => existingData.signed === signed && existingData[context] === period)) {
return;
}

const obj = Object.fromEntries(contexts.map((context) => [context, period]));

await Settings.updateValueById(
'Enterprise_License_Data',
JSON.stringify({
...(existingData.signed === signed && existingData),
...existingData,
[context]: period,
...obj,
signed,
}),
);
Expand All @@ -91,11 +116,11 @@ settings.onReady(async () => {

callbacks.add('workspaceLicenseChanged', async (updatedLicense) => applyLicense(updatedLicense, true));

License.onBehaviorTriggered('prevent_action', (context) => syncByTrigger(`prevent_action_${context.limit}`));
License.onBehaviorTriggered('prevent_action', (context) => syncByTriggerDebounced(`prevent_action_${context.limit}`));

License.onBehaviorTriggered('start_fair_policy', async (context) => syncByTrigger(`start_fair_policy_${context.limit}`));
License.onBehaviorTriggered('start_fair_policy', async (context) => syncByTriggerDebounced(`start_fair_policy_${context.limit}`));

License.onBehaviorTriggered('disable_modules', async (context) => syncByTrigger(`disable_modules_${context.limit}`));
License.onBehaviorTriggered('disable_modules', async (context) => syncByTriggerDebounced(`disable_modules_${context.limit}`));

License.onChange(() => api.broadcast('license.sync'));

Expand Down
1 change: 1 addition & 0 deletions ee/packages/license/src/definition/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type LicenseEvents = ModuleValidation &
BehaviorTriggeredToggled &
BehaviorTriggered &
LimitReached & {
installed: undefined;
validate: undefined;
invalidate: undefined;
module: { module: LicenseModule; valid: boolean };
Expand Down
5 changes: 3 additions & 2 deletions ee/packages/license/src/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export class LicenseManager extends Emitter<LicenseEvents> {

constructor() {
super();

this.on('validate', () => showLicense.call(this, this._license, this._valid));
this.on('installed', () => showLicense.call(this, this._license, this._valid));
}

public get license(): ILicenseV3 | undefined {
Expand Down Expand Up @@ -268,6 +267,8 @@ export class LicenseManager extends Emitter<LicenseEvents> {
}
await this.setLicenseV3(decrypted, encryptedLicense, decrypted, isNewLicense);

this.emit('installed');

return true;
} catch (e) {
logger.error('Invalid license');
Expand Down

0 comments on commit 935d650

Please sign in to comment.