Skip to content

Commit

Permalink
chore: add retry to EnvController waitFor requests (#1632)
Browse files Browse the repository at this point in the history
<!-- Provide summary of changes -->
Address this [comment](#1508 (comment)) left in #1508. Add retry to EnvController when we call `cfn.waitFor()`, so that it won't immediately error out when the request gets throttled.
<!-- Issue number, if available. E.g. "Fixes #31", "Addresses #42, 77" -->

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
  • Loading branch information
iamhopaul123 authored Nov 4, 2020
1 parent 88575b8 commit 21d81a8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
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

0 comments on commit 21d81a8

Please sign in to comment.