-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
539 lines (469 loc) · 14.9 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
var _ = require("lodash");
var amqp = require("amqp");
var async = require("async");
var debug = require("debug");
var Readable = require("stream").Readable;
var Writable = require("stream").Writable;
var Transform = require("stream").Transform;
var EventEmitter = require('events').EventEmitter;
var streamsDebug = debug("rabbitmq-queue-stream:streams");
exports.init = function(numStreams, options, cb) {
if(!cb) {
cb = options;
options = {};
}
var streams = new AMQPStreams(numStreams, options);
streams.initialize(cb);
};
var RequeueMessage = function(message) {
if(!message._meta) {
return console.error();
}
message._meta.requeue = true;
return message;
};
var RejectMessage = function(message) {
if(!message._meta) {
return console.error();
}
message._meta.reject = true;
return message;
};
exports.RequeueMessage = RequeueMessage;
exports.RejectMessage = RejectMessage;
/*
@param numStreams
@params options
options.url - amqp url
options.connection.* - Anything accepted by amqp.createConnection's first arg (See: https://github.com/postwait/node-amqp#connection-options-and-url)
options.nodeAmqp.* - Anything accepted by amqp.createConnection's second arg (See: https://github.com/postwait/node-amqp#connection-options-and-url)
options.queue.connection.* - Anything accepted by connection.queue() (See: https://github.com/postwait/node-amqp#connectionqueuename-options-opencallback)
DEFAULT: { passive: true }
options.queue.subscribe.* - Anything accepted by queue.subscribe() (See: https://github.com/postwait/node-amqp#queuesubscribeoptions-listener)
DEFAULT: { ack: true, prefetchCount: 1 }
*/
function AMQPStreams(numStreams, options) {
EventEmitter.call(this);
this.__numStreams = numStreams || 1;
this.__options = options;
this.channels = [];
}
AMQPStreams.prototype = Object.create(EventEmitter.prototype);
AMQPStreams.prototype.initialize = function(cb) {
streamsDebug("Initializing " + this.__numStreams + " streams");
var me = this;
this._createConnection(this.__options.connection, this.__options.nodeAmqp, function(err, connection) {
if(err) {
return cb(err);
}
me._amqpConnection = connection;
me._connected = false;
connection.on('ready', function() {
me._connected = true;
});
connection.on('close', function() {
me._connected = false;
});
// forward connection events to `this`
['ready', 'error', 'close'].forEach(function(eventName) {
connection.on(eventName, function() {
// ignore error events while disconnecting, since those errors will be forwarded to
// disconnect's callback. See #disconnect()
if (eventName === 'error' && me._disconnecting) {
return;
}
var args = Array.prototype.slice.call(arguments);
args.unshift(eventName);
me.emit.apply(me, args);
});
});
//create individual stream channels to queue
var createWorker = function(n, cb) {
AMQPStream.create(connection, me.__options.queue, cb);
};
async.timesSeries(me.__numStreams, createWorker, function(err, channels) {
if(err) {
return cb(err);
}
me.channels = channels;
cb(null, me);
});
});
};
AMQPStreams.prototype._createConnection = function(connectionOpts, implOptions, cb) {
streamsDebug("Creating amqp connection.");
connectionOpts = connectionOpts || {};
var defaultOpts = {
heartbeat: 10,
clientProperties: {
capabilities: {
consumer_cancel_notify: true
}
}
};
var connection = amqp.createConnection(_.merge(defaultOpts, connectionOpts), implOptions || {});
function onError(err) {
streamsDebug("Error creating connection " + err.message);
connection.removeListener("ready", onReady);
return cb(err);
}
function onReady() {
streamsDebug("Successfully created connection");
connection.removeListener("error", onError);
return cb(null, connection);
}
/* handle successful or error on initial connection */
connection.once("error", onError);
connection.once("ready", onReady);
};
/*
* NOTE: A graceful disconnection routine from rabbitMQ should be
* done in the following order:
*
* AMQPStreams#unsubscribeConsumers - Tells queue to stop
* delivering messages to the queue consumer.
*
* AMQPStreams#closeConsumers- Closes the channel between
* the consumer and queue.
*
* AMQPStreams#disconnect- Closes the actual TCP connection
* to the AMQP source.
*/
/*
* Helper method that strings together the above disconnection
* routine.
*/
AMQPStreams.prototype.gracefulDisconnect = function(cb) {
async.series([
this.unsubscribeConsumers.bind(this),
this.closeConsumers.bind(this),
this.disconnect.bind(this)
], cb);
};
/*
* Stops fetching messages from the queue. Channels are kept open.
* Use AMQPStreams#closeConsumers to close the channels to queue.
*/
AMQPStreams.prototype.unsubscribeConsumers = function(cb) {
// noop if we're disconnected
if (!this._connected) {
streamsDebug("Skipping unsubscribeConsumers");
return cb();
}
//close every worker stream
async.eachSeries(this.channels, function(stream, next) {
stream.unsubscribe(next);
}, cb);
};
/*
* One AMQP Connection is multiplexed across multiple
* channels. This method closes only the channel
* corresponding to this queue stream. See AMQP#disconnect
* to close the actual AMQP connection. You should safely
* unsubsribe all queues before disconnecting from the
* AMQP server.
*
*/
AMQPStreams.prototype.closeConsumers = function(cb) {
// noop if we're disconnected
if (!this._connected) {
streamsDebug("Skipping closeConsumers");
return cb();
}
async.eachSeries(this.channels, function(stream, next) {
stream.close(next);
}, cb);
};
AMQPStreams.prototype.disconnect = function(cb) {
streamsDebug("Closing AMQP connection");
// noop if we're disconnected
if (!this._connected) {
streamsDebug("Skipping disconnect");
return cb();
}
this._disconnecting = true;
var me = this;
this._amqpConnection.disconnect();
var ignoreEconnresetError = function(err) {
/*
* Driver has a bug on some versions of RabbitMQ
* and node combinations where socket.close()
* causes an ECONNRESET. Catch and ignore
* that error for now. More info:
*
* https://github.com/postwait/node-amqp/issues/300
*/
if(_.contains(err.message, "ECONNRESET")) {
streamsDebug("Ignoring ECONNRESET error");
return;
}
cb(err);
};
this._amqpConnection.once("error", ignoreEconnresetError);
this._amqpConnection.once("close", function() {
this._disconnecting = false;
me._amqpConnection.removeListener("error", ignoreEconnresetError);
cb();
});
};
/*
* Resubscribe queue consumers.
*/
AMQPStreams.prototype.resubscribeConsumers = function(cb) {
async.eachSeries(this.channels, function(stream, next) {
if(!stream.subscribed) {
stream._subscribeToQueue(next);
} else {
next();
}
}, cb);
};
/*
@param connection = An object returned by amqp.createConnection
@param options
options.queueName
*/
function AMQPStream(connection, options, workerNum) {
this.id = workerNum;
this.__connection = connection;
this.__options = options || {};
this.__outstandingAcks = [];
this.__pendingQueue = [];
this.__debug = debug("rabbitmq-queue-stream:worker:" + (workerNum || "-"));
}
AMQPStream.create = function(connection, options, cb) {
this.__totalWorkers = this.__totalWorkers || 0;
this.__totalWorkers++;
var stream = new this(connection, options, this.__totalWorkers);
stream.initialize(cb);
};
AMQPStream.prototype.initialize = function(cb) {
var me = this;
this.__debug("Initializing");
if(!this.__options.name) {
throw new Error("You must provide a `name` to queueStream options object");
}
this._connectToQueue(this.__options.name, function(err, queue) {
if(err) {
return cb(err);
}
me.__queue = queue;
//attach a general queueErrorHandler here
queue.on("error", me.__options.onError || function() {});
//Last step is to streamify this queue by attaching stream .source and .sink properties
me._subscribeToQueue(function(err) {
if(err) {
me.__debug("Error subscribe to queue " + this.__options.name + ". " + err.message);
return cb(err);
}
me._streamifyQueue(cb);
});
});
};
AMQPStream.prototype._connectToQueue = function(queueName, cb) {
var me = this;
function onError(err) {
me.__debug("Error connecting to queue " + queueName + ": " + err.message);
return cb(err);
}
this.__connection.once("error", onError);
this.__connection.queue(queueName, _.merge({passive: true}, this.__options.connection), function(queue) {
me.__debug("Connected to queue " + queueName);
me.__connection.removeListener("error", onError);
return cb(null, queue);
});
};
AMQPStream.prototype._subscribeToQueue = function(cb) {
var me = this;
var queue = this.__queue;
/* TODO: Figure out how to error handle subscription. Maybe a 'once' error handler. */
queue.subscribe(
_.merge({ack: true, prefetchCount: 1}, this.__options.subscribe),
this._handleIncomingMessage.bind(this)
).addCallback(function(ok) {
me.__debug("Subscribed with consumer tag: " + ok.consumerTag);
me.__consumerTag = ok.consumerTag;
me.subscribed = true;
cb(null, me);
});
};
AMQPStream.prototype._handleIncomingMessage = function(message, headers, deliveryInfo, ack) {
var isJSON = deliveryInfo.contentType === "application/json";
var serializableMessage = {
payload: isJSON ? message : message.data,
headers: headers,
deliveryInfo: deliveryInfo,
_meta: {
/*
* ack is not serializable, so we need to push it
* onto the outstandingAck array attach
* an ackIndex number to the message
*/
ackIndex: this._insertAckIntoArray(ack)
}
};
if(isJSON && deliveryInfo.parseError) {
this.sink.write(RejectMessage(serializableMessage));
if(this.source.listeners('parseError').length) {
return this.source.emit("parseError", deliveryInfo.parseError, deliveryInfo.rawData);
} else {
console.error("Automatically rejecting malformed message. " +
"Add listener to 'parseError' for custom behavior");
return;
}
}
this.__debug("Received message. Inserted ack into index " + serializableMessage._meta.ackIndex);
this.__pendingQueue.push(serializableMessage);
};
AMQPStream.prototype._streamifyQueue = function(cb) {
var queueStream, prepareMessage;
var me = this;
var queue = this.__queue;
var sink;
/* Create the .source ReadableStream */
queueStream = new Readable({objectMode: true});
queueStream._read = function() {
me.__debug("_read .source");
me._waitForMessage(function(message) {
this.push(message);
}.bind(this));
};
prepareMessage = new Transform({objectMode: true});
prepareMessage._transform = function(message, enc, next) {
this.push(_.pick(message, "payload", "_meta"));
next();
};
this.source = queueStream.pipe(prepareMessage);
/* Create the .sink WritableStream */
sink = new Writable({objectMode: true});
sink._write = function(message, enc, next) {
me.__debug("_write .sink");
if(!message._meta || !_.isNumber(message._meta.ackIndex)) {
me.__debug("Could not find ackIndex in message " + message);
return this.emit("formatError", new Error("No ack index for message"), message);
}
var ackIndex = message._meta.ackIndex;
if(!me.__outstandingAcks[ackIndex]) {
//something went wrong and we can't ack message
me.__debug("Could not find ack function for " + message);
return this.emit("ackError", new Error("Cannot find ack for message."), message);
}
var evt;
if(message._meta.requeue) {
me.__outstandingAcks[ackIndex].reject(true);
evt = "requeued";
} else if(message._meta.reject) {
me.__outstandingAcks[ackIndex].reject(false);
evt = "rejected";
} else {
me.__outstandingAcks[ackIndex].acknowledge(false);
evt = "acknowledged";
}
me.__outstandingAcks[ackIndex] = null;
this.emit(evt, message);
next();
};
this.sink = sink;
cb(null, this);
};
AMQPStream.prototype._waitForMessage = function(cb) {
var i;
var me = this;
if(_.isEmpty(this.__pendingQueue)) {
this.__debug("Waiting for message");
i = setInterval(function() {
if(!_.isEmpty(me.__pendingQueue)) {
clearInterval(i);
me.__debug("Received messages. Continuing...");
return cb(me.__pendingQueue.shift());
}
}, 5);
} else {
this.__debug("Dequeueing pending message");
cb(this.__pendingQueue.shift());
}
};
AMQPStream.prototype._insertAckIntoArray = function(ack) {
for(var i = 0; i < this.__outstandingAcks.length; i++) {
if(!this.__outstandingAcks[i]) {
this.__outstandingAcks[i] = ack;
return i;
}
}
return this.__outstandingAcks.push(ack) - 1;
};
/*
* Unsubscribes from the queue and also closes
* the channel.
*/
AMQPStream.prototype.unsubscribe = function(cb) {
var me = this;
this.__debug("Unsubscribing with consumerTag " + this.__consumerTag);
if(this.subscribed) {
this.__queue.unsubscribe(this.__consumerTag).addCallback(function() {
me.subscribed = false;
cb();
});
} else {
this.__debug("Worker already unsubscribed");
cb();
}
};
AMQPStream.prototype.close = function(cb) {
var me = this;
this.__debug("Unsubscribing with consumerTag " + this.__consumerTag);
this.__queue.close(this.__consumer);
var closeHandler = function() {
me.__queue.removeListener("error", errorHandler);
cb();
};
var errorHandler = function(err) {
me.__queue.removeListener("close", closeHandler);
return cb(err);
};
this.__queue.once("close", closeHandler);
this.__queue.once("error", errorHandler);
};
/*
* Test helper that responds with with a mock `source`
* and `sink` properties. Only returns one channel.
*/
exports.createWithTestMessages = function(testMessages) {
var stubSource = new Readable({objectMode: true});
stubSource._read = function() {
var itemWrapper;
var nextItem = testMessages.shift();
if(!nextItem) {
return this.push(null);
}
itemWrapper = {
headers: {},
deliveryInfo: {},
payload: nextItem,
_meta: {}
};
this.push(itemWrapper);
};
var stubSink = new Writable({objectMode: true});
stubSink._write = function(item, enc, next) {
var evt;
if(item._meta.requeue) {
evt = "requeued";
} else if(item._meta.reject) {
evt = "rejected";
} else {
evt = "acknowledged";
}
this.emit(evt, item);
next();
};
return {
channels: [{
source: stubSource,
sink: stubSink
}]
};
};
/* export for testing */
exports.AMQPStreams = AMQPStreams;
exports.AMQPStream = AMQPStream;