Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(nuq): async race condition when starting listener channels

Gergő Móriczcfe2714cf4fd
diff --git a/apps/api/src/services/worker/nuq.ts b/apps/api/src/services/worker/nuq.ts
index 30b876eb0..6ca177d20 100644
--- a/apps/api/src/services/worker/nuq.ts
+++ b/apps/api/src/services/worker/nuq.ts
@@ -87,34 +87,41 @@ class NuQ<JobData = any, JobReturnValue = any> {
private listens: {
[key: string]: ((status: "completed" | "failed") => void)[];
} = {};
+ private listenerStarting = false;
private shuttingDown = false;
private async startListener() {
- if (this.listener || this.shuttingDown) return;
+ if (this.listener || this.shuttingDown || this.listenerStarting) return;
if (process.env.NUQ_RABBITMQ_URL) {
- const connection = await amqp.connect(process.env.NUQ_RABBITMQ_URL);
- const channel = await connection.createChannel();
- await channel.prefetch(1);
- const queue = await channel.assertQueue(
- this.queueName + ".listen." + this.listenChannelId,
- {
- exclusive: true,
- autoDelete: true,
- durable: false,
- arguments: {
- "x-queue-type": "classic",
- "x-message-ttl": 60000,
+ this.listenerStarting = true;
+
+ try {
+ const connection = await amqp.connect(process.env.NUQ_RABBITMQ_URL);
+ const channel = await connection.createChannel();
+ await channel.prefetch(1);
+ const queue = await channel.assertQueue(
+ this.queueName + ".listen." + this.listenChannelId,
+ {
+ exclusive: true,
+ autoDelete: true,
+ durable: false,
+ arguments: {
+ "x-queue-type": "classic",
+ "x-message-ttl": 60000,
+ },
},
- },
- );
+ );
- this.listener = {
- type: "rabbitmq",
- connection,
- channel,
- queue: queue.queue,
- };
+ this.listener = {
+ type: "rabbitmq",
+ connection,
+ channel,
+ queue: queue.queue,
+ };
+ } finally {
+ this.listenerStarting = false;
+ }
let reconnectTimeout: NodeJS.Timeout | null = null;