9. Day-2 operations
The nightly sweep
Section titled “The nightly sweep”Teardown runs on PR close. The sweep is what catches the closes that never ran — a cancelled
workflow, a runner that died mid-teardown, a PR closed while the box was down. Iterate the PRs that
should be gone and let verify answer:
sweep.sh:
#!/usr/bin/env bash# Verify every given PR is really gone. Exit 1 if anything leaked.leaked=()for pr in "$@"; do pstack --set PR="$pr" verify -q >/dev/null 2>&1 case $? in 0) echo " pr-$pr clean" ;; # clean, or unverifiable — see below 2) echo " pr-$pr LEAKED"; leaked+=("$pr") ;; *) echo " pr-$pr verify errored" ;; esacdoneif (( ${#leaked[@]} )); then printf 'LEAKED: %s\n' "${leaked[*]}" exit 1fiecho "sweep clean"$ ./sweep.sh $(gh pr list --state closed --limit 100 --json number --jq '.[].number') pr-201 LEAKED pr-202 clean pr-203 LEAKEDLEAKED: 201 203$ echo $?1Once the leaks are dealt with, the same sweep is the proof:
$ ./sweep.sh 201 202 203 pr-201 clean pr-202 clean pr-203 cleansweep clean$ echo $?0--set PR=<n> is the flag that makes a sweep possible: it defines or overrides a spec variable for
one invocation without touching the environment (overrides win over the ambient env). It is also the
right tool for any one-off — re-verifying a single old stack, testing a spec change against a
throwaway number:
$ PR=1 pstack --set PR=42 validatestack: pr-42Remember verify exits 0 for an axis with no assert_gone. A sweep is only as complete as your
assert_gone coverage — run pstack validate in the sweep too and treat new ! warnings as sweep
blind spots.
Finding orphans
Section titled “Finding orphans”The sweep only checks stacks you can enumerate. Orphans are the ones nobody remembers — the PR
number is lost, so verify never gets pointed at them. Ask the host instead:
docker compose ls --all # every compose project on the boxdocker network ls --filter name=_default # one dead <stack>_default per leaked teardowndocker images # per-PR tags compose never removesdocker system df # is the disk actually the problem?Or over the API. GET /api/deployments lists everything ever submitted, cross-referenced against
docker compose ls --all, so you can tell intent from reality:
$ curl -s https://api.preview.example.com/api/deployments{ "deployments": [ { "id": "pr-123", "kind": "isolated", "createdAt": 1785014255903, "updatedAt": 1785014255903, "stack": "pr-123", "busy": false, "running": true } ]}busy— a job holds that stack’s lock. Don’t act on it; attach to the job instead.running— a compose project by that name exists on the host.- Both are
null, neverfalse, when they could not be determined: an unresolved spec has no stack name to look up, and a Docker that did not answer is not the same fact as “nothing is running”. A row whose variables were not supplied on the query string carriesunresolvedwith the reason, instead of failing the whole listing — one broken deployment must not hide the other twenty.
running: true with a closed PR is your orphan list. The reverse — a compose project on the host with
no row here — is a stack submitted before the registry was wiped, or one created by hand; the
docker commands above are the only way to see those.
A cluster of <stack>_default networks with no containers is the signature of the profile bug: a
teardown that passed fewer profiles than the deploy did. pstack down cannot produce it; a
hand-rolled script can.
When verify reports a leak
Section titled “When verify reports a leak” ✗ assert_gone database — LEAKED: resource still present after teardown- Believe it, but confirm it by hand. Run the axis’s probe yourself. You are distinguishing the resource survived from the assert is wrong — a false alarm from a broken probe is common and sends you hunting for nothing.
- Re-run
pstack down.downhooks must be idempotent, so this is safe, and a transient API failure clears on the retry. Most leaks end here. - Still leaking? Read the hook with
-v.pstack down -vprints the exact hook that ran (section 5). Wrong name, wrong region, wrong credentials, or a resource that must be drained before deletion are the usual four. - Clean it up manually, then fix the hook. Manual cleanup makes
verifygreen and hides the bug; the next PR leaks identically. Do both, in that order. - Confirm the fix with the drill. Sabotage the repaired hook and make sure the leak is still caught (section 5). A leak that reappears silently is worse than the first one.
Routine hygiene
Section titled “Routine hygiene”| Cadence | Do |
|---|---|
| every PR | validate in CI; keep ! warnings at zero |
| every teardown | let verify run — don’t reach for --no-verify |
| nightly | the sweep above; alert on exit 1 |
| weekly | docker system df on the host; a growing disk means an axis you haven’t modelled yet |
| per new axis | the section 5 drill before it goes near a shared host |
Node pressure (0.45.0)
Section titled “Node pressure (0.45.0)”A full disk took a host down for days once: images piled up, Traefik’s own log reached 3.6 GB, and Traefik could not restart. pstack now watches every machine and acts before that.
The agent. pstack init runs pstack-agent on every node: a POSIX shell script in a stock
docker:29.9.2-cli image pinned by digest, with the docker socket and the host’s / mounted
read-only. On a compose host it is one container. On a swarm it is a container on this manager
plus a global service on every other node, so a worker that joins later gets one with nothing to
install. Every 30 s it
prints one batch to its own log:
- disk:
dfof the host’s docker data root, not the container’s overlay; - memory:
MemAvailable, which does not count the page cache as used; - load: the 1- and 5-minute load average and the core count;
- per container: its stack and its CPU and memory use.
The control plane reads those logs every 30 s (docker logs --since 90s, and docker service logs
on a swarm), so the agent needs no network path to pstack and no credential. The agent decides
nothing: every threshold is a setting on this host. A host from before 0.45.0 has no agent until
pstack upgrade.
What it reports, on the Stats page of either UI, from GET /api/stats (viewer), or with
pstack api stats get:
{ "v": 1, "at": 1785014255903, "agent": true, "nodes": [ { "node": "x8k2m9qv1n3p4r5s6t7u8w9y0", "hostname": "worker-2", "role": "worker", "at": 1785014250000, "stale": false, "cores": 8, "load1": 1.02, "load5": 1.1, "memTotal": 33399881728, "memAvailable": 4294967296, "diskTotal": 105152176128, "diskFree": 5136490496, "pressure": ["disk"], "prune": { "at": 1785014100000, "freedBytes": 12300000000, "reason": "pressure" }, "history": [{ "at": 1785014250000, "load5": 1.1, "memAvailable": 4294967296, "diskFree": 5136490496 }] } ], "stacks": [{ "stack": "pr-42", "cpuPct": 3.1, "mem": 129394278, "containers": 1, "nodes": ["worker-2"] }], "prune": { "lastAt": 1785014100000, "lastFreed": 12300000000 }}- A node with no complete batch in 2 minutes is
stale: true, andatand every reading arenull. Itshistory(an hour, 120 samples) stays. nodeis the swarm node id,nullon a compose host.roleismanagerorworker,nullon compose.- Everything here is in memory. A restart forgets the history and the last prune.
When a node is in pressure. On its newest fresh batch:
| Kind | In pressure when | Setting (default) | Clears at |
|---|---|---|---|
disk |
used ≥ N % | pressure_disk_pct (90) |
below N − 5 |
memory |
MemAvailable ≤ N % of total |
pressure_mem_pct (10) |
above N + 5 |
cpu |
5-minute load ÷ cores ≥ N | pressure_cpu_load (2.0) |
below N − 0.25 |
0 turns a kind off. The margin keeps a value sitting on the line from flapping. A stale or
missing node is never in pressure: no guard acts on data it does not have.
What pstack does while a node is in pressure:
-
Alerts.
node.pressureon the way in,node.recoveredon the way out (webhook-events.md). A restart raises again what is still true; dedupe onid. -
Prunes a full disk. Unused images of any age on that node, at most once per node every 10 minutes, so a disk full of something else is not pruned in a loop.
node.prunedsays how much it freed. -
Sleeps one stack when memory or cpu is short: the least recently used stack with a container on that node, one per 30 s across the whole host. Eligible means awake, not
kind: shared, no job in flight, and unused forpressure_sleep_idle_min(30) minutes;0turns this off. The stack needs nosleep:block, and it wakes on the next request like any other (Sleep and wake-on-call).stack.sleptcarries the reason,worker-2 memory 6% free ≤ 10%. It does nothing while Traefik’s request counters cannot be read: silence is not idle. -
Refuses deploys and wakes when there is no room. On a swarm, that is when every active node is in pressure, since which node a task lands on is not known in advance. On a compose host, when the host is. The job fails with one step, and the wake page shows it:
{ "axis": "pr-42", "phase": "capacity", "ok": false, "code": 1, "message": "worker-2: disk 94% ≥ 90%", "skipped": false }Down, sleep and verify always run: they are how pressure goes away. Swarm can still place a task on a full node while another has room.
Scheduled prunes. Every 6 hours, on every node, images and build cache older than
image_prune_after_h (168, a week) hours: docker image prune -af --filter until=<N>h and the same
for docker builder prune. 0 turns it off. until is the image’s age, not its last use:
- An image any container uses, a stopped one included, is never removed.
- A sleeping stack’s image can be pruned. Its next wake pulls or builds it again, so that wake is slower.
- Containers and volumes are never pruned. A swarm keeps each service’s last five exited tasks, and those hold their images until newer tasks push them out.
On a swarm a prune is a pstack-prune-<ms> job service, removed when it ends. This manager is
always pruned directly, never by a swarm task.
Log caps. The control stack’s containers cap their json-file logs (Traefik 10m × 2, the rest
50m × 3). With Loki off, every preview service without a logging: key gets 10m × 3. Run
pstack upgrade to apply the control-stack caps to an existing host; previews get theirs on their
next deploy.
Two environment variables:
PSTACK_AGENT_IMAGE(init): another image for the agent, such as a mirror. It is not read back: export it on everypstack initandpstack upgrade, or the agent returns todocker:29.9.2-cli. Cloud-init does not pass it. If the image does not pull,initprints a note and the guards stay off.PSTACK_STATS_TICK_MS(serve): how often the logs are read, 30000 by default.0turns the loop off, and with it every guard and prune.
The five settings are in Runtime settings.