2
2
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
3
3
const SQS = require ( "aws-sdk/clients/sqs" ) ;
4
4
const Debug = require ( "debug" ) ;
5
+ const crypto = require ( "crypto" ) ;
5
6
const events_1 = require ( "events" ) ;
6
7
const bind_1 = require ( "./bind" ) ;
7
8
const errors_1 = require ( "./errors" ) ;
@@ -11,6 +12,9 @@ const requiredOptions = [
11
12
// only one of handleMessage / handleMessagesBatch is required
12
13
'handleMessage|handleMessageBatch'
13
14
] ;
15
+ function generateUuid ( ) {
16
+ return crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
17
+ }
14
18
function createTimeout ( duration ) {
15
19
let timeout ;
16
20
const pending = new Promise ( ( _ , reject ) => {
@@ -67,19 +71,19 @@ class Consumer extends events_1.EventEmitter {
67
71
this . handleMessageBatch = options . handleMessageBatch ;
68
72
this . pollingStartedInstrumentCallback = options . pollingStartedInstrumentCallback ;
69
73
this . pollingFinishedInstrumentCallback = options . pollingFinishedInstrumentCallback ;
70
- this . batchSizeUpdatedInstrument = options . batchSizeUpdatedInstrument ;
71
74
this . handleMessageTimeout = options . handleMessageTimeout ;
72
75
this . attributeNames = options . attributeNames || [ ] ;
73
76
this . messageAttributeNames = options . messageAttributeNames || [ ] ;
74
77
this . stopped = true ;
75
78
this . batchSize = options . batchSize || 1 ;
76
- this . concurencyLimit = options . concurencyLimit || 30 ;
79
+ this . concurrencyLimit = options . concurrencyLimit || 30 ;
80
+ this . freeConcurrentSlots = this . concurrencyLimit ;
77
81
this . visibilityTimeout = options . visibilityTimeout ;
78
82
this . terminateVisibilityTimeout = options . terminateVisibilityTimeout || false ;
79
83
this . waitTimeSeconds = options . waitTimeSeconds || 20 ;
80
84
this . authenticationErrorTimeout = options . authenticationErrorTimeout || 10000 ;
81
85
this . pollingWaitTimeMs = options . pollingWaitTimeMs || 0 ;
82
- this . pollingWaitTimeMsBatchSizeZero = options . pollingWaitTimeMsBatchSizeZero || 5 ;
86
+ this . msDelayOnEmptyBatchSize = options . msDelayOnEmptyBatchSize || 5 ;
83
87
this . sqs =
84
88
options . sqs ||
85
89
new SQS ( {
@@ -106,8 +110,7 @@ class Consumer extends events_1.EventEmitter {
106
110
}
107
111
async reportMessageFromBatchFinished ( message , error ) {
108
112
debug ( 'Message from batch has finised' ) ;
109
- this . concurencyLimit ++ ;
110
- this . updateBatchSize ( ) ;
113
+ this . freeConcurrentSlots ++ ;
111
114
try {
112
115
if ( error )
113
116
throw error ;
@@ -119,22 +122,8 @@ class Consumer extends events_1.EventEmitter {
119
122
}
120
123
}
121
124
reportNumberOfMessagesReceived ( numberOfMessages ) {
122
- debug ( 'decrementing next batch size' ) ;
123
- this . concurencyLimit = this . concurencyLimit - numberOfMessages ;
124
- this . updateBatchSize ( ) ;
125
- }
126
- updateBatchSize ( ) {
127
- debug ( 'Updating next batch size' ) ;
128
- this . batchSize = Math . min ( 10 , this . concurencyLimit ) ;
129
- // instrument current batch size
130
- if ( this . batchSizeUpdatedInstrument ) {
131
- this . batchSizeUpdatedInstrument ( {
132
- instanceId : process . env . HOSTNAME ,
133
- queueUrl : this . queueUrl ,
134
- currentConcurencyLimit : this . concurencyLimit ,
135
- batchSize : this . batchSize
136
- } ) ;
137
- }
125
+ debug ( 'Reducing number of messages received from freeConcurrentSlots' ) ;
126
+ this . freeConcurrentSlots = this . freeConcurrentSlots - numberOfMessages ;
138
127
}
139
128
async handleSqsResponse ( response ) {
140
129
debug ( 'Received SQS response' ) ;
@@ -256,22 +245,22 @@ class Consumer extends events_1.EventEmitter {
256
245
this . emit ( 'stopped' ) ;
257
246
return ;
258
247
}
248
+ const pollBatchSize = Math . min ( this . batchSize , this . freeConcurrentSlots ) ;
259
249
debug ( 'Polling for messages' ) ;
260
250
if ( this . pollingStartedInstrumentCallback ) {
261
251
this . pollingStartedInstrumentCallback ( {
262
252
instanceId : process . env . HOSTNAME ,
263
253
queueUrl : this . queueUrl ,
264
- // instrument i am about to request this.batchSize messages
265
- batchSize : this . batchSize
254
+ pollBatchSize
266
255
} ) ;
267
256
}
268
257
let currentPollingTimeout = this . pollingWaitTimeMs ;
269
- if ( this . batchSize > 0 ) {
258
+ if ( pollBatchSize > 0 ) {
270
259
const receiveParams = {
271
260
QueueUrl : this . queueUrl ,
272
261
AttributeNames : this . attributeNames ,
273
262
MessageAttributeNames : this . messageAttributeNames ,
274
- MaxNumberOfMessages : this . batchSize ,
263
+ MaxNumberOfMessages : pollBatchSize ,
275
264
WaitTimeSeconds : this . waitTimeSeconds ,
276
265
VisibilityTimeout : this . visibilityTimeout
277
266
} ;
@@ -293,15 +282,45 @@ class Consumer extends events_1.EventEmitter {
293
282
} ) ;
294
283
}
295
284
else {
296
- setTimeout ( this . poll , this . pollingWaitTimeMsBatchSizeZero ) ;
285
+ setTimeout ( this . poll , this . msDelayOnEmptyBatchSize ) ;
297
286
}
298
287
}
299
288
async processMessageBatch ( messages ) {
300
289
messages . forEach ( message => {
301
290
this . emit ( 'message_received' , message ) ;
302
291
} ) ;
303
292
this . reportNumberOfMessagesReceived ( messages . length ) ;
304
- this . handleMessageBatch ( messages , this ) ;
293
+ const batchUuid = generateUuid ( ) ;
294
+ if ( this . batchStartedInstrumentCallBack ) {
295
+ this . batchStartedInstrumentCallBack ( {
296
+ instanceId : process . env . HOSTNAME ,
297
+ queueUrl : this . queueUrl ,
298
+ batchUuid,
299
+ numberOfMessages : messages . length
300
+ } ) ;
301
+ }
302
+ try {
303
+ await this . handleMessageBatch ( messages , this ) ;
304
+ if ( this . batchFinishedInstrumentCallBack ) {
305
+ this . batchFinishedInstrumentCallBack ( {
306
+ instanceId : process . env . HOSTNAME ,
307
+ queueUrl : this . queueUrl ,
308
+ batchUuid,
309
+ numberOfMessages : messages . length
310
+ } ) ;
311
+ }
312
+ }
313
+ catch ( err ) {
314
+ if ( this . batchFailedInstrumentCallBack ) {
315
+ this . batchFailedInstrumentCallBack ( {
316
+ instanceId : process . env . HOSTNAME ,
317
+ queueUrl : this . queueUrl ,
318
+ batchUuid,
319
+ numberOfMessages : messages . length
320
+ } ) ;
321
+ }
322
+ throw err ;
323
+ }
305
324
}
306
325
}
307
326
exports . Consumer = Consumer ;
0 commit comments