Skip to content

Commit

Permalink
[FIX] Dynamic load matrix is enabled and handle failure (#25495)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Ogle <aaron@geekgonecrazy.com>
  • Loading branch information
2 people authored and d-gubert committed May 20, 2022
1 parent 0459a69 commit 05d9c5c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 24 deletions.
99 changes: 76 additions & 23 deletions apps/meteor/app/federation-v2/server/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,81 @@
import { Bridge, AppServiceRegistration } from '@rocket.chat/forked-matrix-appservice-bridge';
import type { Bridge as MatrixBridge } from '@rocket.chat/forked-matrix-appservice-bridge';

import { settings } from '../../settings/server';
import { IMatrixEvent } from './definitions/IMatrixEvent';
import { MatrixEventType } from './definitions/MatrixEventType';
import { Settings } from '../../models/server/raw';
import type { IMatrixEvent } from './definitions/IMatrixEvent';
import type { MatrixEventType } from './definitions/MatrixEventType';
import { addToQueue } from './queue';
import { getRegistrationInfo } from './config';
import { bridgeLogger } from './logger';

export const matrixBridge = new Bridge({
homeserverUrl: settings.get('Federation_Matrix_homeserver_url'),
domain: settings.get('Federation_Matrix_homeserver_domain'),
registration: AppServiceRegistration.fromObject(getRegistrationInfo()),
disableStores: true,
controller: {
onAliasQuery: (alias, matrixRoomId): void => {
console.log('onAliasQuery', alias, matrixRoomId);
},
onEvent: async (request /* , context*/): Promise<void> => {
// Get the event
const event = request.getData() as unknown as IMatrixEvent<MatrixEventType>;

addToQueue(event);
},
onLog: async (line, isError): Promise<void> => {
console.log(line, isError);
},
},
});
class Bridge {
private bridgeInstance: MatrixBridge;

private isRunning = false;

public async start(): Promise<void> {
try {
await this.stop();
await this.createInstance();

if (!this.isRunning) {
await this.bridgeInstance.run(this.getBridgePort());
this.isRunning = true;
}
} catch (e) {
bridgeLogger.error('Failed to initialize the matrix-appservice-bridge.', e);

bridgeLogger.error('Disabling Matrix Bridge. Please resolve error and try again');
Settings.updateValueById('Federation_Matrix_enabled', false);
}
}

public async stop(): Promise<void> {
if (!this.isRunning) {
return;
}
// the http server can take some minutes to shutdown and this promise to be resolved
await this.bridgeInstance?.close();
this.isRunning = false;
}

public getInstance(): MatrixBridge {
return this.bridgeInstance;
}

private async createInstance(): Promise<void> {
bridgeLogger.info('Performing Dynamic Import of matrix-appservice-bridge');

// Dynamic import to prevent Rocket.Chat from loading the module until needed and then handle if that fails
const { Bridge: MatrixBridge, AppServiceRegistration } = await import('@rocket.chat/forked-matrix-appservice-bridge');

this.bridgeInstance = new MatrixBridge({
homeserverUrl: settings.get('Federation_Matrix_homeserver_url'),
domain: settings.get('Federation_Matrix_homeserver_domain'),
registration: AppServiceRegistration.fromObject(getRegistrationInfo()),
disableStores: true,
controller: {
onAliasQuery: (alias, matrixRoomId): void => {
console.log('onAliasQuery', alias, matrixRoomId);
},
onEvent: async (request /* , context*/): Promise<void> => {
// Get the event
const event = request.getData() as unknown as IMatrixEvent<MatrixEventType>;

addToQueue(event);
},
onLog: async (line, isError): Promise<void> => {
console.log(line, isError);
},
},
});
}

private getBridgePort(): number {
const [, , port] = settings.get<string>('Federation_Matrix_bridge_url').split(':');

return parseInt(port);
}
}

export const matrixBridge = new Bridge();
2 changes: 1 addition & 1 deletion apps/meteor/app/federation-v2/server/matrix-client/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MatrixProfileInfo } from '@rocket.chat/forked-matrix-bot-sdk';
import type { MatrixProfileInfo } from '@rocket.chat/forked-matrix-bot-sdk';
import { IUser } from '@rocket.chat/core-typings';

import { matrixBridge } from '../bridge';
Expand Down

0 comments on commit 05d9c5c

Please sign in to comment.