forked from pushkin-consortium-deprecated/deprecated1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.js
More file actions
61 lines (61 loc) · 1.72 KB
/
rpc.js
File metadata and controls
61 lines (61 loc) · 1.72 KB
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
// returns a promise that resolves to the result of the RPC
const winston = require('./logger');
module.exports = function(conn, channelName, body) {
return new Promise((resolve, reject) => {
return conn.createChannel((err, ch) => {
if (err) {
return reject(err);
}
// create a unique queue
return ch.assertQueue(
'',
{
exclusive: true,
arguments: {
'x-expires': 3 * 60 * 1000
}
},
(err, q) => {
if (err) {
return reject(err);
}
// generate a unique id to listen for unique responses
var corr = generateUuid();
ch.consume(
q.queue,
msg => {
// When the connection is closed it sends a blank message
// check to make sure this isnt that
if (msg) {
const content = JSON.parse(msg.content.toString('utf8'));
winston.info('received', content);
if (msg.properties.correlationId === corr) {
// this is result of the RPC;
// winston.info('is a match')
ch.deleteQueue(q.queue);
ch.close();
resolve(content);
// conn.close();
}
}
},
{
noAck: true
}
);
return ch.sendToQueue(channelName, new Buffer(JSON.stringify(body)), {
correlationId: corr,
replyTo: q.queue
});
}
);
});
});
};
function generateUuid() {
return (
Math.random().toString() +
Math.random().toString() +
Math.random().toString()
);
}