diff --git a/src/data.rs b/src/data.rs index e5d52eb..fb79913 100644 --- a/src/data.rs +++ b/src/data.rs @@ -442,10 +442,11 @@ impl BrainData { let node = match nodes .iter() .find(|n| n.public_key == c.node_pubkey) - .cloned() { - Some(n) => n, - None => return c.locked_nano > 0, - }; + .cloned() + { + Some(n) => n, + None => return c.locked_nano > 0, + }; if node.offline_minutes == 0 { let operator_wallet = node.operator_wallet.clone(); let minutes_to_collect = (Utc::now() - c.collected_at).num_minutes() as u64; @@ -1380,6 +1381,56 @@ impl BrainData { .collect() } + /// This is written to run every minute + pub async fn app_nodes_cron(&self) { + log::debug!("Running app nodes cron..."); + let mut nodes = self.app_nodes.write().unwrap(); + let mut app_contracts = self.app_contracts.write().unwrap(); + for node in nodes.iter_mut() { + if self.daemon_tx.contains_key(&node.node_pubkey) { + node.offline_minutes = 0; + continue; + } + let mut operator = match self + .operators + .iter_mut() + .find(|o| o.app_nodes.contains(&node.node_pubkey)) + { + Some(op) => op, + None => continue, + }; + node.offline_minutes += 1; + // compensate contract admin if the node is offline more then 5 minutes + if node.offline_minutes > 5 { + for c in app_contracts + .iter() + .filter(|c| c.node_pubkey == node.node_pubkey) + { + let compensation = c.price_per_minute() * 10; + if compensation < operator.escrow { + operator.escrow -= compensation; + self.add_nano_to_wallet(&c.admin_pubkey, compensation); + } + } + } + } + // delete nodes that are offline more than 3 hours, and clean contracts + nodes.retain(|n| { + if n.offline_minutes > 1600 { + app_contracts.retain_mut(|c| { + if c.node_pubkey == n.node_pubkey { + self.add_nano_to_wallet(&c.admin_pubkey, c.locked_nano); + } + c.node_pubkey != n.node_pubkey + }); + for mut op in self.operators.iter_mut() { + op.vm_nodes.remove(&n.node_pubkey); + } + } + n.offline_minutes <= 180 + }); + } + pub async fn app_contracts_cron(&self) { let mut deleted_app_contracts: Vec<(String, String)> = Vec::new(); log::debug!("Running app contracts cron..."); diff --git a/src/main.rs b/src/main.rs index 9d2fdbb..cd2dbb9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ async fn main() { tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; data_clone.vm_nodes_cron().await; data_clone.vm_contracts_cron().await; + data_clone.app_nodes_cron().await; data_clone.app_contracts_cron().await; if let Err(e) = data_clone.save_to_disk() { log::error!("Could not save data to disk due to error: {e}")