-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommentSyncer.js
52 lines (47 loc) · 1.43 KB
/
commentSyncer.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
var amqp = require('amqplib/callback_api');
var async = require('async');
var request = require('request');
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var q = 'commentSync';
ch.assertQueue(q, {
durable: true
});
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
var prefetchCount = 1;
//process messages one at a time
ch.prefetch(prefetchCount);
ch.consume(q, function(msg) {
var comment = JSON.parse(msg.content);
console.log(comment);
async.series([
syncComment.bind(null, comment)
], function(err) {
if (err)
console.log('Could not sync comment due to error', err);
else
console.log('Successfully synced comment');
ch.ack(msg);
});
}, {
noAck: false
});
});
});
function syncComment(comment, next) {
request({
uri: 'http://localhost:3000/comments',
headers: {
'content-type': 'application/json',
'accept': 'application/json'
},
method: 'POST',
json: comment
},
function(error, res) {
if (error)
return next(error);
return next();
}
);
}