diff --git a/backend/src/manager/manager_container.rs b/backend/src/manager/manager_container.rs index ba13a652d..00937fc5c 100644 --- a/backend/src/manager/manager_container.rs +++ b/backend/src/manager/manager_container.rs @@ -223,16 +223,14 @@ fn starting_service( persistent_container: ManagerPersistentContainer, running_service: &mut Option>, ) { - let set_running = { - let current_state = current_state.clone(); - Arc::new(move || { - current_state.send_modify(|x| *x = StartStop::Start); - }) - }; let set_stopped = { move || current_state.send_modify(|x| *x = StartStop::Stop) }; let running_main_loop = async move { while desired_state.borrow().is_start() { - let result = run_main(seed.clone()).await; + let result = persistent_container + .execute(models::ProcedureName::Main, Value::Null, None) + .await; + + run_main(seed.clone()).await; set_stopped(); run_main_log_result(result, seed.clone()).await; } diff --git a/backend/src/manager/persistent_container.rs b/backend/src/manager/persistent_container.rs index da51f6ea0..0753a9bb4 100644 --- a/backend/src/manager/persistent_container.rs +++ b/backend/src/manager/persistent_container.rs @@ -3,8 +3,10 @@ use std::time::Duration; use color_eyre::eyre::eyre; use helpers::UnixRpcClient; -use tokio::sync::oneshot; +use models::ProcedureName; +use serde::de::DeserializeOwned; use tokio::sync::watch::{self, Receiver}; +use tokio::sync::{oneshot, Mutex}; use tracing::instrument; use super::manager_seed::ManagerSeed; @@ -12,15 +14,20 @@ use super::{ add_network_for_main, get_long_running_ip, long_running_docker, remove_network_for_main, GetRunningIp, }; +use crate::prelude::*; use crate::procedure::docker::DockerContainer; use crate::util::NonDetachingJoinHandle; -use crate::Error; + +struct ProcedureId(u64); /// Persistant container are the old containers that need to run all the time /// The goal is that all services will be persistent containers, waiting to run the main system. pub struct PersistentContainer { _running_docker: NonDetachingJoinHandle<()>, + // TODO: Drb: Implement to spec https://github.com/Start9Labs/start-sdk/blob/master/lib/types.ts#L223 pub rpc_client: Receiver>, + manager_seed: Arc, + procedures: Mutex>, } impl PersistentContainer { @@ -32,15 +39,86 @@ impl PersistentContainer { Self { _running_docker: running_docker, rpc_client, + manager_seed: seed.clone(), + procedures: Default::default(), } } else { - todo!("No containers in manifest") + todo!("DRB No containers in manifest") }) } pub fn rpc_client(&self) -> Arc { self.rpc_client.borrow().clone() } + + pub async fn execute( + &self, + name: ProcedureName, + input: Value, + timeout: Option, + ) -> Result, Error> + where + O: DeserializeOwned, + { + match self._execute(name, input, timeout).await { + Ok(Ok(a)) => Ok(Ok(imbl_value::from_value(a).map_err(|e| { + Error::new( + eyre!("Error deserializing output: {}", e), + crate::ErrorKind::Deserialization, + ) + })?)), + Ok(Err(e)) => Ok(Err(e)), + Err(e) => Err(e), + } + } + pub async fn sanboxed( + &self, + name: ProcedureName, + input: Value, + timeout: Option, + ) -> Result, Error> + where + O: DeserializeOwned, + { + match self._sandboxed(name, input, timeout).await { + Ok(Ok(a)) => Ok(Ok(imbl_value::from_value(a).map_err(|e| { + Error::new( + eyre!("Error deserializing output: {}", e), + crate::ErrorKind::Deserialization, + ) + })?)), + Ok(Err(e)) => Ok(Err(e)), + Err(e) => Err(e), + } + } + async fn _execute( + &self, + name: ProcedureName, + input: Value, + timeout: Option, + ) -> Result, Error> { + todo!( + r#""" + DRB + Call into the persistant via rpc, start a procedure. + Procedure already has access to rpc to call back, maybe an id to track? + Should be able to cancel. + Note(Main): Only one should be running at a time + Note(Main): Has additional effect of setRunning + Note: The input (Option) is not generic because we don't want to clone this fn for each type of input + Note: The output is not generic because we don't want to clone this fn for each type of output + """# + ) + } + + async fn _sandboxed( + &self, + name: ProcedureName, + input: Value, + timeout: Option, + ) -> Result, Error> { + todo!("DRB") + } } pub async fn spawn_persistent_container(