Skip to content

Commit

Permalink
[samp local] make cleanup more resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Aug 22, 2023
1 parent dca517a commit 8cce920
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/commands/local/lib/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ import ini from 'ini';
import { fromSSO } from '@aws-sdk/credential-provider-sso';
import { type } from 'os';

console.log("Cleaning up...");
console.log("Cleaning up...", process.env.SAMP_PROFILE);
let configEnv = 'default';
let functions = undefined;
const cachePath = process.cwd() + "/.lambda-debug";
let conf;
if (fs.existsSync(cachePath)) {
conf = JSON.parse(fs.readFileSync(cachePath, 'utf-8'));
configEnv = conf.configEnv || 'default';
if (conf.functions) {
functions = conf.functions;
}
fs.unlinkSync(cachePath);
} else {
conf = {};
conf.envConfig = JSON.parse(process.env.SAMP_SAMCONFIG);
}

if (fs.existsSync(process.cwd() + "/.samp-out")) {
console.log("Removing .samp-out directory");
fs.rmSync(process.cwd() + "/.samp-out", { recursive: true, force: true });
}
if (!conf || !conf.envConfig) process.exit(0);

const stackName = conf.envConfig.stack_name;
const region = conf.envConfig.region;
const profile = conf.envConfig.profile;
Expand All @@ -47,15 +46,15 @@ do {

const template = JSON.parse(templateResponse.TemplateBody);

functions = functions || Object.keys(template.Resources).filter(key => template.Resources[key].Type === "AWS::Lambda::Function");;

const functions = Object.keys(template.Resources).filter(key => template.Resources[key].Type === "AWS::Lambda::Function");;
console.log("Restoring functions...");
let resourceConflictCount = 0;
const updatePromises = functions.map(async functionName => {
let updated = false;
do {
try {
const func = template.Resources[functionName];
const physicalId = stackResources.find(resource => resource.LogicalResourceId === functionName).PhysicalResourceId;
console.log(`Restoring function: ${functionName}`);

await lambdaClient.send(new UpdateFunctionConfigurationCommand({
FunctionName: physicalId,
Expand All @@ -82,14 +81,18 @@ const updatePromises = functions.map(async functionName => {

await lambdaClient.send(new UpdateFunctionCodeCommand(params));

console.log("Restored function:", functionName);
updated = true;
} catch (error) {
if (error.name === "TooManyRequestsException") {
console.log("Too many requests, sleeping for 1 second");
await new Promise(resolve => setTimeout(resolve, 1000));
} else if (error.name === "ResourceConflictException") {
console.log("Resource conflict, retrying");
resourceConflictCount++;
if (resourceConflictCount < 10) {
console.log("Resource conflict, retrying...");
} else {
console.log("Resource conflict, retrying... Hang in there - this may take a while");
}
await new Promise(resolve => setTimeout(resolve, 1000));
} else {
throw error;
Expand All @@ -101,5 +104,6 @@ const updatePromises = functions.map(async functionName => {

// Wait for all promises to resolve
await Promise.all(updatePromises);
console.log("Finished restoring functions");


2 changes: 2 additions & 0 deletions src/commands/local/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ async function run(cmd) {
await warn();

if (cmd.forceRestore) {
const samConfig = samConfigParser.parse();
process.env.SAMP_SAMCONFIG = JSON.stringify(samConfig); // pass it as an env var since it could be run in a separate process
await runner.stop();
return;
}
Expand Down

0 comments on commit 8cce920

Please sign in to comment.