Skip to content

feat(ttl-reaper): graceful pre-stop flush via SSM before terminate (#187) - #190

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/187-reaper-graceful-prestop
Jun 16, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/187-reaper-graceful-prestop

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Implements #187 (follow-up to #184). The out-of-band ttl-reaper called TerminateInstances directly — so whenever it was the one terminating (spored dead/wedged), the instance's --pre-stop hook never ran and the user's flush was skipped. Harmless today (reaper is dry-run) but a data-loss path once it runs in enforce mode.

Change — opt-in graceful tier (REAPER_GRACEFUL=true)

Before the hard terminate, if the candidate is running, has a spawn:pre-stop tag, and the account exposes an SSM client:

  • run the hook via SSM AWS-RunShellScript, as spawn:local-username (su - <user> -c, mirroring --pre-stop runs as root, should run as the instance user #63),
  • bounded by min(spawn:pre-stop-timeout, REAPER_GRACEFUL_MAX_WAIT) (default cap 2m — it's a backstop, not a babysitter),
  • poll to a terminal status, then terminate regardless of the outcome.

Strictly best-effort / fail-open: every error (not SSM-managed, command failed, timed out, agent missing) is logged and swallowed; the terminate always runs. The hard-deadline guarantee (#72) is never weakened. Off by default.

Mechanics

  • candidate carries running / preStop / preStopTimeout / localUsername from tags (populated in evaluate).
  • account gains ssmFor alongside ec2For across all three builders (scan-self, cross-account assume-role, fallback-self).
  • reaper's go.mod adds aws-sdk-go-v2/service/ssm.
  • shellQuote escapes the su -c argument.

Tests

evaluate populates the graceful fields; a stopped instance isn't marked running (graceful skips it — SSM RunCommand needs a running target); shellQuote escaping. Existing reaper tests still green after the evaluate refactor. go build/vet/gofmt clean.

Note: enforce-mode rollout + the cross-account SSM permissions the graceful tier needs (ssm:SendCommand, ssm:GetCommandInvocation) are deploy-side and out of scope here; this is off by default (REAPER_GRACEFUL unset).

Refs #187, #184, #63.

)

The out-of-band reaper called TerminateInstances directly, so whenever it was
the one terminating (spored dead/wedged), the instance's --pre-stop hook never
ran and the user's data flush was skipped — a data-loss path once the reaper
runs in enforce mode.

Add an opt-in graceful tier (REAPER_GRACEFUL=true): before the hard kill, if the
candidate is running, has a spawn:pre-stop tag, and the account exposes an SSM
client, run the hook via SSM AWS-RunShellScript as spawn:local-username (#63),
bounded by min(pre-stop-timeout, REAPER_GRACEFUL_MAX_WAIT [default 2m]). Poll to
a terminal status, then terminate REGARDLESS of the outcome — strictly
best-effort, swallows all errors, never blocks or skips the terminate, so the
hard-deadline guarantee (#72) is preserved.

- candidate carries running/preStop/preStopTimeout/localUsername from tags.
- account gains ssmFor (parallel to ec2For) across all three builders.
- reaper.go.mod: add aws-sdk-go-v2/service/ssm.

Tests: evaluate populates the graceful fields; stopped instances aren't marked
running (graceful skips them); shellQuote escapes the su -c argument.

Refs #187
@scttfrdmn
scttfrdmn merged commit 362fa64 into main Jun 16, 2026
6 checks passed
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant