Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to stop the database process #223

Open
wants to merge 7 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
--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.
Expand All @@ -75,6 +76,7 @@ custom:
migrate: true
seed: true
convertEmptyValues: true
pauseDbAfterSeeding: true
# Uncomment only if you already have a DynamoDB running locally
# noStart: true
```
Expand Down
36 changes: 35 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
}
},
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."
}
}
},
noStart: {
Expand Down Expand Up @@ -254,8 +257,21 @@ 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.pauseDbAfterSeeding) {
this.serverlessLog("DynamoDB - Database is running. Waiting for user to stop...");
return BbPromise.resolve()
.then(() => this.listenForTermination())
.then(() => this.endHandler());
}
});
}


endHandler() {
if (this.shouldExecute() && !this.options.noStart) {
this.serverlessLog("DynamoDB - stopping local database");
Expand Down Expand Up @@ -286,6 +302,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
*/
Expand Down