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

chore: add retry to EnvController waitFor requests #1632

Merged
merged 2 commits into from
Nov 4, 2020
Merged
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
62 changes: 44 additions & 18 deletions cf-custom-resources/lib/env-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const aws = require("aws-sdk");
// These are used for test purposes only
let defaultResponseURL;

const updateStackWaiter = {
delay: 30,
maxAttempts: 29,
};

/**
* Upload a CloudFormation response object to S3.
*
Expand Down Expand Up @@ -136,6 +141,7 @@ const controlEnv = async function (
await cfn
.waitFor("stackUpdateComplete", {
StackName: stackName,
$waiter: updateStackWaiter,
})
.promise();
continue;
Expand All @@ -144,6 +150,7 @@ const controlEnv = async function (
await cfn
.waitFor("stackUpdateComplete", {
StackName: stackName,
$waiter: updateStackWaiter,
})
.promise();
describeStackResp = await cfn
Expand All @@ -169,30 +176,39 @@ exports.handler = async function (event, context) {
try {
switch (event.RequestType) {
case "Create":
responseData = await controlEnv(
"Create",
props.EnvStack,
props.Workload,
props.Parameters
);
responseData = await Promise.race([
exports.deadlineExpired(),
controlEnv(
"Create",
props.EnvStack,
props.Workload,
props.Parameters
),
]);
physicalResourceId = `envcontoller/${props.EnvStack}/${props.Workload}`;
break;
case "Update":
responseData = await controlEnv(
"Update",
props.EnvStack,
props.Workload,
props.Parameters
);
responseData = await Promise.race([
exports.deadlineExpired(),
controlEnv(
"Update",
props.EnvStack,
props.Workload,
props.Parameters
),
]);
physicalResourceId = event.PhysicalResourceId;
break;
case "Delete":
await controlEnv(
"Delete",
props.EnvStack,
props.Workload,
props.Parameters
);
responseData = await Promise.race([
exports.deadlineExpired(),
controlEnv(
"Delete",
props.EnvStack,
props.Workload,
props.Parameters
),
]);
physicalResourceId = event.PhysicalResourceId;
break;
default:
Expand Down Expand Up @@ -253,6 +269,16 @@ const updateParameter = function (requestType, workload, paramValue) {
return [updatedParamValue, updatedParamValue !== paramValue];
};

exports.deadlineExpired = function () {
return new Promise(function (resolve, reject) {
setTimeout(
reject,
14 * 60 * 1000 + 30 * 1000 /* 14.5 minutes*/,
new Error("Lambda took longer than 14.5 minutes to update environment")
);
});
};

/**
* @private
*/
Expand Down
5 changes: 4 additions & 1 deletion cf-custom-resources/test/env-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ describe("Env Controller Handler", () => {

beforeEach(() => {
EnvController.withDefaultResponseURL(ResponseURL);
EnvController.deadlineExpired = function () {
return new Promise(function (resolve, reject) {});
};
// Prevent logging.
console.log = function () {};
});
Expand Down Expand Up @@ -208,7 +211,7 @@ describe("Env Controller Handler", () => {
{
StackName: "mockEnvStack",
Parameters: testParams,
Outputs: []
Outputs: [],
},
],
});
Expand Down