-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathworker.js
More file actions
executable file
·34 lines (28 loc) · 929 Bytes
/
worker.js
File metadata and controls
executable file
·34 lines (28 loc) · 929 Bytes
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
#!/usr/bin/env node
var amqp = require('amqplib');
var conn = amqp.connect('amqp://localhost');
conn.then(createChannel).then(null, console.warn);
function createChannel(conn) {
process.once('SIGINT', function() { conn.close(); });
return conn.createChannel().then(consume);
}
function consume(ch) {
var ok = ch.assertQueue('task_queue', {durable: true});
ok = ok.then(function() { ch.prefetch(1); });
ok = ok.then(function(_ignore) {
return ch.consume(
'task_queue',
function doSomeWork(msg) {
console.log(" [x] Received '%s'", msg.content.toString());
var secs = msg.content.toString().split('.').length - 1;
setTimeout(function() {
console.log(" [x] Done");
ch.ack(msg);
}, secs * 1000);
},
{noAck: false});
});
return ok.then(function(_consumeOk) {
console.log(' [*] Waiting for messages. To exit press CTRL+C');
});
}