Skip to content

Commit

Permalink
Regression: Improve apps bridges for HA setup (#15080)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-gubert committed Aug 2, 2019
1 parent 8dbdb9d commit 96d6ff9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
6 changes: 3 additions & 3 deletions app/apps/client/admin/appManage.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ Template.appManage.events({
event.preventDefault();
event.stopPropagation();

const { id, state } = instance;
const { appId, state } = instance;

if (state.get('isSaving')) {
return;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/apps/server/communication/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 19 additions & 6 deletions app/apps/server/communication/websockets.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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));
Expand All @@ -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 });
}
Expand All @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions app/apps/server/orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,33 @@ 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()
.then((affs) => console.log(`Loaded the Apps Framework and loaded a total of ${ affs.length } Apps!`))
.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()
.then(() => console.log('Unloaded the Apps Framework.'))
.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)
Expand Down
11 changes: 3 additions & 8 deletions app/apps/server/storage/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 96d6ff9

Please sign in to comment.