From 05d9c5c70a9d562a6da49c9478c6446fce7043cb Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 18 May 2022 19:28:37 -0300 Subject: [PATCH] [FIX] Dynamic load matrix is enabled and handle failure (#25495) Co-authored-by: Aaron Ogle --- .../meteor/app/federation-v2/server/bridge.ts | 99 ++++++++++++++----- .../server/matrix-client/user.ts | 2 +- 2 files changed, 77 insertions(+), 24 deletions(-) diff --git a/apps/meteor/app/federation-v2/server/bridge.ts b/apps/meteor/app/federation-v2/server/bridge.ts index d02c2b674adc..289f7e9033d4 100644 --- a/apps/meteor/app/federation-v2/server/bridge.ts +++ b/apps/meteor/app/federation-v2/server/bridge.ts @@ -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 => { - // Get the event - const event = request.getData() as unknown as IMatrixEvent; - - addToQueue(event); - }, - onLog: async (line, isError): Promise => { - console.log(line, isError); - }, - }, -}); +class Bridge { + private bridgeInstance: MatrixBridge; + + private isRunning = false; + + public async start(): Promise { + 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 { + 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 { + 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 => { + // Get the event + const event = request.getData() as unknown as IMatrixEvent; + + addToQueue(event); + }, + onLog: async (line, isError): Promise => { + console.log(line, isError); + }, + }, + }); + } + + private getBridgePort(): number { + const [, , port] = settings.get('Federation_Matrix_bridge_url').split(':'); + + return parseInt(port); + } +} + +export const matrixBridge = new Bridge(); diff --git a/apps/meteor/app/federation-v2/server/matrix-client/user.ts b/apps/meteor/app/federation-v2/server/matrix-client/user.ts index f17c200426d1..2674bd95f96c 100644 --- a/apps/meteor/app/federation-v2/server/matrix-client/user.ts +++ b/apps/meteor/app/federation-v2/server/matrix-client/user.ts @@ -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';