diff --git a/appmgr/src/backup/backup_bulk.rs b/appmgr/src/backup/backup_bulk.rs index 77cf89d2a..193c0512e 100644 --- a/appmgr/src/backup/backup_bulk.rs +++ b/appmgr/src/backup/backup_bulk.rs @@ -264,7 +264,8 @@ async fn perform_backup( let mut tx = db.begin().await?; // for lock scope let (started, health) = match main_status_model.get(&mut tx, true).await?.into_owned() { - MainStatus::Running { started, health } => (Some(started.clone()), health.clone()), + MainStatus::Starting => (Some(Utc::now()), Default::default()), + MainStatus::Running { started, health } => (Some(started), health.clone()), MainStatus::Stopped | MainStatus::Stopping => (None, Default::default()), MainStatus::BackingUp { .. } => { backup_report.insert( diff --git a/appmgr/src/control.rs b/appmgr/src/control.rs index 0cbaabd69..e4fcaf81b 100644 --- a/appmgr/src/control.rs +++ b/appmgr/src/control.rs @@ -46,10 +46,7 @@ pub async fn start( .to_owned(); let mut status = installed.status().main().get_mut(&mut tx).await?; - *status = MainStatus::Running { - started: Utc::now(), - health: BTreeMap::new(), - }; + *status = MainStatus::Starting; status.save(&mut tx).await?; heal_all_dependents_transitive(&ctx, &mut tx, &id).await?; diff --git a/appmgr/src/manager/mod.rs b/appmgr/src/manager/mod.rs index 8ad083974..a778a6ce1 100644 --- a/appmgr/src/manager/mod.rs +++ b/appmgr/src/manager/mod.rs @@ -143,9 +143,10 @@ pub struct Manager { #[derive(TryFromPrimitive)] #[repr(usize)] pub enum Status { - Running = 0, - Stopped = 1, - Paused = 2, + Starting = 0, + Running = 1, + Stopped = 2, + Paused = 3, } pub struct ManagerSharedState { @@ -268,6 +269,15 @@ async fn run_main( } } }; + let _ = state + .status + .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| { + if x == Status::Starting as usize { + Some(Status::Running as usize) + } else { + None + } + }); let res = tokio::select! { a = runtime => a.map_err(|_| Error::new(eyre!("Manager runtime panicked!"), crate::ErrorKind::Docker)).and_then(|a| a), _ = health => Err(Error::new(eyre!("Health check daemon exited!"), crate::ErrorKind::Unknown)), @@ -464,10 +474,15 @@ async fn start(shared: &ManagerSharedState) -> Result<(), Error> { crate::ErrorKind::Docker, ) })?; - shared.status.store( - Status::Running as usize, - std::sync::atomic::Ordering::SeqCst, - ); + let _ = shared + .status + .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| { + if x != Status::Running as usize { + Some(Status::Starting as usize) + } else { + None + } + }); Ok(()) } diff --git a/appmgr/src/manager/sync.rs b/appmgr/src/manager/sync.rs index 2c58774d0..c2d1d1299 100644 --- a/appmgr/src/manager/sync.rs +++ b/appmgr/src/manager/sync.rs @@ -1,3 +1,4 @@ +use std::collections::BTreeMap; use std::convert::TryInto; use std::sync::atomic::Ordering; use std::time::Duration; @@ -29,16 +30,34 @@ async fn synchronize_once(shared: &ManagerSharedState) -> Result<(), Error> { MainStatus::Stopping => { *status = MainStatus::Stopped; } + MainStatus::Starting => { + start(shared).await?; + } MainStatus::Running { started, .. } => { *started = Utc::now(); start(shared).await?; } MainStatus::BackingUp { .. } => (), }, + Status::Starting => match *status { + MainStatus::Stopped | MainStatus::Stopping => { + stop(shared).await?; + } + MainStatus::Starting | MainStatus::Running { .. } => (), + MainStatus::BackingUp { .. } => { + pause(shared).await?; + } + }, Status::Running => match *status { MainStatus::Stopped | MainStatus::Stopping => { stop(shared).await?; } + MainStatus::Starting => { + *status = MainStatus::Running { + started: Utc::now(), + health: BTreeMap::new(), + }; + } MainStatus::Running { .. } => (), MainStatus::BackingUp { .. } => { pause(shared).await?; @@ -48,7 +67,7 @@ async fn synchronize_once(shared: &ManagerSharedState) -> Result<(), Error> { MainStatus::Stopped | MainStatus::Stopping => { stop(shared).await?; } - MainStatus::Running { .. } => { + MainStatus::Starting | MainStatus::Running { .. } => { resume(shared).await?; } MainStatus::BackingUp { .. } => (), diff --git a/appmgr/src/status/mod.rs b/appmgr/src/status/mod.rs index ee9fcb42e..7600cfb8b 100644 --- a/appmgr/src/status/mod.rs +++ b/appmgr/src/status/mod.rs @@ -29,6 +29,7 @@ pub struct Status { pub enum MainStatus { Stopped, Stopping, + Starting, Running { started: DateTime, health: BTreeMap, @@ -94,22 +95,25 @@ impl MainStatus { } pub fn running(&self) -> bool { match self { - MainStatus::Running { .. } + MainStatus::Starting + | MainStatus::Running { .. } | MainStatus::BackingUp { started: Some(_), .. } => true, - _ => false, + MainStatus::Stopped + | MainStatus::Stopping + | MainStatus::BackingUp { started: None, .. } => false, } } pub fn stop(&mut self) { match self { - MainStatus::Running { .. } => { + MainStatus::Starting | MainStatus::Running { .. } => { *self = MainStatus::Stopping; } MainStatus::BackingUp { started, .. } => { *started = None; } - _ => (), + MainStatus::Stopped | MainStatus::Stopping => (), } } }