summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-12-16 09:22:49 +0100
committerRory& <root@rory.gay>2025-12-16 09:22:49 +0100
commit9f9cdadcb15b5690703b4c884b125fd9425cf151 (patch)
tree901a1543a6a7478d354c33573d31da516ad0e52c /src
parentDirty rabbitmq hack (diff)
downloadserver-ts-9f9cdadcb15b5690703b4c884b125fd9425cf151.tar.xz
Listen for connection errors?
Diffstat (limited to 'src')
-rw-r--r--src/util/util/RabbitMQ.ts40
1 files changed, 25 insertions, 15 deletions
diff --git a/src/util/util/RabbitMQ.ts b/src/util/util/RabbitMQ.ts

index 2bda3d31..66384d24 100644 --- a/src/util/util/RabbitMQ.ts +++ b/src/util/util/RabbitMQ.ts
@@ -19,15 +19,11 @@ import amqp, { Channel, ChannelModel } from "amqplib"; import { Config } from "./Config"; -export const RabbitMQ: { - connection: ChannelModel | null; - channel: Channel | null; - init: () => Promise<void>; - getSafeChannel: () => Promise<Channel>; -} = { - connection: null, - channel: null, - init: async function () { +export class RabbitMQ { + public connection: ChannelModel | null = null; + public channel: Channel | null = null; + + async init() { const host = Config.get().rabbitmq.host; if (!host) return; console.log(`[RabbitMQ] connect: ${host}`); @@ -47,11 +43,11 @@ export const RabbitMQ: { // will be a pain since we will have to reconstruct entire state }); - await this.getSafeChannel(); - }, - getSafeChannel: async function () { - if (!this.connection) return Promise.reject(); + await this.getSafeChannel(); // why is this here? + } + async getSafeChannel(): Promise<Channel> { + if (!this.connection) return Promise.reject(); if (this.channel) return this.channel; try { @@ -68,6 +64,20 @@ export const RabbitMQ: { this.channel = null; }); + this.connection.on("error", (err) => { + console.error("[RabbitMQ] connection error, setting channel to null and reconnecting:", err); + this.channel = null; + this.connection = null; + this.init(); + }); + + this.connection.on("close", () => { + console.log("[RabbitMQ] connection closed, setting channel to null and reconnecting"); + this.channel = null; + this.connection = null; + this.init(); + }); + return this.channel; } catch (e) { console.error("[RabbitMQ] Failed to create channel:", e); @@ -78,5 +88,5 @@ export const RabbitMQ: { return await this.getSafeChannel(); // return Promise.reject(e); } - }, -}; + } +}