inform daemon about VMs deleted by cron

This commit is contained in:
ghe0 2025-01-27 17:45:28 +02:00
parent f0f5edac00
commit 9fa62a1978
Signed by: ghe0
GPG Key ID: 451028EE56A0FBB4
2 changed files with 21 additions and 5 deletions

@ -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) {

@ -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}"
))),
}
}