|
1 | 1 | const isEmpty = require('lodash.isempty');
|
2 | 2 | const { retrieveRestApiId } = require('./restApiId');
|
3 | 3 | const MAX_PATCH_OPERATIONS_PER_STAGE_UPDATE = 80;
|
| 4 | +const BASE_RETRY_DELAY_MS = 500; |
4 | 5 |
|
5 | 6 | String.prototype.replaceAll = function (search, replacement) {
|
6 | 7 | let target = this;
|
@@ -194,38 +195,48 @@ const updateStageFor = async (serverless, params, stage, region) => {
|
194 | 195 | paramsInChunks.push(params);
|
195 | 196 | }
|
196 | 197 |
|
197 |
| - for (let index in paramsInChunks) { |
198 |
| - serverless.cli.log(`[serverless-api-gateway-caching] Updating API Gateway cache settings (${parseInt(index) + 1} of ${paramsInChunks.length}).`); |
199 |
| - await applyUpdateStageForChunk(paramsInChunks[index], serverless, stage, region); |
| 198 | + for (const [index, chunk] of paramsInChunks.entries()) { |
| 199 | + serverless.cli.log(`[serverless-api-gateway-caching] Updating API Gateway cache settings (${index + 1} of ${paramsInChunks.length}).`); |
| 200 | + await applyUpdateStageForChunk(chunk, serverless, stage, region); |
200 | 201 | }
|
201 | 202 |
|
202 | 203 | serverless.cli.log(`[serverless-api-gateway-caching] Done updating API Gateway cache settings.`);
|
203 | 204 | }
|
204 | 205 |
|
205 | 206 | const applyUpdateStageForChunk = async (chunk, serverless, stage, region) => {
|
206 | 207 | const maxRetries = 10;
|
207 |
| - const baseDelay = 500; |
| 208 | + const baseDelay = BASE_RETRY_DELAY_MS; |
208 | 209 | let attempt = 0;
|
209 | 210 |
|
210 |
| - while (attempt <= maxRetries) { |
| 211 | + while (attempt < maxRetries) { |
211 | 212 | try {
|
212 | 213 | serverless.cli.log(`[serverless-api-gateway-caching] Updating API Gateway cache settings. Attempt ${attempt + 1}.`);
|
213 | 214 | await serverless.providers.aws.request('APIGateway', 'updateStage', chunk, stage, region);
|
214 |
| - break; |
| 215 | + return; |
215 | 216 | } catch (error) {
|
| 217 | + // Check for specific error code first, fallback to message check |
216 | 218 | if (
|
217 |
| - attempt < maxRetries && |
218 |
| - error.message.includes('A previous change is still in progress') |
| 219 | + (error.code === 'ConflictException' || error.message.includes('A previous change is still in progress')) |
219 | 220 | ) {
|
220 | 221 | attempt++;
|
221 |
| - const delay = baseDelay * 2 ** (attempt - 1); |
| 222 | + if (attempt >= maxRetries) { |
| 223 | + serverless.cli.log(`[serverless-api-gateway-caching] Maximum retries (${maxRetries}) reached. Failed to update API Gateway cache settings.`); |
| 224 | + // Log the full error for better debugging before throwing |
| 225 | + console.error('[serverless-api-gateway-caching] Final error details:', error); |
| 226 | + throw new Error(`Failed to update API Gateway cache settings after ${maxRetries} retries: ${error.message}`); |
| 227 | + } |
| 228 | + const delay = baseDelay * 2 ** attempt; |
222 | 229 | serverless.cli.log(`[serverless-api-gateway-caching] Retrying (${attempt}/${maxRetries}) after ${delay}ms due to error: ${error.message}`);
|
223 | 230 | await new Promise((resolve) => setTimeout(resolve, delay));
|
224 | 231 | } else {
|
225 |
| - throw new Error(`Failed to update API Gateway cache settings after ${attempt} retries: ${error.message}`); |
| 232 | + console.error('[serverless-api-gateway-caching] Non-retryable error during update:', error); |
| 233 | + // Throw immediately for non-retryable errors or if string/code doesn't match |
| 234 | + throw new Error(`Failed to update API Gateway cache settings: ${error.message}`); |
226 | 235 | }
|
227 | 236 | }
|
228 | 237 | }
|
| 238 | + // This part should ideally not be reached if the loop condition is < maxRetries and success returns early. |
| 239 | + // Removing the final safeguard as it is redundant. |
229 | 240 | }
|
230 | 241 |
|
231 | 242 | const updateStageCacheSettings = async (settings, serverless) => {
|
@@ -266,4 +277,7 @@ const updateStageCacheSettings = async (settings, serverless) => {
|
266 | 277 | await updateStageFor(serverless, params, settings.stage, settings.region);
|
267 | 278 | }
|
268 | 279 |
|
269 |
| -module.exports = updateStageCacheSettings; |
| 280 | +module.exports = { |
| 281 | + updateStageCacheSettings, |
| 282 | + applyUpdateStageForChunk |
| 283 | +} |
0 commit comments