-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
33 lines (29 loc) · 1005 Bytes
/
worker.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
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
if (err) {
console.log(err);
return err
}
conn.createChannel((err, channel) => {
if (err) {
console.log(err);
return err
}
const q = 'task_queue'; // declaring the queue from which we are going to consume
channel.assertQueue(q, {durable: true});
channel.prefetch(1);
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
// Listening to the messages
channel.consume(q, (msg) => {
// we will set the setTimeout to number of dots secs
const secs = msg.content.toString().split('.').length - 1;
console.log(" [x] Received %s", msg.content.toString());
// we are using setTimeout to fake the time taken by process
setTimeout(() => {
console.log(" [x] Done");
// sending ack back after getting done
channel.ack(msg);
}, secs * 1000);
}, {noAck: false});
});
});