feat(serve): add --foreground flag for container/supervisor use (#126)#129
Conversation
pdf-amzn
left a comment
There was a problem hiding this comment.
Looks good. One of us will need to manually try this out before approving but the code is nice and straightforward. I have a few comments, I don't think any are blocking. If you want to update this PR in the meantime, you can. Otherwise, please do a followup PR or cut a followup issue for at least 1, and please cut a followup issue for 3 but I don't think our current test framework is ready for a test like that yet, so I do not recommend you try to solve 3 at this time.
Review notes (AI generated; 1, 4, and 5 are the ones I personally think are noteworthy):
- Logging initialization duplication (medium)
The tracing subscriber setup now has 4 branches (foreground×json, foreground×text, daemon×json, daemon×text) with near-identical code:
rust
if foreground {
if json { /* stderr + json / } else { / stderr + text / }
} else {
if json { / syslog + json / } else { / syslog + text */ }
}
This could be simplified by extracting the writer choice first, then having a single json/text branch. Not a correctness issue, but increases maintenance surface.
- PID file race window in foreground mode (low risk)
In daemon mode, Daemonize::new().pid_file(&pid_file) writes the PID atomically during the fork. In foreground mode, the PID file is written later in serve_inner — after catalog version lookup. If the process crashes between startup and PID write, extenddb stop won't find it. The window is small (milliseconds) and the process would be dead anyway, so this is acceptable.
- No SIGTERM handler test
The PR description shows manual SIGTERM testing, but there's no automated test for graceful shutdown in foreground mode. The existing signal handling code is unchanged, so this is low risk, but a test would be nice for a feature specifically targeting supervisor environments.
- --foreground doesn't suppress the startup banner to stdout
Looking at run(), the banner "extenddb server started (pid X, addr)" is printed to stdout before entering serve_inner. In foreground mode, this mixes stdout (banner) and stderr (tracing logs). Supervisors typically expect all output on one stream. Minor UX issue.
- No --foreground in extenddb.toml config
The flag is CLI-only. For container deployments, an env var (EXTENDDB_FOREGROUND=1) or config file option would be more ergonomic than modifying the command line. However, this is a feature request, not a bug — CLI-only is fine for v1.
|
Thanks for the review @pdf-amzn
|
What
Adds a
--foregroundflag (alias--no-daemon) toextenddb serveso theprocess can run attached to its controlling terminal instead of daemonizing.
When the flag is set:
Daemonize::execute()block and the syslog panic hook are skipped.captures them naturally.
serve_innerrather than bydaemonize), soextenddb statusandextenddb stopkeep working.log_output=stderrinstead oflog_output=syslog.When the flag is not set, behavior is byte-identical to the previous
release: same daemonize path, same syslog logging, same panic hook, same
PID-file lifecycle.
Five unit tests in
crates/bin/src/cmd_serve.rscover the argv contract:default-is-daemon, the flag, the
--no-daemonalias, combination with--config/--port, and rejection of unknown flag spellings.README updates:
--foregroundfor container and supervisor use.supervisors it targets (Docker, Kubernetes, systemd Type=simple, runit, s6).
--foregroundexample line.Why
Closes #126.
extenddb servealways double-forks, which is awkward inside a container orunder a process supervisor that expects PID 1 to stay attached. Operators
running ExtendDB under Docker, Kubernetes,
systemd Type=simple, runit, ors6 currently have to wrap the binary just to undo the daemonization. A first-
class flag is the standard fix and keeps the daemon-mode default intact for
existing users.
Testing done
Static checks
New tests:
End-to-end (PostgreSQL 16 via Homebrew, macOS)
Init:
./extenddb init --config extenddb.toml \ --pg-user extenddb --pg-pass extenddb-local-dev \ --extenddb-user extenddb --extenddb-pass extenddb-local-dev \ --pg-host 127.0.0.1 --pg-port 5432 --bind-addr 127.0.0.1 # Catalog + data DBs created, TLS cert generated, admin user provisioned.Daemon mode (regression check, default behavior):
Foreground mode (new behavior):
While running in foreground, in another shell:
Graceful shutdown via SIGTERM (the supervisor scenario):
Cleanup with
extenddb destroysucceeded.Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Breaking changes
None. The flag defaults to
false, soextenddb servewithout the flag isbehaviorally identical to the previous release: same daemonize path, same
syslog logging, same panic hook, same PID-file location and lifecycle.
By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.