fix: Let the service be able to be started

This commit is contained in:
J H
2024-03-04 13:37:48 -07:00
parent 5366b4c873
commit 11c93231aa
3 changed files with 39 additions and 16 deletions

View File

@@ -389,7 +389,7 @@ impl Service {
}
}
#[derive(Clone)]
#[derive(Debug, Clone)]
struct RunningStatus {
health: OrdMap<HealthCheckId, HealthCheckResult>,
started: DateTime<Utc>,
@@ -406,7 +406,32 @@ pub(self) struct ServiceActorSeed {
synchronized: Arc<Notify>,
}
impl ServiceActorSeed {
pub fn started(&self) {
self.persistent_container
.current_state
.send_replace(StartStop::Start);
self.persistent_container
.running_status
.send_modify(|running_status| {
*running_status =
Some(
std::mem::take(running_status).unwrap_or_else(|| RunningStatus {
health: Default::default(),
started: Utc::now(),
}),
);
})
}
pub fn stopped(&self) {
self.persistent_container
.current_state
.send_replace(StartStop::Stop);
self.persistent_container.running_status.send_replace(None);
}
}
struct ServiceActor(Arc<ServiceActorSeed>);
impl Actor for ServiceActor {
fn init(&mut self, jobs: &mut BackgroundJobs) {
let seed = self.0.clone();
@@ -418,7 +443,7 @@ impl Actor for ServiceActor {
let mut transition = seed.transition_state.subscribe();
let mut running = seed.running_status.clone();
loop {
let (desired_state, current_state, transition_kind, running_status) = (
let (desired_state, current_state, transition_kind, running_status) = dbg!(
temp_desired.borrow().unwrap_or(*desired.borrow()),
*current.borrow(),
transition.borrow().as_ref().map(|t| t.kind()),
@@ -464,10 +489,8 @@ impl Actor for ServiceActor {
timeout: todo!("sigterm timeout"),
}
}
(None, StartStop::Start, StartStop::Stop, _) => {
MainStatus::Starting
}
(None, StartStop::Start, StartStop::Start, None) => {
(None, StartStop::Start, StartStop::Stop, _)
| (None, StartStop::Start, StartStop::Start, None) => {
MainStatus::Starting
}
(None, StartStop::Start, StartStop::Start, Some(status)) => {

View File

@@ -1,9 +1,10 @@
use std::ffi::OsString;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::{Arc, Weak};
use std::{ffi::OsString, time::Instant};
use chrono::Utc;
use clap::builder::{TypedValueParser, ValueParserFactory};
use clap::Parser;
use imbl_value::json;
@@ -12,7 +13,6 @@ use patch_db::json_ptr::JsonPointer;
use rpc_toolkit::{from_fn, from_fn_async, AnyContext, Context, Empty, HandlerExt, ParentHandler};
use tokio::process::Command;
use crate::db::model::ExposedUI;
use crate::disk::mount::filesystem::idmapped::IdMapped;
use crate::disk::mount::filesystem::loop_dev::LoopDev;
use crate::disk::mount::filesystem::overlayfs::OverlayGuard;
@@ -25,6 +25,7 @@ use crate::status::health_check::HealthCheckResult;
use crate::status::MainStatus;
use crate::util::clap::FromStrParser;
use crate::util::{new_guid, Invoke};
use crate::{db::model::ExposedUI, service::RunningStatus};
use crate::{echo, ARCH};
#[derive(Clone)]
@@ -487,6 +488,7 @@ async fn stopped(context: EffectContext, params: ParamsMaybePackageId) -> Result
Ok(json!(matches!(package, MainStatus::Stopped)))
}
async fn running(context: EffectContext, params: ParamsMaybePackageId) -> Result<Value, Error> {
dbg!("Starting the running {params:?}");
let context = context.deref()?;
let peeked = context.ctx.db.peek().await;
let package_id = params.package_id.unwrap_or_else(|| context.id.clone());
@@ -586,14 +588,12 @@ struct SetMainStatus {
status: Status,
}
async fn set_main_status(context: EffectContext, params: SetMainStatus) -> Result<Value, Error> {
dbg!(format!("Status for main will be is {params:?}"));
let context = context.deref()?;
context
.persistent_container
.current_state
.send_replace(match params.status {
Status::Running => StartStop::Start,
Status::Stopped => StartStop::Stop,
});
match params.status {
Status::Running => context.started(),
Status::Stopped => context.stopped(),
}
Ok(Value::Null)
}

View File

@@ -51,7 +51,7 @@ impl Drop for TransitionState {
}
}
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct TempDesiredState(pub(super) Arc<watch::Sender<Option<StartStop>>>);
impl TempDesiredState {
pub fn stop(&self) {