Skip to content

Commit

Permalink
fix: app status inconsistencies when running multiple instances in a …
Browse files Browse the repository at this point in the history
…cluster (#29180)
  • Loading branch information
d-gubert authored May 11, 2023
1 parent a0509a4 commit b9f23b0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
15 changes: 11 additions & 4 deletions apps/meteor/app/apps/server/bridges/activation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ export class AppActivationBridge extends ActivationBridge {
super();
}

protected async appAdded(app: ProxiedApp): Promise<void> {
await this.orch.getNotifier().appAdded(app.getID());
protected async appAdded(_app: ProxiedApp): Promise<void> {
// await this.orch.getNotifier().appAdded(app.getID());

// Calls made via AppActivationBridge should NOT go through
// View https://github.com/RocketChat/Rocket.Chat/pull/29180 for details
return undefined;
}

protected async appUpdated(app: ProxiedApp): Promise<void> {
await this.orch.getNotifier().appUpdated(app.getID());
protected async appUpdated(_app: ProxiedApp): Promise<void> {
// Calls made via AppActivationBridge should NOT go through
// View https://github.com/RocketChat/Rocket.Chat/pull/29180 for details
// await this.orch.getNotifier().appUpdated(app.getID());
return undefined;
}

protected async appRemoved(app: ProxiedApp): Promise<void> {
Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/ee/server/apps/communication/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ export class AppsRestApi {
info.status = success ? AppStatus.AUTO_ENABLED : info.status;
}

void orchestrator.getNotifier().appAdded(info.id);

return API.v1.success({
app: info,
implemented: aff.getImplementedInferfaces(),
Expand Down Expand Up @@ -769,6 +771,8 @@ export class AppsRestApi {

void notifyAppInstall(orchestrator.getMarketplaceUrl() as string, 'update', info);

void orchestrator.getNotifier().appUpdated(info.id);

return API.v1.success({
app: info,
implemented: aff.getImplementedInferfaces(),
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/ee/server/apps/communication/websockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class AppServerListener {
}

async onAppAdded(appId: string): Promise<void> {
await (this.orch.getManager()! as any).loadOne(appId); // TO-DO: fix type
await (this.orch.getManager()! as any).addLocal(appId); // TO-DO: fix type
this.clientStreamer.emitWithoutBroadcast(AppEvents.APP_ADDED, appId);
}

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
"@rocket.chat/account-utils": "workspace:^",
"@rocket.chat/agenda": "workspace:^",
"@rocket.chat/api-client": "workspace:^",
"@rocket.chat/apps-engine": "1.39.0-alpha.212",
"@rocket.chat/apps-engine": "1.39.0-alpha.219",
"@rocket.chat/base64": "workspace:^",
"@rocket.chat/cas-validate": "workspace:^",
"@rocket.chat/core-services": "workspace:^",
Expand Down
30 changes: 22 additions & 8 deletions apps/meteor/server/services/apps-engine/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,34 @@ export class AppsEngineService extends ServiceClassInternal implements IAppsEngi
});

this.onEvent('apps.added', async (appId: string): Promise<void> => {
Apps.getRocketChatLogger().debug(`"apps.added" event received for app "${appId}"`);
// if the app already exists in this instance, don't load it again
const app = Apps.getManager()?.getOneById(appId);

if (app) {
Apps.getRocketChatLogger().info(`"apps.added" event received for app "${appId}", but it already exists in this instance`);
return;
}

await (Apps.getManager() as any)?.loadOne(appId);
await Apps.getManager()?.addLocal(appId);
});

this.onEvent('apps.removed', async (appId: string): Promise<void> => {
Apps.getRocketChatLogger().debug(`"apps.removed" event received for app "${appId}"`);
const app = Apps.getManager()?.getOneById(appId);
if (!app) {
Apps.getRocketChatLogger().info(`"apps.removed" event received for app "${appId}", but it couldn't be found in this instance`);
return;
}

await Apps.getManager()?.removeLocal(appId);
});

this.onEvent('apps.updated', async (appId: string): Promise<void> => {
Apps.getRocketChatLogger().debug(`"apps.updated" event received for app "${appId}"`);
const storageItem = await Apps.getStorage()?.retrieveOne(appId);
if (!storageItem) {
Apps.getRocketChatLogger().info(`"apps.updated" event received for app "${appId}", but it couldn't be found in the storage`);
return;
}

Expand All @@ -59,8 +65,15 @@ export class AppsEngineService extends ServiceClassInternal implements IAppsEngi
});

this.onEvent('apps.statusUpdate', async (appId: string, status: AppStatus): Promise<void> => {
Apps.getRocketChatLogger().debug(`"apps.statusUpdate" event received for app "${appId}" with status "${status}"`);
const app = Apps.getManager()?.getOneById(appId);
if (!app || app.getStatus() === status) {
if (!app) {
Apps.getRocketChatLogger().info(`"apps.statusUpdate" event received for app "${appId}", but it couldn't be found in this instance`);
return;
}

if (app.getStatus() === status) {
Apps.getRocketChatLogger().info(`"apps.statusUpdate" event received for app "${appId}", but the status is the same`);
return;
}

Expand All @@ -72,21 +85,22 @@ export class AppsEngineService extends ServiceClassInternal implements IAppsEngi
});

this.onEvent('apps.settingUpdated', async (appId: string, setting: ISetting & { id: string }): Promise<void> => {
Apps.getRocketChatLogger().debug(`"apps.settingUpdated" event received for app "${appId}"`, { setting });
const app = Apps.getManager()?.getOneById(appId);
const oldSetting = app?.getStorageItem().settings[setting.id].value;

// avoid updating the setting if the value is the same,
// which caused an infinite loop
if (oldSetting === setting.value) {
Apps.getRocketChatLogger().info(
`"apps.settingUpdated" event received for setting ${setting.id} of app "${appId}", but the setting value is the same`,
);
return;
}

const appManager = Apps.getManager();
if (!appManager) {
return;
}

await appManager.getSettingsManager().updateAppSetting(appId, setting as any);
await Apps.getManager()
?.getSettingsManager()
.updateAppSetting(appId, setting as any);
});
}

Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6226,9 +6226,9 @@ __metadata:
languageName: node
linkType: hard

"@rocket.chat/apps-engine@npm:1.39.0-alpha.212":
version: 1.39.0-alpha.212
resolution: "@rocket.chat/apps-engine@npm:1.39.0-alpha.212"
"@rocket.chat/apps-engine@npm:1.39.0-alpha.219":
version: 1.39.0-alpha.219
resolution: "@rocket.chat/apps-engine@npm:1.39.0-alpha.219"
dependencies:
adm-zip: ^0.5.9
cryptiles: ^4.1.3
Expand All @@ -6240,7 +6240,7 @@ __metadata:
vm2: ^3.9.17
peerDependencies:
"@rocket.chat/ui-kit": "*"
checksum: 2f43ece57b8f31c3a395b42f3b764de557ccd1058a77c6f8bfff4b9f4fd280f1a00bb5efb78e04507a6bc9dbdd76c103e1e51253422a4f082904bcc9a8a2483a
checksum: db924a4b8d8efb592e5c09f1e938ab330bb10fdfaf4c15a153e9ea749a28371e85bee55686a4f82d61355ec71bd23e43bb9b9dc061f1384330ea27c1688c9cb1
languageName: node
linkType: hard

Expand Down Expand Up @@ -6875,7 +6875,7 @@ __metadata:
"@rocket.chat/account-utils": "workspace:^"
"@rocket.chat/agenda": "workspace:^"
"@rocket.chat/api-client": "workspace:^"
"@rocket.chat/apps-engine": 1.39.0-alpha.212
"@rocket.chat/apps-engine": 1.39.0-alpha.219
"@rocket.chat/base64": "workspace:^"
"@rocket.chat/cas-validate": "workspace:^"
"@rocket.chat/core-services": "workspace:^"
Expand Down Expand Up @@ -24481,7 +24481,7 @@ __metadata:
resolution: "lamejs@https://github.com/zhuker/lamejs.git#commit=582bbba6a12f981b984d8fb9e1874499fed85675"
dependencies:
use-strict: 1.0.1
checksum: fa829e0c170a65573e653b4d908a44aaf06a50e1bbade3b1217a300a03ccd59a537e294e2d924a584f9d70c7726a12d4c3af9c675436d48d08be5fb94b5eb400
checksum: ed7f6f1c9629b53c17023eb04b4fc5a222e9c34fcb4a2f61214488fc64e5cfea825e4588d959c5fb20f3a91f0120103fa60307dd43df995d498ff5ddb6200cd9
languageName: node
linkType: hard

Expand Down

0 comments on commit b9f23b0

Please sign in to comment.