-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickqueue.js
More file actions
269 lines (209 loc) · 6.96 KB
/
quickqueue.js
File metadata and controls
269 lines (209 loc) · 6.96 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'use strict';
const Async = require('async');
const Amqp = require('amqplib');
const EventEmitter = require('events');
const Util = require('util');
const internals = {
messages: {},
channel: null,
/**
* Add a message to the messages hash if it's currently not included
*
* @param {object} msg The message to queue
*/
pushMessage: function (msg) {
const deliveryTag = msg.fields.deliveryTag;
if (!this.messages[deliveryTag]) {
this.messages[deliveryTag] = msg;
}
},
/**
* Check if a message has been queued or not
*
* @param {object} msg The message that will be checked
*/
isQueued: function (msg) {
if (this.messages[msg.fields.deliveryTag]) {
return true;
}
return false;
}
};
const QuickQueue = function () {
this.channel;
this.exName;
};
Util.inherits(QuickQueue, EventEmitter);
QuickQueue.prototype.getMessages = function () {
return internals.messages;
};
/**
* Initialize an AMQP setup
*
* @param {string} uri The URI of the AMQP server
*
* @param {hash} config The configuration object. Must include options,
* exchange, & queues. All queues in the queues array are created, if they don't
* already exist. The exchange is created & all the queues are bound to it.
*
* const config = options: {
* // These options are applied to the exchange & queues as applicable.
* durable: true,
* persistent: false,
* ...
* },
* exchange: { // All of the queues will be bound to this exchange.
* name: 'theExchangeName',
* type: 'topic'
* },
* queues: [
* { name: 'queue1', routingKey: 'rainbows' },
* { name: 'queue2', routingKey: 'butterflies' },
* ...
* ]
* }
*
* @returns {promise} promise The promise resolves with the channel created or
* rejects with an error message.
*/
QuickQueue.prototype.initialize = function (uri, config) {
const connection = Amqp.connect(uri);
const promise = new Promise((resolve, reject) => {
connection.catch((err) => {
console.error(err);
reject(err);
});
connection.then((conn) => {
this.channel = conn.createConfirmChannel();
this.channel.then((ch) => {
internals.channel = ch;
this.exName = config.exchange.name;
const exType = config.exchange.type;
const exchange = ch.assertExchange(this.exName,
exType,
config.options);
Async.map(config.queues, (item, cb) => {
const queue = exchange.then((ex) => {
return ch.assertQueue(item.name, config.options);
});
queue.then((q) => {
console.log('Created ' + item.name + ' queue.');
ch.bindQueue(item.name,
config.exchange.name,
item.routingKey).then(() => {
console.log('Bound to exchange.');
cb(null, item);
}).catch((error) => {
console.error(error);
reject(error);
});
});
queue.catch((error) => {
console.error(error);
reject(error);
});
}, () => {
resolve(this.channel);
});
});
});
});
return promise;
};
/**
* Acknowledge a message
*
* @param {object} msg The message to acknowledge
*/
QuickQueue.prototype.ackMessage = function (msg) {
const deliveryTag = msg.fields.deliveryTag;
if (internals.messages[deliveryTag]) {
internals.channel.ack(msg);
delete internals.messages[deliveryTag];
}
};
/**
* Publish messsages to an exchange with a routing key
*
* @param {hash} options A hash of exchange publising options
*
* @param {string} routing_key The routing key as a string
*
* @param {array} messages An array of messages to be published
*
* @param {hash} eventNames (optional) A hash of events that will be
* emitted. Expects keys & values for any or all of the following 3 events:
* published, notPublished, & completed. Defaults to 'published', 'error',
* & 'allPublished'. The completed event is emitted if all the messages have
* successfully been published. The published event returns the message that was
* published, the notPublished event returns the error & the message that was not
* published.
*/
QuickQueue.prototype.enqueue = function (options, routing_key, messages, eventNames) {
const buffers = [];
let allPublished = true;
let published = 'published';
let notPublished = 'error';
let completed = 'allPublished';
if (typeof eventNames === 'object') {
if (eventNames.published) {
published = eventNames.published;
}
if (eventNames.notPublished) {
notPublished = eventNames.notPublished;
}
if (eventNames.completed) {
completed = eventNames.completed;
}
}
for (let i = 0; i < messages.length; ++i) {
buffers.push(new Buffer(messages[i]));
}
Async.map(buffers, (item, cb) => {
internals.channel.publish(this.exName, routing_key, item, options, (err, ok) => {
if (err !== null) {
this.emit(notPublished, err, item);
allPublished = false;
}
else {
this.emit(published, item);
}
cb(null, item);
});
}, () => {
if (allPublished) {
this.emit(completed);
}
});
};
/**
* Setup a consumer to consume messages from a given queue
*
* @param {string} queue The name of the queue to consume
*
* @param {hash} options A hash of amqlib options used to setup the
* consumer
*
* @param {string} eventName (optional) Name of event to emit when a message
* is consumed. Default is 'dequeue'. The event returns the message that was
* consumed & an AMQP channel. The message is not acknowledged.
*/
QuickQueue.prototype.dequeue = function (options, queue, eventName) {
const consumerEvent = eventName || 'dequeue';
this.channel.then((ch) => {
internals.channel.consume(queue, (msg) => {
const notQueued = !internals.isQueued(msg);
internals.pushMessage(msg);
/**
* If this message has already been seen, don't emit event or run
* callback
*/
if (notQueued) {
this.emit(consumerEvent, msg, ch);
}
}, options);
});
};
const exportedQueue = new QuickQueue();
exportedQueue.setMaxListeners(0);
module.exports = exportedQueue;