Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ and listed with their assets on the
uses a skipped container's address is an error in the profile's check. A
lane on a node does not create what its profile skips. A file with `skip`
wants `requires = "0.21.0"`.
- **A service's `stop` command** (ADR-0059, #245): `[services.<name>]
stop = "docker rm -f api{lane_suffix}-{port}"` ends what `run` started
outside its process tree, a container of a `docker run` service. It
runs whenever an instance ends (a stop, a replacement, a failed start,
one that ended by itself), before the graceful signal, with the
instance's placeholders, at most 30 s; after a crash the next start runs
the ones recorded. `docs/stack-file.md` describes a container as a
service (`--init`, `--rm`, a name per instance). A file with `stop` says
`requires = "0.21.0"`.

## [0.20.0] - 2026-10-23

Expand Down
4 changes: 4 additions & 0 deletions crates/devstack-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub const ANNOUNCED_SINCE: &str = "0.20.0";
/// file that sets it wants `requires` at least this.
pub const SKIP_SINCE: &str = "0.21.0";

/// The first devstack that knows `[services.<name>] stop` (ADR-0059): a
/// file that sets it wants `requires` at least this.
pub const STOP_SINCE: &str = "0.21.0";

/// `"1.2.3"`, `"v1.2"`, `"1"` as numbers; `None` when malformed. A
/// pre-release or build suffix (`1.2.3-rc1`, `1.2.3+abc`) is ignored here;
/// [`version_order`] takes it into account.
Expand Down
12 changes: 12 additions & 0 deletions crates/devstack-config/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub struct ServiceProfile {
pub build: Option<String>,
pub output: Option<String>,
pub run: Option<String>,
/// What ends what `run` started outside its process tree (ADR-0059).
pub stop: Option<String>,
pub spa: Option<bool>,
pub cwd: Option<String>,
pub health: Option<Health>,
Expand Down Expand Up @@ -256,6 +258,7 @@ impl Stack {
// gives itself is set below.
if k == Kind::Static {
svc.run = String::new();
svc.stop = None;
}
if k != Kind::Devserver {
svc.rebuild = None;
Expand Down Expand Up @@ -288,6 +291,9 @@ impl Stack {
if let Some(r) = &o.run {
svc.run = r.clone();
}
if o.stop.is_some() {
svc.stop = o.stop.clone();
}
if o.spa.is_some() {
svc.spa = o.spa;
}
Expand Down Expand Up @@ -838,6 +844,12 @@ pub struct Service {
/// serves the artifact itself).
#[serde(default)]
pub run: String,
/// Command run whenever an instance ends, before the graceful signal
/// (ADR-0059): for what `run` started outside its process tree, a
/// container (`docker rm -f api{lane_suffix}-{port}`). The instance's
/// placeholders, as `run`; at most 30 s; a failure does not hold up the
/// stop.
pub stop: Option<String>,
/// `kind = "static"`: answer paths without a file extension with
/// `index.html` (client-side routes of a single-page app). Default `true`.
pub spa: Option<bool>,
Expand Down
52 changes: 50 additions & 2 deletions crates/devstack-config/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,19 @@ pub fn validate(stack: &Stack) -> Vec<Issue> {
"missing `run`: the command that starts the service",
);
}
if let Some(stop) = &svc.stop {
if svc.kind == Kind::Static {
c.err(
format!("{p}.stop"),
"kind `static` has no `stop`: devstack serves the built files itself, nothing runs",
);
} else if stop.trim().is_empty() {
c.err(
format!("{p}.stop"),
"is empty: leave it out, or give the command that ends what `run` started (ADR-0059)",
);
}
}
if svc.spa.is_some() && svc.kind != Kind::Static {
c.err(format!("{p}.spa"), "`spa` belongs to `kind = \"static\"`");
}
Expand Down Expand Up @@ -797,6 +810,9 @@ pub fn validate(stack: &Stack) -> Vec<Issue> {
artifact: svc.build.is_some(),
};
c.text(&format!("{p}.run"), &svc.run, scope);
if let Some(stop) = &svc.stop {
c.text(&format!("{p}.stop"), stop, scope);
}
for (k, field) in [("build", &svc.build), ("output", &svc.output)] {
if let Some(v) = field {
c.text(&format!("{p}.{k}"), v, Scope::Build);
Expand Down Expand Up @@ -995,8 +1011,9 @@ pub fn validate(stack: &Stack) -> Vec<Issue> {
const FIRST_LANES: u32 = 3;

/// What a valid stack file may want changed (`devstack check` prints them):
/// `[stack] reviewed` without the `requires` that keeps an older devstack
/// from failing on it; two services whose ports differ by one of the first
/// a newer key (`[stack] reviewed`, `announced_ports`, a service's `stop`)
/// without the `requires` that keeps an older devstack from failing on it;
/// two services whose ports differ by one of the first
/// few multiples of `[lanes] port_step`, so that one of the first lanes
/// would run one of them on the other's port. The daemon skips such lane
/// numbers, so lane 1 is not on `port + port_step` as the file suggests.
Expand Down Expand Up @@ -1058,6 +1075,37 @@ pub fn warnings(stack: &Stack) -> Vec<Issue> {
});
}
}
let stop = crate::version_order(crate::STOP_SINCE);
if stack
.stack
.requires
.as_deref()
.is_none_or(|r| crate::version_order(r) < stop)
{
let in_profiles = stack.profiles.iter().flat_map(|(p, profile)| {
profile
.services
.iter()
.filter(|(_, o)| o.stop.is_some())
.map(move |(n, _)| format!("profiles.{p}.services.{n}.stop"))
});
let paths: Vec<String> = stack
.services
.iter()
.filter(|(_, svc)| svc.stop.is_some())
.map(|(n, _)| format!("services.{n}.stop"))
.chain(in_profiles)
.collect();
for path in paths {
out.push(Issue {
path,
message: format!(
"a devstack before {v} does not know this key and fails on it with \"unknown field\": `requires = \"{v}\"` makes it ask to be updated instead",
v = crate::STOP_SINCE
),
});
}
}
if step == 0 {
return out;
}
Expand Down
25 changes: 25 additions & 0 deletions crates/devstack-config/tests/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,28 @@ fn skip_wants_requires_at_its_first_devstack() {
s.stack.requires = Some(SKIP_SINCE.into());
assert!(warnings(&s).iter().all(|i| i.path != "profiles.lite.skip"));
}

#[test]
fn a_profile_sets_a_services_stop_and_a_static_one_drops_it() {
let s = load(
r#"
[services.ocr]
run = "docker run --rm --init --name ocr{lane_suffix}-{port} -p {port}:8000 ocr:dev"
stop = "docker rm -f ocr{lane_suffix}-{port}"
port = 5100

[profiles.acceptance.services.mailer]
stop = "mailer --stop"

[profiles.acceptance.services.ocr]
kind = "static"
build = "npm run build"
output = "dist"
"#,
)
.unwrap();
let a = s.with_profile("acceptance").unwrap();
assert_eq!(a.services["mailer"].stop.as_deref(), Some("mailer --stop"));
assert_eq!(a.services["ocr"].stop, None);
assert!(s.services["mailer"].stop.is_none());
}
37 changes: 37 additions & 0 deletions crates/devstack-config/tests/stack_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,3 +1025,40 @@ services:
assert!(!all.contains("DEVSTACK_PORT_X"), "{all}");
assert_eq!(w.len(), 3, "each once: {all}");
}

#[test]
fn a_services_stop_takes_the_placeholders_of_its_run() {
use devstack_config::{STOP_SINCE, validate::warnings};
let text = MINIMAL.replace(
"port = 5000\n",
"port = 5000\nstop = \"docker rm -f api{lane_suffix}-{port}\"\n",
);
let mut s = from_str(&text, Path::new("t")).unwrap();
assert_eq!(
s.services["api"].stop.as_deref(),
Some("docker rm -f api{lane_suffix}-{port}")
);
let w = warnings(&s);
assert!(
w.iter()
.any(|i| i.path == "services.api.stop" && i.message.contains(STOP_SINCE)),
"{w:?}"
);
s.stack.requires = Some(STOP_SINCE.into());
assert!(warnings(&s).is_empty());

// An unknown placeholder, an empty command and a static service's stop.
let all = invalid(&MINIMAL.replace("port = 5000\n", "port = 5000\nstop = \"rm {nope}\"\n"))
.join("\n");
assert!(all.contains("services.api.stop"), "{all}");
let all = invalid(&MINIMAL.replace("port = 5000\n", "port = 5000\nstop = \" \"\n")).join("\n");
assert!(all.contains("services.api.stop: is empty"), "{all}");
let all = invalid(&format!(
"{MINIMAL}[services.site]\nkind = \"static\"\nbuild = \"npm run build\"\noutput = \"dist\"\nport = 4300\nstop = \"true\"\n"
))
.join("\n");
assert!(
all.contains("services.site.stop: kind `static` has no `stop`"),
"{all}"
);
}
16 changes: 16 additions & 0 deletions crates/devstack-daemon/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,28 @@ pub struct Persisted {
/// Process trees to clean up after a crash (Linux; ADR-0016).
#[serde(default)]
pub records: Vec<devstack_proc::Record>,
/// The `stop` command of every running instance that has one
/// (ADR-0059), expanded: what the next start runs after a crash.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub stops: Vec<StopRecord>,
/// Service -> the candidate a promotion gate holds (ADR-0013), so the
/// gate outlives a daemon restart.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub held: BTreeMap<String, HeldRecord>,
}

/// An instance's expanded `stop` command (ADR-0059) as `state.json` keeps
/// it, without the instance's environment (it can hold secrets).
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct StopRecord {
/// The instance's cleanup record (`<service>@<port>`).
pub record: String,
pub command: String,
pub cwd: PathBuf,
/// The instance's log, which its output goes to.
pub log: PathBuf,
}

/// A held candidate as `state.json` keeps it.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct HeldRecord {
Expand Down
Loading
Loading