Skip to content

Commit c5bd0e2

Browse files
committed
option to keep running until user sends terminate
1 parent 5e53d76 commit c5bd0e2

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ class ServerlessDynamodbLocal {
8888
convertEmptyValues: {
8989
shortcut: "e",
9090
usage: "Set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB.",
91-
}
91+
},
92+
leaveOn: {
93+
usage: 'Keep the process running until user sends kill command. This will kill the process the database is running once user sends terminate.'
94+
}
9295
}
9396
},
9497
noStart: {
@@ -254,8 +257,20 @@ class ServerlessDynamodbLocal {
254257
} else {
255258
this.serverlessLog("Skipping start: DynamoDB Local is not available for stage: " + this.stage);
256259
}
260+
261+
return BbPromise.resolve()
262+
.then(() => options.migrate && this.migrateHandler())
263+
.then(() => options.seed && this.seedHandler())
264+
.then(() => {
265+
if (options.leaveOn) {
266+
return BbPromise.resolve()
267+
.then(() => this._listenForTermination())
268+
.then(() => this.endHandler());
269+
}
270+
});
257271
}
258272

273+
259274
endHandler() {
260275
if (this.shouldExecute() && !this.options.noStart) {
261276
this.serverlessLog("DynamoDB - stopping local database");
@@ -286,6 +301,24 @@ class ServerlessDynamodbLocal {
286301
}).filter((n) => n);
287302
}
288303

304+
_listenForTermination() {
305+
// SIGINT will be usually sent when user presses ctrl+c
306+
const waitForSigInt = new Promise(resolve => {
307+
process.on('SIGINT', () => resolve('SIGINT'));
308+
});
309+
310+
// SIGTERM is a default termination signal in many cases,
311+
// for example when "killing" a subprocess spawned in node
312+
// with child_process methods
313+
const waitForSigTerm = new Promise(resolve => {
314+
process.on('SIGTERM', () => resolve('SIGTERM'));
315+
});
316+
317+
return Promise.race([waitForSigInt, waitForSigTerm]).then(command => {
318+
this.serverlessLog(`Got ${command} signal. Will stop dynamodb local...`);
319+
});
320+
}
321+
289322
/**
290323
* Gets the table definitions
291324
*/

0 commit comments

Comments
 (0)