-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
105 lines (91 loc) · 2.73 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
var amqp = require('amqp');
var uuid = require('uuid');
var util = require('util');
var fs = require('fs');
exports.log = console;
var conns = [];
exports.connectEx = function (options, callback) {
options = options || {};
var conn = conns[options.url] = (conns[options.url] || amqp.createConnection({url: options.url, heartbeat: options.heartbeat}));
if (conn.isReady)
connected(conn, options.name, callback);
else {
conn.addListener('ready', readyListener);
conn.addListener('error', function (err) {
exports.log.error('amqp error', err);
// This is fairly terrible and will bring down the whole app if uncaught, but it's better than
// silently going into a rabbit hole. The only thing more terrible is that pun. :)
// We should probably rewrite this and make it an eventEmitter.
throw err;
});
conn.addListener('heartbeat', function () {
exports.log.debug('amqp heartbeat');
});
}
function readyListener() {
conn.removeListener('ready', readyListener);
conn.isReady = true;
connected(conn, options.name, callback);
}
};
// backward compat connect method
exports.connect = function (url, name, callback) {
return exports.connectEx({
url,
name
}, callback);
};
exports.close = function (url) {
if(!conns[url]) return;
conns[url].end();
delete conns[url];
};
function connected(conn, name, callback) {
exports.log.info("connected to", name, "on", conn.serverProperties.product);
conn.exchange(name + 'Xch', {type: 'fanout', durable: true, autoDelete: false}, function (exchange) {
function publish(message) {
exchange.publish("msg", message, {
mandatory: true,
deliveryMode: 2,
messageId: process.pid + "-" + uuid.v4()
});
}
function subscribeToWorkQueue(cb, fetchCount) {
fetchCount = fetchCount || 1;
conn.queue(name + 'Q', {durable: true, autoDelete: false}, function (q) {
q.bind(exchange, "*");
q.subscribe({ ack: true, prefetchCount: fetchCount }, function (json, headers, deliveryInfo, msg) {
cb({
data: json,
headers: headers,
deliveryInfo: deliveryInfo,
ack: function () {
msg.acknowledge();
}
});
});
});
}
function subscribeToFanoutQueue(cb) {
conn.queue(name + 'Q-' + process.pid + '-' + Math.round(100000 * Math.random()), {durable: false, exclusive: true}, function (q) {
q.bind(exchange, "*");
q.subscribe({ ack: true }, function (json, headers, deliveryInfo, msg) {
cb({
data: json,
headers: headers,
deliveryInfo: deliveryInfo,
ack: function () {
msg.acknowledge();
}
});
});
});
}
callback({
publish: publish,
subscribeToWorkQueue: subscribeToWorkQueue,
subscribeToFanoutQueue: subscribeToFanoutQueue
});
});
}