-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsHandler.js
66 lines (54 loc) · 1.43 KB
/
jsHandler.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const RABBIT_CONFIG = {
HOST: "rabbitmq",
PORT: "5672",
USER: "node-publisher",
PASSWORD: "F]k[N$u6SMY2Rum-",
}
const IMAGES_CONFIG = {
EXCHANGE: "images",
QUEUE: "images.process",
};
const sendRequest = (channel, body, callbackQueue) => {
let corrId = crypto.randomUUID();
channel.publish(
exchange=IMAGES_CONFIG.EXCHANGE,
routingKey=IMAGES_CONFIG.QUEUE,
body,
{
replyTo: callbackQueue,
correlationId: corrId,
}
);
};
msg.payload = await (
async () => {
let response = null;
const conn = await amqplib.connect(
`amqp://${RABBIT_CONFIG.USER}:${RABBIT_CONFIG.PASSWORD}@${RABBIT_CONFIG.HOST}:${RABBIT_CONFIG.PORT}`
);
const ch = await conn.createChannel();
const callbackQueue = await ch.assertQueue(
"",
{
exclusive: true,
}
);
ch.consume(
callbackQueue.queue,
(mg) => {
response = mg.content.toString();
ch.ack(mg);
},
{
noAck: false,
}
);
sendRequest(ch, msg.payload, callbackQueue.queue);
while(!response) {
await new Promise(resolve => setTimeout(resolve, 100));
}
ch.close();
conn.close();
return response;
}
)();