Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Dynamic load matrix is enabled and handle failure #25495

Merged
merged 3 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions apps/meteor/app/federation-v2/server/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Bridge as MatrixBridge, 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';

class Bridge {
private bridgeInstance: MatrixBridge;
Expand All @@ -14,12 +16,17 @@ class Bridge {
public async start(): Promise<void> {
try {
await this.stop();
} finally {
this.createInstance();
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);
}
}

Expand All @@ -28,15 +35,20 @@ class Bridge {
return;
}
// the http server can take some minutes to shutdown and this promise to be resolved
await this.bridgeInstance.close();
await this.bridgeInstance?.close();
this.isRunning = false;
}

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

private createInstance(): void {
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'),
Expand Down
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