-
Notifications
You must be signed in to change notification settings - Fork 67
/
pubsubqueue.js
156 lines (134 loc) · 5.25 KB
/
pubsubqueue.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var events = require('events');
var extend = require('extend');
var newId = require('node-uuid').v4;
var util = require('util');
function PubSubQueue (options) {
options = options || {};
var exchangeOptions = options.exchangeOptions || {};
var queueOptions = options.queueOptions || {};
extend(queueOptions, {
autoDelete: options.autoDelete || ! (options.ack || options.acknowledge),
contentType: options.contentType || 'application/json',
durable: Boolean(options.ack || options.acknowledge),
exclusive: options.exclusive || false,
persistent: Boolean(options.ack || options.acknowledge || options.persistent)
});
extend(exchangeOptions, {
type: exchangeOptions.type || 'topic',
durable: exchangeOptions.durable === false ? false : true,
autoDelete: exchangeOptions.autoDelete || false
});
this.ack = (options.ack || options.acknowledge);
this.bus = options.bus;
this.confirmChannel = options.confirmChannel;
this.correlator = options.correlator;
this.errorQueueName = options.queueName + '.error';
this.exchangeName = options.exchangeName || this.bus.exchangeName || 'amq.topic';
this.exchangeOptions = exchangeOptions;
this.formatter = options.formatter;
this.initialized = false;
this.listening = false;
this.listenChannel = options.listenChannel;
this.log = options.log;
this.maxRetries = options.maxRetries || 3;
this.queueName = options.queueName;
this.queueOptions = queueOptions;
this.rejected = {};
this.routingKey = options.routingKey;
this.sendChannel = options.sendChannel;
this.log('asserting exchange %s', this.exchangeName);
this.sendChannel.assertExchange(this.exchangeName, this.exchangeOptions.type || 'topic', this.exchangeOptions);
if (this.confirmChannel) {
this.confirmChannel.assertExchange(this.exchangeName, this.exchangeOptions.type || 'topic', this.exchangeOptions);
}
events.EventEmitter.call(this);
this.setMaxListeners(Infinity);
}
util.inherits(PubSubQueue, events.EventEmitter);
PubSubQueue.prototype.publish = function publish (event, options, cb) {
options = options || {};
var self = this;
options.contentType = options.contentType || this.contentType;
var channel = cb ? self.confirmChannel : self.sendChannel;
channel.publish(self.exchangeName, self.routingKey || self.queueName, new Buffer(options.formatter.serialize(event)), options, cb);
};
PubSubQueue.prototype.subscribe = function subscribe (options, callback) {
var self = this;
var subscribed = false;
var subscription = null;
this.log('subscribing to queue %j with routingKey %j', this.queueName, this.routingKey);
function _unsubscribe (cb) {
if (subscribed) {
// should we prevent multiple cancel calls?
self.listenChannel
.cancel(subscription.consumerTag)
.then(function () {
self.emit('unlistened');
if (cb) {
cb();
}
});
} else {
self.on('subscribed', _unsubscribe.bind(this, cb));
}
}
function _subscribe (uniqueName) {
self.listenChannel.consume(uniqueName, function (message) {
/*
Note from http://www.squaremobius.net/amqp.node/doc/channel_api.html
& http://www.rabbitmq.com/consumer-cancel.html:
If the consumer is cancelled by RabbitMQ, the message callback will be invoked with null.
*/
if (message === null) {
return;
}
// todo: map contentType to default formatters
message.content = options.formatter.deserialize(message.content);
options.queueType = 'pubsubqueue';
self.bus.handleIncoming(self.listenChannel, message, options, function (channel, message, options) {
// amqplib intercepts errors and closes connections before bubbling up
// to domain error handlers when they occur non-asynchronously within
// callback. Therefore, if there is a process domain, we try-catch to
// redirect the error, assuming the domain creator's intentions.
try {
callback(message.content, message);
} catch (err) {
if (process.domain && process.domain.listeners('error')) {
process.domain.emit('error', err);
} else {
self.emit('error', err);
}
}
});
}, { noAck: ! self.ack })
.then(function (ok) {
subscribed = true;
subscription = { consumerTag: ok.consumerTag };
self.emit('subscribed');
});
}
self.correlator.queueName(options, function (err, uniqueName) {
if (err) throw err;
self.listenChannel.assertQueue(uniqueName, self.queueOptions)
.then(function (qok) {
return self.listenChannel.bindQueue(uniqueName, self.exchangeName, self.routingKey || self.queueName);
}).then(function () {
if (self.ack) {
self.log('asserting error queue ' + self.errorQueueName);
var errorQueueOptions = extend(self.queueOptions, {
autoDelete: options.autoDeleteErrorQueue || false
});
self.listenChannel.assertQueue(self.errorQueueName, errorQueueOptions)
.then(function (_qok) {
_subscribe(uniqueName);
});
} else {
_subscribe(uniqueName);
}
});
});
return {
unsubscribe: _unsubscribe
};
};
module.exports = PubSubQueue;