From c5bd0e249d0997aa5c70b4d32379f9627f6c69a3 Mon Sep 17 00:00:00 2001 From: Dustin Belliston Date: Tue, 14 May 2019 13:45:13 -0600 Subject: [PATCH 1/7] option to keep running until user sends terminate --- index.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c0fb955c..448f5166 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,10 @@ class ServerlessDynamodbLocal { convertEmptyValues: { shortcut: "e", 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.", - } + }, + leaveOn: { + usage: 'Keep the process running until user sends kill command. This will kill the process the database is running once user sends terminate.' + } } }, noStart: { @@ -254,8 +257,20 @@ class ServerlessDynamodbLocal { } else { this.serverlessLog("Skipping start: DynamoDB Local is not available for stage: " + this.stage); } + + return BbPromise.resolve() + .then(() => options.migrate && this.migrateHandler()) + .then(() => options.seed && this.seedHandler()) + .then(() => { + if (options.leaveOn) { + return BbPromise.resolve() + .then(() => this._listenForTermination()) + .then(() => this.endHandler()); + } + }); } + endHandler() { if (this.shouldExecute() && !this.options.noStart) { this.serverlessLog("DynamoDB - stopping local database"); @@ -286,6 +301,24 @@ class ServerlessDynamodbLocal { }).filter((n) => n); } + _listenForTermination() { + // SIGINT will be usually sent when user presses ctrl+c + const waitForSigInt = new Promise(resolve => { + process.on('SIGINT', () => resolve('SIGINT')); + }); + + // SIGTERM is a default termination signal in many cases, + // for example when "killing" a subprocess spawned in node + // with child_process methods + const waitForSigTerm = new Promise(resolve => { + process.on('SIGTERM', () => resolve('SIGTERM')); + }); + + return Promise.race([waitForSigInt, waitForSigTerm]).then(command => { + this.serverlessLog(`Got ${command} signal. Will stop dynamodb local...`); + }); + } + /** * Gets the table definitions */ From b34cd60b3a691c50d47e7969f71b60ccf05b38e1 Mon Sep 17 00:00:00 2001 From: Dustin Belliston Date: Tue, 14 May 2019 13:59:52 -0600 Subject: [PATCH 2/7] docs on userStop option --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 448f5166..e0b498c5 100644 --- a/index.js +++ b/index.js @@ -89,8 +89,8 @@ class ServerlessDynamodbLocal { shortcut: "e", 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.", }, - leaveOn: { - usage: 'Keep the process running until user sends kill command. This will kill the process the database is running once user sends terminate.' + userStop: { + usage: 'After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates runinng process. When terminate signal received, it will stop the database on the running port.' } } }, @@ -262,7 +262,8 @@ class ServerlessDynamodbLocal { .then(() => options.migrate && this.migrateHandler()) .then(() => options.seed && this.seedHandler()) .then(() => { - if (options.leaveOn) { + if (options.userStop) { + this.serverlessLog("DynamoDB - Database is running. Waiting for user to stop...") return BbPromise.resolve() .then(() => this._listenForTermination()) .then(() => this.endHandler()); From f16dc043c6188c966f1e0aed441133cc67b22445 Mon Sep 17 00:00:00 2001 From: Dustin Belliston Date: Tue, 14 May 2019 14:00:01 -0600 Subject: [PATCH 3/7] docs on userStop option --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bf6a576c..fb3a88d3 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ All CLI options are optional: --migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration. --seed -s After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload. --convertEmptyValues -e 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. +--userStop After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates runinng process. When terminate signal received, it will stop the database on the running port. ``` All the above options can be added to serverless.yml to set default configuration: e.g. @@ -75,6 +76,7 @@ custom: migrate: true seed: true convertEmptyValues: true + userStop: true # Uncomment only if you already have a DynamoDB running locally # noStart: true ``` From 94db8626451a1ae68eecfc0275b36536ba12f6b5 Mon Sep 17 00:00:00 2001 From: Dustin Belliston Date: Tue, 14 May 2019 14:01:37 -0600 Subject: [PATCH 4/7] spelling fix --- README.md | 2 +- index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fb3a88d3..1f241878 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ All CLI options are optional: --migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration. --seed -s After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload. --convertEmptyValues -e 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. ---userStop After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates runinng process. When terminate signal received, it will stop the database on the running port. +--userStop After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port. ``` All the above options can be added to serverless.yml to set default configuration: e.g. diff --git a/index.js b/index.js index e0b498c5..5477f100 100644 --- a/index.js +++ b/index.js @@ -90,7 +90,7 @@ class ServerlessDynamodbLocal { 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.", }, userStop: { - usage: 'After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates runinng process. When terminate signal received, it will stop the database on the running port.' + usage: 'After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port.' } } }, From 29bcc005e5141e9813a28b8619d8b9881d8e7c4e Mon Sep 17 00:00:00 2001 From: Dustin Belliston Date: Tue, 14 May 2019 14:25:02 -0600 Subject: [PATCH 5/7] linted --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 5477f100..10d0be66 100644 --- a/index.js +++ b/index.js @@ -90,7 +90,7 @@ class ServerlessDynamodbLocal { 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.", }, userStop: { - usage: 'After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port.' + usage: "After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port." } } }, @@ -263,7 +263,7 @@ class ServerlessDynamodbLocal { .then(() => options.seed && this.seedHandler()) .then(() => { if (options.userStop) { - this.serverlessLog("DynamoDB - Database is running. Waiting for user to stop...") + this.serverlessLog("DynamoDB - Database is running. Waiting for user to stop..."); return BbPromise.resolve() .then(() => this._listenForTermination()) .then(() => this.endHandler()); @@ -304,18 +304,18 @@ class ServerlessDynamodbLocal { _listenForTermination() { // SIGINT will be usually sent when user presses ctrl+c - const waitForSigInt = new Promise(resolve => { - process.on('SIGINT', () => resolve('SIGINT')); + const waitForSigInt = new Promise((resolve) => { + process.on("SIGINT", () => resolve("SIGINT")); }); // SIGTERM is a default termination signal in many cases, // for example when "killing" a subprocess spawned in node // with child_process methods - const waitForSigTerm = new Promise(resolve => { - process.on('SIGTERM', () => resolve('SIGTERM')); + const waitForSigTerm = new Promise((resolve) => { + process.on("SIGTERM", () => resolve("SIGTERM")); }); - return Promise.race([waitForSigInt, waitForSigTerm]).then(command => { + return Promise.race([waitForSigInt, waitForSigTerm]).then((command) => { this.serverlessLog(`Got ${command} signal. Will stop dynamodb local...`); }); } From 0955bc81be84135031a718a525cdacfda471c116 Mon Sep 17 00:00:00 2001 From: Dustin Belliston Date: Mon, 20 May 2019 09:19:00 -0600 Subject: [PATCH 6/7] named options to pause db once seeding is complete --- README.md | 4 ++-- index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1f241878..cc955278 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ All CLI options are optional: --migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration. --seed -s After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload. --convertEmptyValues -e 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. ---userStop After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port. +--pauseDbAfterSeeding -p After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port. ``` All the above options can be added to serverless.yml to set default configuration: e.g. @@ -76,7 +76,7 @@ custom: migrate: true seed: true convertEmptyValues: true - userStop: true + pauseDbAfterSeeding: true # Uncomment only if you already have a DynamoDB running locally # noStart: true ``` diff --git a/index.js b/index.js index 10d0be66..e78f9022 100644 --- a/index.js +++ b/index.js @@ -89,7 +89,7 @@ class ServerlessDynamodbLocal { shortcut: "e", 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.", }, - userStop: { + pauseDbAfterSeeding: { usage: "After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port." } } @@ -262,7 +262,7 @@ class ServerlessDynamodbLocal { .then(() => options.migrate && this.migrateHandler()) .then(() => options.seed && this.seedHandler()) .then(() => { - if (options.userStop) { + if (options.pauseDbAfterSeeding) { this.serverlessLog("DynamoDB - Database is running. Waiting for user to stop..."); return BbPromise.resolve() .then(() => this._listenForTermination()) From b32daeda7f470284b4c055cbede89ec7323d4682 Mon Sep 17 00:00:00 2001 From: Dustin Belliston Date: Mon, 20 May 2019 09:23:57 -0600 Subject: [PATCH 7/7] changes private funciton indicator --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e78f9022..74691ea6 100644 --- a/index.js +++ b/index.js @@ -265,7 +265,7 @@ class ServerlessDynamodbLocal { if (options.pauseDbAfterSeeding) { this.serverlessLog("DynamoDB - Database is running. Waiting for user to stop..."); return BbPromise.resolve() - .then(() => this._listenForTermination()) + .then(() => this.listenForTermination()) .then(() => this.endHandler()); } }); @@ -302,7 +302,7 @@ class ServerlessDynamodbLocal { }).filter((n) => n); } - _listenForTermination() { + listenForTermination() { // SIGINT will be usually sent when user presses ctrl+c const waitForSigInt = new Promise((resolve) => { process.on("SIGINT", () => resolve("SIGINT"));