File size: 825 Bytes
c7fbc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

import { getChannel } from "@/lib/lavinmq";

export const QUEUE_NAME = "publish_jobs";

export async function enqueuePublishJob(postId: string) {
    const channel = await getChannel();

    // assertQueue checks if the queue exists, if not it creates it.
    // durable: true means the queue survives broker restarts
    await channel.assertQueue(QUEUE_NAME, { durable: true });

    const message = JSON.stringify({ postId });

    // persistent: true means messages are saved to disk
    const sent = channel.sendToQueue(QUEUE_NAME, Buffer.from(message), {
        persistent: true
    });

    if (sent) {
        console.log(`[LavinMQ] Enqueued job for post ${postId}`);
    } else {
        console.error(`[LavinMQ] Failed to enqueue job for post ${postId}`);
        throw new Error("Failed to enqueue job");
    }
}