@@ -29,6 +29,7 @@ const LambdaContext = require('serverless-offline/src/LambdaContext');
29
29
30
30
const NO_KINESIS_FOUND = 'Could not find kinesis stream' ;
31
31
const KINESIS_RETRY_DELAY = 200 ;
32
+ const KINESIS_RETRY_TIMEOUT = 30000 ;
32
33
33
34
const printBlankLine = ( ) => console . log ( ) ;
34
35
@@ -166,65 +167,45 @@ class ServerlessOfflineKinesis {
166
167
if ( isString ( physicalResourceName ) ) return physicalResourceName ;
167
168
}
168
169
169
- this . serverless . cli . log ( `Could not resolve stream name for spec: ${ JSON . stringify ( streamEvent , null , 2 ) } ` ) ;
170
+ this . serverless . cli . log (
171
+ `Could not resolve stream name for spec: ${ JSON . stringify ( streamEvent , null , 2 ) } `
172
+ ) ;
170
173
171
174
throw new Error (
172
175
`StreamName not found. See https://github.com/CoorpAcademy/serverless-plugins/tree/master/packages/serverless-offline-kinesis#functions`
173
176
) ;
174
177
}
175
178
176
- // FIXME: to really incorporate [to be done after conflict resolving]
177
- pollStreamUntilActive ( streamName , timeout ) {
178
- const client = this . getClient ( ) ;
179
- const lastTime = Date . now ( ) + timeout ;
180
- return new Promise ( ( resolve , reject ) => {
181
- const poll = async ( ) => {
182
- const {
183
- StreamDescription : { StreamStatus}
184
- } = await client . describeStream ( { StreamName : streamName } ) . promise ( ) ;
185
- if ( StreamStatus === 'ACTIVE' ) {
186
- resolve ( ) ;
187
- } else if ( Date . now ( ) > lastTime ) {
188
- reject (
189
- new Error (
190
- `Stream ${ streamName } did not become active within timeout of ${ Math . floor (
191
- timeout / 1000
192
- ) } s`
193
- )
194
- ) ;
195
- } else {
196
- setTimeout ( poll , 1000 ) ;
197
- }
198
- } ;
199
- poll ( ) ;
200
- } ) ;
201
- }
202
-
203
- async createKinesisReadable ( functionName , streamEvent , retry = false ) {
179
+ async createKinesisReadable ( functionName , streamEvent , delay = null ) {
204
180
const client = this . getClient ( ) ;
205
181
const streamName = this . getStreamName ( streamEvent ) ;
206
182
207
183
this . serverless . cli . log ( `Waiting for ${ streamName } to become active` ) ;
208
184
209
- await this . pollStreamUntilActive ( streamName , this . getConfig ( ) . waitForActiveTimeout || 30000 ) ; // FIXME
210
-
211
185
const kinesisStream = await client
212
186
. describeStream ( {
213
187
StreamName : streamName
214
188
} )
215
189
. promise ( )
190
+ . then ( ( { StreamDescription} ) => {
191
+ if ( StreamDescription . StreamStatus !== 'ACTIVE' )
192
+ throw new Error ( 'Stream found but not yet active' ) ;
193
+ return { StreamDescription} ;
194
+ } )
216
195
. catch ( err => err ) ;
217
196
218
197
if ( kinesisStream instanceof Error ) {
219
- if ( ! retry ) throw new Error ( NO_KINESIS_FOUND ) ;
198
+ if ( delay === null ) throw new Error ( NO_KINESIS_FOUND ) ;
199
+ if ( delay < KINESIS_RETRY_DELAY )
200
+ throw new Error ( `Stream ${ streamName } did not become active within specified timeout` ) ;
220
201
221
202
this . serverless . cli . log (
222
203
`${ streamName } - not found because of ${
223
204
kinesisStream . code
224
205
} , retrying in ${ KINESIS_RETRY_DELAY } ms`
225
206
) ;
226
207
return setTimeout ( ( ) => {
227
- this . createKinesisReadable ( functionName , streamEvent , retry ) ;
208
+ this . createKinesisReadable ( functionName , streamEvent , delay - KINESIS_RETRY_DELAY ) ;
228
209
} , KINESIS_RETRY_DELAY ) ;
229
210
}
230
211
@@ -286,8 +267,10 @@ class ServerlessOfflineKinesis {
286
267
printBlankLine ( ) ;
287
268
this . serverless . cli . log ( `Kinesis for ${ functionName } :` ) ;
288
269
270
+ const waitForStreamDelay = this . getConfig ( ) . waitForActiveTimeout || KINESIS_RETRY_TIMEOUT ;
271
+ // ! FIXME: probably rename (and document the variable name)
289
272
forEach ( streamEvent => {
290
- this . createKinesisReadable ( functionName , streamEvent , true ) ; // TMP: retry is not configurable so far
273
+ this . createKinesisReadable ( functionName , streamEvent , waitForStreamDelay ) ;
291
274
} , streams ) ;
292
275
293
276
printBlankLine ( ) ;
0 commit comments