Repository navigation
Scale to zero: stop idle projects, start them on the next visit - #3
Conversation
- Global services gain alios_idle (image tagged with the CLI version, ALIOS_IDLE_TIMEOUT passed through, Docker socket mounted, internal network only). - The base Caddyfile defines an alios_wake snippet that sends 502/503/504 upstream failures to the waker; project, HTTP-only and share vhosts import it in place of the old "Project unavailable" response. - `alios idle [on|off|<duration>]` stores a per-project setting in the alios.project.idle label (recreating the container like `alios env`). - `exec`, `shell`, the implicit command fallthrough and `share` start a sleeping project instead of failing; `list` and `status` show a stopped project as sleeping while alios_idle runs; `stop` says a visit restarts it. - `doctor` checks alios_idle, ALIOS_IDLE_TIMEOUT, vhosts that predate the snippet, and busy-warning lines from the last 24h; `global start` and `restart` point out vhosts that still need `alios cert repair`. - scripts/release.sh builds and pushes the multi-arch alios-idle image. - docs/IDLE.md, README and CLAUDE.md describe it. Verified in sandboxes on the real Docker daemon, without touching the live stack: the watcher stopped a 1m-timeout container on schedule, and a request through Caddy with the generated snippet woke a stopped project in ~280ms and was redirected back to the same URL. Caddy 2.10 adapts the generated Caddyfile with the snippet routing only 502/503/504.
- POST/PUT bodies: the first proxy hop consumes the body, so forwarding the request to the waker failed and Caddy returned an empty 502. The snippet now asks the waker with a bodiless GET; the browser resends the original method and body after the 307. Verified through Caddy 2.10 with 100 KB and chunked POSTs. - `alios cert repair` wrote vhosts importing alios_wake without the base Caddyfile that defines it, so Caddy crash-looped until `global start`. Repair now writes the base config first (services.WriteBaseConfig). - alios_idle was a compose service in GlobalServices, so an unpullable image aborted `global start` and took every project down. It now runs on its own, best-effort, with a warning when it can't start. - `alios pause` stopped projects while the waker still ran, so a request mid-pause could start one again. Pause stops alios_idle first and still resumes it. - An `alios shell` opened as the container started was folded into the process baseline and never counted, so the shell was killed after the timeout. Exec'd processes are now identified directly (not descended from the container's main PID) instead of against a baseline. - Open redirect: a path starting "//" made the waker's Location protocol-relative (https://<share>//evil.com). Leading slashes are collapsed. - ALIOS_IDLE_TIMEOUT from whichever shell started services silently reset the service's timeout. It's now only changed when the variable is set, stored on the container, and status/idle/doctor report the running value. - A container started moments ago by anything (not just the waker) gets a retry instead of "not responding"; a request that arrives while the container is being stopped starts it again once the stop finishes rather than waiting out the timeout; Docker API calls time out after 30s; a failed sample no longer restarts a project's idle timer; the stale-vhost hint counts only project vhost files.
Review round 1 (fresh-context reviewer,
|
- startIdle removed the running alios_idle before pulling the image for the new CLI version, so a failed pull (offline, not yet published) left no waker and every sleeping project served an empty 502. It now pulls first and keeps the previous service on failure. - alios_idle is listed in `alios status` / `global status` again (it left GlobalServices), so a stopped or crashed one is visible. Doctor's services section skips it; its own section covers it. - An open but idle `alios shell` counted as "busy without traffic" and produced busy warnings after 2h. Only CPU use without traffic counts now (regression test fails without the fix). - Docs/comments no longer describe the removed process baseline.
Review round 2The reviewer confirmed all 12 round-1 fixes hold. It checked exec detection against a live container's Fixed in 7ed8c86:
Not verified: whether Docker Desktop's |
Review round 3The reviewer confirmed all four round-2 fixes hold. It checked every There was one P3, fixed in 9d678f3: when the pull fails and the old alios_idle also fails to start, the warning now says so instead of claiming it's running. Leaving this as a draft while the |
A project the watcher stops for being idle is asleep and wakes on the next visit. One stopped by hand now stays stopped: otherwise a polling browser tab, an HMR reconnect or a webhook undoes the stop a second later with no explanation. - The vhost records which: `alios stop` swaps `import alios_wake <name>` for `import alios_stopped <name>`, a new snippet answering 503 "<name> is stopped. Run 'alios start'..." (checked through Caddy 2.10). `alios start` rewrites the vhost back; restart, pause/resume leave it. - `cert repair` keeps stopped projects stopped. - `exec`, `shell` and `share` start an asleep project but refuse a stopped one; `list`/`status` show sleeping vs stopped; doctor reports a stopped project as info, and neither it nor the global-start hint mistakes a stopped vhost for one that predates scale to zero. - The swap matches whole lines (unit-tested), so stopping "sho" can't touch "shop"'s vhost.
- P1: the vhost was marked stopped only after the container was down, so a polling tab hit the waker mid-stop and it started the project again. The marker is now written (and Caddy reloaded) before the stop. - rebuild, php and image start the container but left the marker, so a running project showed as stopped and wouldn't wake after its next idle stop. They clear it now. - A vhost importing a snippet the running base Caddyfile lacks (stack started by an older alios) made the reload fail and left a file that crash-loops Caddy on its next start. stop/start/rebuild/php/image now write the current base Caddyfile first, and SetProjectStopped puts the vhost back if Caddy rejects it. - `cert remove` (updateVhostToHTTP) keeps a stop. - exec/shell/share refuse only a project stopped by hand; if alios_idle is down or the vhosts can't be read they wake it as before. - `alios list` reads the stop markers only when a project is down and alios_idle runs, and on a read error calls nothing asleep. - The stop message no longer promises "stays stopped" after a failed mark; docs list which commands clear and keep the marker.
|
Scale to zero: land #3 on main
Stacked on #2 (it changes the share vhost too). Merge #2 first; this PR then retargets to
main.What
Projects nobody is using are stopped, and the next request to one starts it again.
alios_idle, a new global service (cmd/alios-idle,internal/idle), does both jobs:alios shell. One that stays quiet on all three forALIOS_IDLE_TIMEOUT(default 30m) is stopped.import alios_wake <project>. That snippet sends Caddy's 502/503/504 upstream failures to the waker, which starts the container, waits for port 80, and replies with a 307 to the same URL. The waker is asked with a bodiless GET, and the browser resends a POST's body itself. Concurrent requests share one start. A container that's up and listening but still failing gets an error page instead of a redirect loop, except just after a wake, when a burst of asset requests is expected.alios stopkeeps a project down untilalios start: its vhost importsalios_stoppedinstead ofalios_wake, and visits get a "stopped" page.alios idle [on|off|<duration>]sets a per-project setting, stored in thealios.project.idlelabel.exec,shellandsharestart a sleeping project.listandstatusshow it as "sleeping".doctorchecks the service, and flags vhosts that predate the snippet and projects busy for hours with no traffic.global startwarns and everything else still works.ghcr.io/aliasproject/alios-idle:<version>is a ~9 MB image built from scratch.scripts/release.shbuilds and pushes it for amd64 and arm64.Testing
go test -race ./...covers the idle decision logic, the waker (redirects, the loop guard, concurrent wakes, refusing containers that aren't the project's, timeouts) and the watcher (timeouts, per-project settings, busy warnings).caddy adaptaccepts the generated base Caddyfile and vhosts. Only 502/503/504 route to the waker.Not tested yet
alios global starton a real machine, because the image isn't published, and switching the live stack over stops idle sites.release.sh'sbuildx --pushhas never been run.