diff --git a/src/data.rs b/src/data.rs index 5f47280..07d1629 100644 --- a/src/data.rs +++ b/src/data.rs @@ -66,11 +66,10 @@ impl HostResources { for (port, _) in app.mapped_ports.iter() { self.reserved_host_ports.remove(port); } - let removed_existing_app = self.existing_apps.take(&app.uuid).ok_or_else(|| { + self.existing_apps.take(&app.uuid).ok_or_else(|| { log::error!("App \"{}\" not found", app.uuid); anyhow!("App \"{}\" not found", app.uuid) })?; - dbg!(removed_existing_app, &self.existing_apps); self.save_to_disk() } diff --git a/src/main.rs b/src/main.rs index 9c483cf..6a75467 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use detee_shared::pb::brain::NewAppRes; use detee_shared::types::brain::AppDeployConfig; use log::info; use log::warn; +use std::collections::HashSet; use std::time::Duration; use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Sender; @@ -63,8 +64,21 @@ impl AppHandler { } } - fn handle_contracts(&mut self, contracts: Vec) { - dbg!(&contracts); + async fn handle_contracts(&mut self, contracts: Vec) { + let apps_in_host = self.host_resource.existing_apps.clone(); + let apps_in_brain: HashSet = contracts + .iter() + .map(|contact| contact.uuid.clone()) + .collect(); + + let deleted_apps: HashSet = + apps_in_host.difference(&apps_in_brain).cloned().collect(); + + for uuid in deleted_apps { + if let Err(e) = self.handle_del_app_req(uuid.clone()).await { + log::error!("Failed to delete app: {e}"); + } + } } async fn run(mut self) { @@ -154,7 +168,7 @@ async fn main() -> Result<(), Box> { let brain_url = app_handler.host_config.brain_url.clone(); match grpc::register_node(&app_handler.host_config).await { - Ok(app_contracts) => app_handler.handle_contracts(app_contracts), + Ok(app_contracts) => app_handler.handle_contracts(app_contracts).await, Err(e) => log::error!("Failed to connect to brain: {e}"), }