From 9fa62a1978d2e2e6390a91a18a39262a4a5c22c0 Mon Sep 17 00:00:00 2001 From: ghe0 Date: Mon, 27 Jan 2025 17:45:28 +0200 Subject: [PATCH] inform daemon about VMs deleted by cron --- src/data.rs | 18 ++++++++++++++++++ src/grpc.rs | 8 +++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/data.rs b/src/data.rs index b2d0ce3..ff0620b 100644 --- a/src/data.rs +++ b/src/data.rs @@ -192,6 +192,7 @@ impl BrainData { } pub fn contracts_cron(&self) { + let mut deleted_contracts = Vec::new(); log::debug!("Running contracts cron..."); let mut contracts = self.contracts.write().unwrap(); contracts.retain_mut(|c| { @@ -213,8 +214,25 @@ impl BrainData { ); c.locked_nano -= nanotokens_to_collect; self.add_nano_to_wallet(&owner_key, nanotokens_to_collect); + if c.locked_nano == 0 { + deleted_contracts.push((c.uuid.clone(), c.node_pubkey.clone())); + } c.locked_nano > 0 }); + // inform daemons of the deletion of the contracts + for (uuid, node_pubkey) in deleted_contracts.iter() { + if let Some(daemon_tx) = self.daemon_tx.get(&node_pubkey.clone()) { + let msg = grpc::BrainMessage { + msg: Some(grpc::brain_message::Msg::DeleteVm(grpc::DeleteVmReq { + uuid: uuid.to_string(), + })), + }; + let daemon_tx = daemon_tx.clone(); + tokio::runtime::Runtime::new().unwrap().spawn(async move { + let _ = daemon_tx.send(msg).await; + }); + } + } } pub fn insert_node(&self, node: Node) { diff --git a/src/grpc.rs b/src/grpc.rs index fcad76d..e2e9055 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -162,11 +162,9 @@ impl BrainCli for BrainCliMock { info!("Sending UpdateVMResp: {response:?}"); Ok(Response::new(response)) } - Err(e) => { - Err(Status::unknown( - "Update VM request failed due to error: {e}", - )) - } + Err(e) => Err(Status::unknown(format!( + "Update VM request failed due to error: {e}" + ))), } }