Skip to content

Commit

Permalink
Merge pull request #182 from matrix-org/hs/safe-shutdown
Browse files Browse the repository at this point in the history
Ensure bridge quits on SIGTERM
  • Loading branch information
Half-Shot authored Oct 22, 2020
2 parents 83a0c6b + 544c78a commit 012b53a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/182.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The bridge will now close the AS and XMPP connections on a SIGTERM signal
2 changes: 1 addition & 1 deletion src/MatrixEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ return `- ${account.protocol.name} (${username}) [Enabled=${account.isEnabled}]
}
const requiredPl = this.config.provisioning.requiredUserPL;
const intent = this.bridge.getIntent();
const powerLevels = await intent.getClient().getStateEvent(event.room_id, "m.room.power_levels");
const powerLevels = await intent.getStateEvent(event.room_id, "m.room.power_levels");
const userPl = powerLevels.users[event.sender] === undefined ? powerLevels.users_default :
powerLevels.users[event.sender];
if (userPl < requiredPl) {
Expand Down
9 changes: 9 additions & 0 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Program {
});
this.cfg = new Config();
this.deduplicator = new Deduplicator();
process.on("SIGTERM", () =>
this.killBridge()
)
}

public get config(): Config {
Expand Down Expand Up @@ -131,6 +134,12 @@ class Program {
}
}

private async killBridge() {
log.info("SIGTERM recieved, killing bridge");
await this.bridge.close();
await this.purple.close();
}

private async runBridge(port: number, config: any) {
const checkOnly = process.env.BIFROST_CHECK_ONLY === "true";
this.cfg.ApplyConfig(config);
Expand Down
1 change: 1 addition & 0 deletions src/bifrost/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface IBifrostInstance extends EventEmitter {
createBifrostAccount(username, protocol: BifrostProtocol): IBifrostAccount;
getBuddyFromChat(conv: any, buddy: string): any;
start(): Promise<void>;
close(): Promise<void>;
getAccount(username: string, protocolId: string, mxid?: string): IBifrostAccount|null;
getProtocol(id: string): BifrostProtocol|undefined;
getProtocols(): BifrostProtocol[];
Expand Down
2 changes: 2 additions & 0 deletions src/purple/PurpleInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export class PurpleInstance extends EventEmitter implements IBifrostInstance {
return true;
}

public async close() {/* nothing to do */}

public getUsernameFromMxid(
mxid: string,
prefix: string = ""): {username: string, protocol: BifrostProtocol} {
Expand Down
4 changes: 4 additions & 0 deletions src/xmppjs/XJSInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export class XmppJsInstance extends EventEmitter implements IBifrostInstance {
return undefined;
}

public async close() {
await this.xmpp?.stop();
}

public async start(): Promise<void> {
const config = this.config.purple;
const opts = config.backendOpts as IXJSBackendOpts;
Expand Down

0 comments on commit 012b53a

Please sign in to comment.