Skip to content

Commit b23cfd3

Browse files
authored
feat(db-proxy): test connection before deploying (webiny#707)
1 parent 5ca4808 commit b23cfd3

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

components/serverless-db-proxy/serverless.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
const { Component } = require("@serverless/core");
22
const get = require("lodash.get");
3+
const { MongoClient } = require("mongodb");
34

45
class DbProxyComponent extends Component {
56
async default(inputs = {}) {
6-
const { env, region, concurrencyLimit, timeout } = inputs;
7+
const { env, region, concurrencyLimit, timeout, testConnectionBeforeDeploy } = inputs;
8+
9+
if (testConnectionBeforeDeploy === true) {
10+
try {
11+
const connection = await MongoClient.connect(env.MONGODB_SERVER, {
12+
useNewUrlParser: true,
13+
useUnifiedTopology: true,
14+
connectTimeoutMS: 5000,
15+
socketTimeoutMS: 5000,
16+
serverSelectionTimeoutMS: 5000
17+
});
18+
19+
// Let's immediately close the connection so we don't end up with a zombie connection.
20+
await connection.close();
21+
} catch (e) {
22+
throw new Error(
23+
`Could not connect to the MongoDB server, make sure the connection string is correct and that the database server allows outside connections. Check https://docs.webiny.com/docs/get-started/quick-start#3-setup-database-connection for more information.`
24+
);
25+
}
26+
}
727

828
const name =
9-
get(this.state, "output.name") ||
10-
this.context.instance.getResourceName(inputs.name);
29+
get(this.state, "output.name") || this.context.instance.getResourceName(inputs.name);
1130

1231
const proxyLambda = await this.load("@webiny/serverless-function");
1332
const proxyLambdaOutput = await proxyLambda({

examples/api/serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ gateway:
4343
dbProxy:
4444
component: "@webiny/serverless-db-proxy/dist"
4545
inputs:
46+
testConnectionBeforeDeploy: true
4647
region: ${vars.region}
4748
concurrencyLimit: 15
4849
timeout: 30

examples/apps/serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ vars:
99
dbProxy:
1010
component: "@webiny/serverless-db-proxy/dist"
1111
inputs:
12+
testConnectionBeforeDeploy: true
1213
region: ${vars.region}
1314
concurrencyLimit: 15
1415
timeout: 30

packages/cli/create/template/api/serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ gateway:
4242
dbProxy:
4343
component: "@webiny/serverless-db-proxy"
4444
inputs:
45+
testConnectionBeforeDeploy: true
4546
region: ${vars.region}
4647
concurrencyLimit: 15
4748
timeout: 30

packages/cli/create/template/apps/serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ vars:
99
dbProxy:
1010
component: "@webiny/serverless-db-proxy"
1111
inputs:
12+
testConnectionBeforeDeploy: true
1213
region: ${vars.region}
1314
concurrencyLimit: 15
1415
timeout: 30

0 commit comments

Comments
 (0)