From 96d6ff96c9d8a68fc4fb35effcd97a31575140db Mon Sep 17 00:00:00 2001 From: Douglas Gubert Date: Fri, 2 Aug 2019 01:49:09 -0300 Subject: [PATCH] Regression: Improve apps bridges for HA setup (#15080) --- app/apps/client/admin/appManage.js | 6 ++--- app/apps/server/communication/rest.js | 2 +- app/apps/server/communication/websockets.js | 25 ++++++++++++++++----- app/apps/server/orchestrator.js | 12 +++++----- app/apps/server/storage/storage.js | 11 +++------ package.json | 2 +- 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/app/apps/client/admin/appManage.js b/app/apps/client/admin/appManage.js index 460bea32932e..e94565cca3cb 100644 --- a/app/apps/client/admin/appManage.js +++ b/app/apps/client/admin/appManage.js @@ -334,7 +334,7 @@ Template.appManage.events({ event.preventDefault(); event.stopPropagation(); - const { id, state } = instance; + const { appId, state } = instance; if (state.get('isSaving')) { return; @@ -345,14 +345,14 @@ Template.appManage.events({ const settings = state.get('settings'); try { - const toSave = Object.entries(settings) + const toSave = Object.values(settings) .filter(({ hasChanged }) => hasChanged); if (!toSave.length) { return; } - const updated = await Apps.setAppSettings(id, toSave); + const updated = await Apps.setAppSettings(appId, toSave); updated.forEach(({ id, value }) => { settings[id].value = value; settings[id].oldValue = value; diff --git a/app/apps/server/communication/rest.js b/app/apps/server/communication/rest.js index 63e8502f972e..5b0a4f19d40b 100644 --- a/app/apps/server/communication/rest.js +++ b/app/apps/server/communication/rest.js @@ -73,7 +73,7 @@ export class AppsRestApi { let result; try { - result = HTTP.get(`${ baseUrl }/v1/apps?version=${ Info.marketplaceApiVersion }`, { + result = HTTP.get(`${ baseUrl }/v1/apps`, { headers, }); } catch (e) { diff --git a/app/apps/server/communication/websockets.js b/app/apps/server/communication/websockets.js index 94b9c22765a7..857158ddd3bb 100644 --- a/app/apps/server/communication/websockets.js +++ b/app/apps/server/communication/websockets.js @@ -1,5 +1,5 @@ import { Meteor } from 'meteor/meteor'; -import { AppStatus, AppStatusUtils } from '@rocket.chat/apps-engine/definition/AppStatus'; +import { AppStatusUtils } from '@rocket.chat/apps-engine/definition/AppStatus'; export const AppEvents = Object.freeze({ APP_ADDED: 'app/added', @@ -20,11 +20,12 @@ export class AppServerListener { this.clientStreamer = clientStreamer; this.received = received; - this.engineStreamer.on(AppEvents.APP_ADDED, this.onAppAdded.bind(this)); this.engineStreamer.on(AppEvents.APP_STATUS_CHANGE, this.onAppStatusUpdated.bind(this)); - this.engineStreamer.on(AppEvents.APP_SETTING_UPDATED, this.onAppSettingUpdated.bind(this)); this.engineStreamer.on(AppEvents.APP_REMOVED, this.onAppRemoved.bind(this)); this.engineStreamer.on(AppEvents.APP_UPDATED, this.onAppUpdated.bind(this)); + this.engineStreamer.on(AppEvents.APP_ADDED, this.onAppAdded.bind(this)); + + this.engineStreamer.on(AppEvents.APP_SETTING_UPDATED, this.onAppSettingUpdated.bind(this)); this.engineStreamer.on(AppEvents.COMMAND_ADDED, this.onCommandAdded.bind(this)); this.engineStreamer.on(AppEvents.COMMAND_DISABLED, this.onCommandDisabled.bind(this)); this.engineStreamer.on(AppEvents.COMMAND_UPDATED, this.onCommandUpdated.bind(this)); @@ -36,21 +37,27 @@ export class AppServerListener { this.clientStreamer.emit(AppEvents.APP_ADDED, appId); } + async onAppStatusUpdated({ appId, status }) { + const app = this.orch.getManager().getOneById(appId); + + if (app.getStatus() === status) { + return; + } + this.received.set(`${ AppEvents.APP_STATUS_CHANGE }_${ appId }`, { appId, status, when: new Date() }); if (AppStatusUtils.isEnabled(status)) { - await this.orch.getManager().enable(appId); + await this.orch.getManager().enable(appId).catch(console.error); this.clientStreamer.emit(AppEvents.APP_STATUS_CHANGE, { appId, status }); } else if (AppStatusUtils.isDisabled(status)) { - await this.orch.getManager().disable(appId, AppStatus.MANUALLY_DISABLED === status); + await this.orch.getManager().disable(appId, status, true).catch(console.error); this.clientStreamer.emit(AppEvents.APP_STATUS_CHANGE, { appId, status }); } } async onAppSettingUpdated({ appId, setting }) { this.received.set(`${ AppEvents.APP_SETTING_UPDATED }_${ appId }_${ setting.id }`, { appId, setting, when: new Date() }); - await this.orch.getManager().getSettingsManager().updateAppSetting(appId, setting); this.clientStreamer.emit(AppEvents.APP_SETTING_UPDATED, { appId }); } @@ -65,6 +72,12 @@ export class AppServerListener { } async onAppRemoved(appId) { + const app = this.orch.getManager().getOneById(appId); + + if (!app) { + return; + } + await this.orch.getManager().remove(appId); this.clientStreamer.emit(AppEvents.APP_REMOVED, appId); } diff --git a/app/apps/server/orchestrator.js b/app/apps/server/orchestrator.js index a470f2d2477c..427fae9f5006 100644 --- a/app/apps/server/orchestrator.js +++ b/app/apps/server/orchestrator.js @@ -99,11 +99,11 @@ class AppServerOrchestrator { return this._marketplaceUrl; } - load() { + async load() { // Don't try to load it again if it has // already been loaded if (this.isLoaded()) { - return Promise.resolve(); + return; } return this._manager.load() @@ -111,11 +111,11 @@ class AppServerOrchestrator { .catch((err) => console.warn('Failed to load the Apps Framework and Apps!', err)); } - unload() { + async unload() { // Don't try to unload it if it's already been // unlaoded or wasn't unloaded to start with if (!this.isLoaded()) { - return Promise.resolve(); + return; } return this._manager.unload() @@ -123,9 +123,9 @@ class AppServerOrchestrator { .catch((err) => console.warn('Failed to unload the Apps Framework!', err)); } - updateAppsMarketplaceInfo(apps = []) { + async updateAppsMarketplaceInfo(apps = []) { if (!this.isLoaded()) { - return Promise.resolve(); + return; } return this._manager.updateAppsMarketplaceInfo(apps) diff --git a/app/apps/server/storage/storage.js b/app/apps/server/storage/storage.js index 4b8c08eb572d..8fc9c79b705f 100644 --- a/app/apps/server/storage/storage.js +++ b/app/apps/server/storage/storage.js @@ -44,11 +44,7 @@ export class AppRealStorage extends AppStorage { return reject(e); } - if (doc) { - resolve(doc); - } else { - reject(new Error(`No App found by the id: ${ id }`)); - } + resolve(doc); }); } @@ -74,12 +70,11 @@ export class AppRealStorage extends AppStorage { return new Promise((resolve, reject) => { try { this.db.update({ id: item.id }, item); + resolve(item.id); } catch (e) { return reject(e); } - - this.retrieveOne(item.id).then((updated) => resolve(updated)).catch((err) => reject(err)); - }); + }).then(this.retrieveOne.bind(this)); } remove(id) { diff --git a/package.json b/package.json index b83f37158066..ea7e820a596d 100644 --- a/package.json +++ b/package.json @@ -133,7 +133,7 @@ "@google-cloud/language": "^2.0.0", "@google-cloud/storage": "^2.3.1", "@google-cloud/vision": "^0.23.0", - "@rocket.chat/apps-engine": "^1.5.1", + "@rocket.chat/apps-engine": "^1.5.2", "@slack/client": "^4.8.0", "adm-zip": "^0.4.13", "apollo-server-express": "^1.3.6",