-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Dynamic load matrix is enabled and handle failure (#25495)
Co-authored-by: Aaron Ogle <aaron@geekgonecrazy.com>
- Loading branch information
Showing
2 changed files
with
77 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters