Skip to content

Commit b373158

Browse files
committed
ci(cleanup): count the nested retain's deferred roots apart from the serial sweep's
The Amplify step now deletes one sandbox at a time (#1255) and counts the sandboxes it defers to the next run in DEFERRED. This PR counted the roots it defers after starting a nested retain in a variable of the same name, so both summary lines printed the sum. It now has its own counter, NESTED_DEFERRED. The nested retain stays behind the one-at-a-time gate, so it never starts while an earlier sandbox's delete may still be running, and its nested delete is not awaited: the model stack holds no API Gateway resource, so it doesn't compete for the REST API delete quota the wait protects. A comment in the step says so. Tests: a stuck and a normal sandbox in one run (the stuck root is deferred on its own line, the normal one deleted and awaited), and no nested delete once the wait budget is spent or while an earlier delete is still running. The step tests use the step's poll override, so they take seconds, not minutes.
1 parent dcf2340 commit b373158

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

‎.github/workflows/cleanup-stacks.yml‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ jobs:
408408
# at 40 min and defer the remainder to the next scheduled run.
409409
DEADLINE=$((SECONDS + 2400)) # 40 minutes
410410
# Of that, at most 15 minutes waiting on deletes (see the delete loop).
411-
# The wait settings are overridable for scripts/ci/amplify-sweep.test.mjs.
411+
# The wait settings are overridable for the step tests
412+
# (scripts/ci/amplify-sweep.test.mjs, amplify-stuck-delete.test.mjs).
412413
WAIT_DEADLINE=$((SECONDS + ${AMPLIFY_SWEEP_WAIT_BUDGET_SECONDS:-900}))
413414
PER_STACK_WAIT=${AMPLIFY_SWEEP_STACK_WAIT_SECONDS:-360}
414415
POLL=${AMPLIFY_SWEEP_POLL_SECONDS:-15}
@@ -421,7 +422,7 @@ jobs:
421422
DEFERRED=0
422423
BLOCKED=""
423424
TRIGGER_FAILED=0
424-
DEFERRED=0
425+
NESTED_DEFERRED=0
425426
SCRIPT_FAILED=0
426427
RETAINED_FILE=$(mktemp)
427428
@@ -564,6 +565,14 @@ jobs:
564565
# for deletion by hand, on this run and every later one (--leftovers
565566
# below). The script runs from the job's checkout, which includes
566567
# scripts/ci on every leg.
568+
#
569+
# It runs behind the one-at-a-time gate above, so it never starts while
570+
# an earlier root delete may still be running. Its own nested delete is
571+
# not awaited (so it can overlap a later root delete, or another nested
572+
# one): the model stack holds the AppSync resolvers, IAM roles and the
573+
# table, but no API Gateway resource, so it doesn't compete for the REST
574+
# API delete quota the wait protects. The root, whose `blocks` nested
575+
# stack holds the REST API, is deleted (and awaited) on the next run.
567576
echo "🧹 Emptying nested buckets for $STACK"
568577
_VISITED_STACKS=""
569578
empty_stack_buckets "$STACK"
@@ -574,7 +583,7 @@ jobs:
574583
if [ "$RC" -eq 3 ]; then
575584
# Not deleting the root now: its delete would race the nested one.
576585
echo "Nested stack delete started for $STACK; root delete deferred to the next run"
577-
DEFERRED=$((DEFERRED + 1))
586+
NESTED_DEFERRED=$((NESTED_DEFERRED + 1))
578587
continue
579588
elif [ "$RC" -ne 0 ]; then
580589
echo "::warning::amplify-stuck-delete.sh exited $RC for $STACK; plain retry only"
@@ -630,7 +639,7 @@ jobs:
630639
echo "- Deleted (confirmed): $DELETED" | tee -a "$GITHUB_STEP_SUMMARY"
631640
echo "- Deferred to the next run: $DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
632641
echo "- Delete-stack call failed: $TRIGGER_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
633-
echo "- Root delete deferred (nested stack delete started): $DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
642+
echo "- Root delete deferred (nested stack delete started): $NESTED_DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
634643
echo "- Nested retain script failed: $SCRIPT_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
635644
echo "- Still DELETE_FAILED: $STILL_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
636645
# What amplify-stuck-delete.sh retained, this run or earlier (with the
@@ -651,7 +660,7 @@ jobs:
651660
echo "" | tee -a "$GITHUB_STEP_SUMMARY"
652661
echo "_Each delete is awaited (bounded) before the next; stacks that don't finish (commonly a throttled REST API delete, or the Amplify managed DynamoDB table) are retried on the next scheduled run._" | tee -a "$GITHUB_STEP_SUMMARY"
653662
if [ "$STILL_FAILED" != unknown ] && [ "$STILL_FAILED" -gt 0 ]; then
654-
echo "::warning::$STILL_FAILED Amplify sandbox stack(s) still in DELETE_FAILED after cleanup, nested ones included ($DEFERRED root(s) deferred after a nested retain); AppSync API quota may be at risk"
663+
echo "::warning::$STILL_FAILED Amplify sandbox stack(s) still in DELETE_FAILED after cleanup, nested ones included ($NESTED_DEFERRED root(s) deferred after a nested retain); AppSync API quota may be at risk"
655664
fi
656665
657666
- name: Cleanup orphaned API Gateway REST APIs

‎scripts/ci/amplify-stuck-delete.test.mjs‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,17 @@ function stepScript() {
243243
* Runs the step against `files`, with the root listed, tagged and `status`.
244244
* `cwd` replaces the repo root the step runs scripts/ci/ from.
245245
*/
246-
function runStep(files, { status = 'DELETE_FAILED', created = '2026-10-19T14:48:22.894Z', cwd } = {}) {
246+
function runStep(files, { status = 'DELETE_FAILED', created = '2026-10-19T14:48:22.894Z', cwd, env = {} } = {}) {
247247
const dir = fixture({
248248
'stacks.txt': `${ROOT}\n`,
249249
[`info-${key(ROOT)}.txt`]: `sandbox\t${status}\t${created}\n`,
250250
...files,
251251
});
252252
const file = join(dir, 'step.sh');
253253
writeFileSync(file, stepScript());
254-
return run(dir, [file], { AMPLIFY_RETAINED_FILE: '' }, cwd);
254+
// The step waits for each plain delete (polling describe-stacks); no real
255+
// sleeps in tests.
256+
return run(dir, [file], { AMPLIFY_RETAINED_FILE: '', AMPLIFY_SWEEP_POLL_SECONDS: '0', ...env }, cwd);
255257
}
256258

257259
const NO_ACCOUNT = /123456789012/;
@@ -807,6 +809,52 @@ describe(`cleanup-stacks.yml "${STEP}" step`, { skip: !GNU_DATE && 'needs GNU da
807809
assert.doesNotMatch(r.summary, NO_ACCOUNT);
808810
});
809811

812+
// The step deletes one sandbox at a time and defers the rest once its wait
813+
// budget is spent or a delete is still running. The nested retain runs behind
814+
// that gate, and its deferred roots are counted on their own line.
815+
const NORMAL = 'amplify-amplifygen2-amp716639251-sandbox-1111111111';
816+
const two = (over = {}) =>
817+
sandbox({
818+
'stacks.txt': `${ROOT}\n${NORMAL}\n`,
819+
[`info-${key(NORMAL)}.txt`]: 'sandbox\tROLLBACK_COMPLETE\t2026-10-19T14:48:22.894Z\n',
820+
...over,
821+
});
822+
823+
it('defers a stuck root on its own count, then deletes a normal sandbox and waits for it', () => {
824+
const r = runStep(two());
825+
assert.equal(r.status, 0, r.stdout);
826+
assert.deepEqual(r.deletes, [RETAIN_TODO, `cloudformation delete-stack --stack-name ${NORMAL}`]);
827+
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 1$/m);
828+
assert.match(r.summary, /^- Deferred to the next run: 0$/m);
829+
assert.match(r.summary, /^- Deleted \(confirmed\): 1$/m);
830+
});
831+
832+
it('starts no nested delete once the wait budget is spent', () => {
833+
const r = runStep(two(), { env: { AMPLIFY_SWEEP_WAIT_BUDGET_SECONDS: '0' } });
834+
assert.equal(r.status, 0, r.stdout);
835+
assert.deepEqual(r.deletes, []);
836+
assert.match(r.summary, /^- Deferred to the next run: 2$/m);
837+
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 0$/m);
838+
});
839+
840+
it('starts no nested delete while an earlier sandbox is still deleting', () => {
841+
// The normal sandbox comes first, and every poll in its 1-second wait
842+
// reads DELETE_IN_PROGRESS.
843+
const r = runStep(
844+
two({
845+
'stacks.txt': `${NORMAL}\n${ROOT}\n`,
846+
[`status-${key(NORMAL)}.txt`]: 'DELETE_IN_PROGRESS\n',
847+
}),
848+
{ env: { AMPLIFY_SWEEP_STACK_WAIT_SECONDS: '1', AMPLIFY_SWEEP_POLL_SECONDS: '0.2' } },
849+
);
850+
assert.equal(r.status, 0, r.stdout);
851+
assert.match(r.calls, new RegExp(`describe-stacks --stack-name ${NORMAL} --query Stacks\\[0\\]\\.StackStatus`));
852+
assert.match(r.stdout, new RegExp(`Still deleting: ${NORMAL}`));
853+
assert.deepEqual(r.deletes, [`cloudformation delete-stack --stack-name ${NORMAL}`]);
854+
assert.match(r.summary, /^- Deferred to the next run: 1$/m);
855+
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 0$/m);
856+
});
857+
810858
it('only retries plainly a ROLLBACK_FAILED sandbox (the retain needs DELETE_FAILED)', () => {
811859
const r = runStep(sandbox(), { status: 'ROLLBACK_FAILED' });
812860
assert.equal(r.status, 0, r.stdout);

0 commit comments

Comments
 (0)