Skip to content

Commit ac8cada

Browse files
committed
option to keep running until user sends terminate
1 parent 8278a7e commit ac8cada

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

index.js

Lines changed: 33 additions & 3 deletions
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: {
@@ -225,11 +228,20 @@ class ServerlessDynamodbLocal {
225228
if (!options.noStart) {
226229
dynamodbLocal.start(options);
227230
}
231+
228232
return BbPromise.resolve()
229-
.then(() => options.migrate && this.migrateHandler())
230-
.then(() => options.seed && this.seedHandler());
233+
.then(() => options.migrate && this.migrateHandler())
234+
.then(() => options.seed && this.seedHandler())
235+
.then(() => {
236+
if (options.leaveOn) {
237+
return BbPromise.resolve()
238+
.then(() => this._listenForTermination())
239+
.then(() => this.endHandler());
240+
}
241+
});
231242
}
232243

244+
233245
endHandler() {
234246
if (!this.options.noStart) {
235247
this.serverlessLog("DynamoDB - stopping local database");
@@ -258,6 +270,24 @@ class ServerlessDynamodbLocal {
258270
}).filter((n) => n);
259271
}
260272

273+
_listenForTermination() {
274+
// SIGINT will be usually sent when user presses ctrl+c
275+
const waitForSigInt = new Promise(resolve => {
276+
process.on('SIGINT', () => resolve('SIGINT'));
277+
});
278+
279+
// SIGTERM is a default termination signal in many cases,
280+
// for example when "killing" a subprocess spawned in node
281+
// with child_process methods
282+
const waitForSigTerm = new Promise(resolve => {
283+
process.on('SIGTERM', () => resolve('SIGTERM'));
284+
});
285+
286+
return Promise.race([waitForSigInt, waitForSigTerm]).then(command => {
287+
this.serverlessLog(`Got ${command} signal. Will stop dynamodb local...`);
288+
});
289+
}
290+
261291
/**
262292
* Gets the table definitions
263293
*/

0 commit comments

Comments
 (0)