Refactor app deletion logic to handle contracts asynchronously and improve error logging

This commit is contained in:
Noor 2025-02-07 13:16:55 +00:00
parent ba4884e25a
commit 28618f38d0
Signed by: noormohammedb
GPG Key ID: E424C39E19EFD7DF
2 changed files with 18 additions and 5 deletions

@ -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()
}

@ -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<AppContract>) {
dbg!(&contracts);
async fn handle_contracts(&mut self, contracts: Vec<AppContract>) {
let apps_in_host = self.host_resource.existing_apps.clone();
let apps_in_brain: HashSet<String> = contracts
.iter()
.map(|contact| contact.uuid.clone())
.collect();
let deleted_apps: HashSet<String> =
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<dyn std::error::Error>> {
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}"),
}