diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 4 | ||||
| -rw-r--r-- | src/migrate.rs | 4 | ||||
| -rw-r--r-- | src/plan.rs | 44 |
3 files changed, 38 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs index 3539d76..49b8558 100644 --- a/src/main.rs +++ b/src/main.rs @@ -230,13 +230,13 @@ fn run() -> Result<ExitCode> { let systemctl = settings .systemctl .unwrap_or_else(|| PathBuf::from("systemctl")); - let _lock = store.lock()?; + let lock = store.lock()?; match command { "plan" | "apply" => { let submission = Submission::load(path.as_deref().unwrap())?; let planned = plan::plan(&schema, &store, &submission)?; let plan = if command == "apply" { - planned.apply(&store, &schema_bytes, &systemctl)? + planned.apply(&store, &schema_bytes, &systemctl, lock)? } else { planned.plan }; diff --git a/src/migrate.rs b/src/migrate.rs index 340180f..dbbf38d 100644 --- a/src/migrate.rs +++ b/src/migrate.rs @@ -23,7 +23,7 @@ use serde::Serialize; use crate::content::Content; use crate::name::Address; -use crate::plan::{ViewOutcome, react}; +use crate::plan::{Reacting, ViewOutcome, react}; use crate::schema::{Reactions, Schema}; use crate::scratch::Scratch; use crate::store::{Kept, Snapshot, Store}; @@ -311,7 +311,7 @@ pub fn migrate( let failed = if dry_run { Vec::new() } else { - react(&reactions, systemctl) + react(&reactions, systemctl, Reacting::Migrate) }; Ok(Migration { accepted: refused.is_empty(), diff --git a/src/plan.rs b/src/plan.rs index 80e2bad..8627dd8 100644 --- a/src/plan.rs +++ b/src/plan.rs @@ -21,7 +21,7 @@ use crate::name::Address; use crate::program::{self, Given, ProgramFailed, Role}; use crate::schema::{Reactions, Schema}; use crate::scratch::Scratch; -use crate::store::{Kept, Store}; +use crate::store::{Kept, Lock, Store}; use crate::submission::{Change, Item, Submission}; use crate::view::{self, FileChange}; @@ -469,8 +469,16 @@ fn apply_items(base: &Content, items: &[Item], excluded: &BTreeSet<usize>) -> Co impl Planned { /// Applies the plan, if it was accepted: the new snapshot, the views that change, - /// and their reactions, by `systemctl`. - pub fn apply(self, store: &Store, schema_bytes: &[u8], systemctl: &Path) -> Result<Plan> { + /// and, with `lock` released, their reactions. Units restarted may want the unit + /// that runs `--migrate`, which waits for the lock: holding it while they restart + /// would deadlock. + pub fn apply( + self, + store: &Store, + schema_bytes: &[u8], + systemctl: &Path, + lock: Lock, + ) -> Result<Plan> { let mut plan = self.plan; if !plan.accepted { return Ok(plan); @@ -491,22 +499,38 @@ impl Planned { .with_context(|| format!("view {name}"))?; } } - plan.failed = react(&plan.reactions, systemctl); + drop(lock); + plan.failed = react(&plan.reactions, systemctl, Reacting::Apply); Ok(plan) } } -/// Runs `reactions`; what failed. -pub fn react(reactions: &Reactions, systemctl: &Path) -> Vec<String> { +/// How reactions are run. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Reacting { + /// After applying a submission: units are restarted or reloaded, and started if + /// they aren't running (they may have failed for want of their views). + Apply, + /// After a generation change, from the unit that runs `--migrate`, which units + /// using views are ordered after: only units that are running, without waiting + /// for them (`--no-block`). A unit that isn't running starts with the views in + /// place. + Migrate, +} + +/// Runs `reactions` by `systemctl`, each unit once; returns what failed. +pub fn react(reactions: &Reactions, systemctl: &Path, how: Reacting) -> Vec<String> { let mut failed = Vec::new(); - for (verb, units) in [ - ("restart", &reactions.restart), - ("reload", &reactions.reload), - ] { + let (options, restart, reload): (&[&str], _, _) = match how { + Reacting::Apply => (&[], "restart", "reload-or-restart"), + Reacting::Migrate => (&["--no-block"], "try-restart", "try-reload-or-restart"), + }; + for (verb, units) in [(restart, &reactions.restart), (reload, &reactions.reload)] { if units.is_empty() { continue; } let result = Command::new(systemctl) + .args(options) .arg(verb) .args(units) .status() |
