-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathindex.js
145 lines (123 loc) · 3.45 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
'use strict';
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var async = require('async');
var AWS = require('aws-sdk');
var debug = require('debug')('sqs-consumer');
var requiredOptions = [
'queueUrl',
'handleMessage'
];
function validate(options) {
requiredOptions.forEach(function (option) {
if (!options[option]) {
throw new Error('Missing SQS consumer option [' + option + '].');
}
});
if (options.batchSize > 10 || options.batchSize < 1) {
throw new Error('SQS batchSize option must be between 1 and 10.');
}
}
/**
* An SQS consumer.
* @param {object} options
* @param {string} options.queueUrl
* @param {string} options.region
* @param {function} options.handleMessage
* @param {array} options.messageAttributeNames
* @param {number} options.batchSize
* @param {object} options.sqs
*/
function Consumer(options) {
validate(options);
this.queueUrl = options.queueUrl;
this.handleMessage = options.handleMessage;
this.messageAttributeNames = options.messageAttributeNames || [];
this.stopped = true;
this.batchSize = options.batchSize || 1;
this.sqs = options.sqs || new AWS.SQS({
region: options.region || 'eu-west-1'
});
}
util.inherits(Consumer, EventEmitter);
/**
* Construct a new Consumer
*/
Consumer.create = function (options) {
return new Consumer(options);
};
/**
* Start polling for messages.
*/
Consumer.prototype.start = function () {
if (this.stopped) {
debug('Starting consumer');
this.stopped = false;
this._poll();
}
};
/**
* Stop polling for messages.
*/
Consumer.prototype.stop = function () {
debug('Stopping consumer');
this.stopped = true;
};
Consumer.prototype._poll = function () {
var receiveParams = {
QueueUrl: this.queueUrl,
MessageAttributeNames: this.messageAttributeNames,
MaxNumberOfMessages: this.batchSize,
WaitTimeSeconds: 20
};
if (!this.stopped) {
debug('Polling for messages');
this.sqs.receiveMessage(receiveParams, this._handleSqsResponse.bind(this));
}
};
Consumer.prototype._handleSqsResponse = function (err, response) {
if (err) this.emit('error', new Error('SQS receive message failed: ' + err.message));
var consumer = this;
debug('Received SQS response');
debug(response);
if (response && response.Messages && response.Messages.length > 0) {
async.each(response.Messages, this._processMessage.bind(this), function () {
// start polling again once all of the messages have been processed
consumer._poll();
});
} else {
// there were no messages, so start polling again
this._poll();
}
};
Consumer.prototype._processMessage = function (message, cb) {
var consumer = this;
this.emit('message_received', message);
async.series([
function handleMessage(done) {
consumer.handleMessage(message, done);
},
function deleteMessage(done) {
consumer._deleteMessage(message, done);
}
], function (err) {
if (err) {
consumer.emit('error', err);
} else {
consumer.emit('message_processed', message);
}
cb();
});
};
Consumer.prototype._deleteMessage = function (message, cb) {
var deleteParams = {
QueueUrl: this.queueUrl,
ReceiptHandle: message.ReceiptHandle
};
debug('Deleting message %s', message.MessageId);
this.sqs.deleteMessage(deleteParams, function (err) {
if (err) return cb(new Error('SQS delete message failed: ' + err.message));
cb();
});
};
module.exports = Consumer;