summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-05-16 20:52:24 +0200
committerRory& <root@rory.gay>2026-05-24 06:14:39 +0200
commitb2236313c53171ea22fb0b4f37dfe56ba7885405 (patch)
tree3c6c5300eb95257cbdd6b9b08f9666d6d8dce5b7
parentMove rabbitListen to RabbitMQ.ts (diff)
downloadserver-ts-b2236313c53171ea22fb0b4f37dfe56ba7885405.tar.xz
Move publishEvent to RabbitMQ.ts
-rw-r--r--src/util/util/ipc/Event.ts29
-rw-r--r--src/util/util/ipc/RabbitMQ.ts28
2 files changed, 28 insertions, 29 deletions
diff --git a/src/util/util/ipc/Event.ts b/src/util/util/ipc/Event.ts

index 3eb052b9..8a2c55d8 100644 --- a/src/util/util/ipc/Event.ts +++ b/src/util/util/ipc/Event.ts
@@ -40,34 +40,7 @@ export async function emitEvent(payload: Omit<Event, "created_at">) { if (!id) return console.error("event doesn't contain any id", payload); if (RabbitMQ.connection) { - const data = typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.data; // use rabbitmq for event transmission - - const publishEvent = async (retryCount = 0): Promise<void> => { - const channel = await RabbitMQ.getSafeChannel(); - try { - await channel.assertExchange(id, "fanout", { - durable: false, - }); - - // assertQueue isn't needed, because a queue will automatically created if it doesn't exist - const successful = channel.publish(id, "", Buffer.from(`${data}`), { type: payload.event }); - if (!successful) throw new Error("failed to send event"); - } catch (e) { - // Check if this is a channel closed error and if we should retry - const errorMessage = e instanceof Error ? e.message : String(e); - const isChannelError = errorMessage.includes("Channel closed") || errorMessage.includes("IllegalOperationError") || errorMessage.includes("RESOURCE_ERROR"); - - if (isChannelError && retryCount < 1) { - console.log("[RabbitMQ] Channel error detected, retrying with new channel..."); - // Force the cached channel to be discarded by calling getSafeChannel which will create a new one - return publishEvent(retryCount + 1); - } - - console.log("[RabbitMQ] ", e); - } - }; - - await publishEvent(); + await RabbitMQ.publishEvent(id, payload); } else if (process.env.EVENT_TRANSMISSION === "unix" && process.env.EVENT_SOCKET_PATH) { if (!unixSocketWriter) { console.error("[Event] Unix socket writer not initialized, cannot emit event!"); diff --git a/src/util/util/ipc/RabbitMQ.ts b/src/util/util/ipc/RabbitMQ.ts
index 47d1660d..e3a1ca4f 100644 --- a/src/util/util/ipc/RabbitMQ.ts +++ b/src/util/util/ipc/RabbitMQ.ts
@@ -19,7 +19,7 @@ import { randomUUID } from "node:crypto"; import EventEmitter from "node:events"; import amqp, { Channel, ChannelModel } from "amqplib"; -import { EVENT } from "../../interfaces"; +import { Event, EVENT } from "../../interfaces"; import { Config } from "../Config"; import type { EventOpts } from "./Event"; @@ -173,6 +173,32 @@ export class RabbitMQ { static isConnected(): boolean { return this.connection !== null && !this.isReconnecting; } + + static async publishEvent(id: string, payload: Omit<Event, "created_at">, retryCount = 0): Promise<void> { + const data = typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.data; // use rabbitmq for event transmission + const channel = await RabbitMQ.getSafeChannel(); + try { + await channel.assertExchange(id, "fanout", { + durable: false, + }); + + // assertQueue isn't needed, because a queue will automatically created if it doesn't exist + const successful = channel.publish(id, "", Buffer.from(`${data}`), { type: payload.event }); + if (!successful) throw new Error("failed to send event"); + } catch (e) { + // Check if this is a channel closed error and if we should retry + const errorMessage = e instanceof Error ? e.message : String(e); + const isChannelError = errorMessage.includes("Channel closed") || errorMessage.includes("IllegalOperationError") || errorMessage.includes("RESOURCE_ERROR"); + + if (isChannelError && retryCount < 1) { + console.log("[RabbitMQ] Channel error detected, retrying with new channel..."); + // Force the cached channel to be discarded by calling getSafeChannel which will create a new one + return RabbitMQ.publishEvent(id, payload, retryCount + 1); + } + + console.log("[RabbitMQ] ", e); + } + } } export async function rabbitListen(channel: Channel, id: string, callback: (event: EventOpts) => unknown, opts?: { acknowledge?: boolean }): Promise<() => Promise<void>> {