Skip to content

Commit 4bdb23f

Browse files
committed
Very basic retry mecanism when kinesis stream (or kinesis) is not found
1 parent 44463fe commit 4bdb23f

File tree

1 file changed

+23
-6
lines changed
  • packages/serverless-offline-kinesis/src

1 file changed

+23
-6
lines changed

packages/serverless-offline-kinesis/src/index.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ const {
2525
const functionHelper = require('serverless-offline/src/functionHelper');
2626
const LambdaContext = require('serverless-offline/src/LambdaContext');
2727

28+
const NO_KINESIS_FOUND = 'Could not find kinesis stream';
29+
const KINESIS_RETRY_DELAY = 2000;
30+
2831
const fromCallback = fun =>
2932
new Promise((resolve, reject) => {
3033
fun((err, data) => {
@@ -151,22 +154,36 @@ class ServerlessOfflineKinesis {
151154
);
152155
}
153156

154-
async createKinesisReadable(functionName, streamEvent) {
157+
async createKinesisReadable(functionName, streamEvent, retry = false) {
155158
const client = this.getClient();
156159
const streamName = this.getStreamName(streamEvent);
157160

158161
this.serverless.cli.log(`${streamName}`);
159162

160-
const {
161-
StreamDescription: {Shards: shards}
162-
} = await fromCallback(cb =>
163+
const kinesisStream = await fromCallback(cb =>
163164
client.describeStream(
164165
{
165166
StreamName: streamName
166167
},
167168
cb
168169
)
169-
);
170+
).catch(err => null);
171+
if (!kinesisStream) {
172+
if (retry) {
173+
this.serverless.cli.log(`${streamName} - not Found, retrying in ${KINESIS_RETRY_DELAY}ms`);
174+
setTimeout(
175+
this.createKinesisReadable.bind(this),
176+
KINESIS_RETRY_DELAY,
177+
functionName,
178+
streamEvent,
179+
retry
180+
);
181+
return;
182+
} else throw new Error(NO_KINESIS_FOUND);
183+
}
184+
const {
185+
StreamDescription: {Shards: shards}
186+
} = kinesisStream;
170187

171188
forEach(({ShardId: shardId}) => {
172189
const readable = KinesisReadable(
@@ -222,7 +239,7 @@ class ServerlessOfflineKinesis {
222239
this.serverless.cli.log(`Kinesis for ${functionName}:`);
223240

224241
forEach(streamEvent => {
225-
this.createKinesisReadable(functionName, streamEvent);
242+
this.createKinesisReadable(functionName, streamEvent, true); // TMP: retry is not configurable so far
226243
}, streams);
227244

228245
printBlankLine();

0 commit comments

Comments
 (0)