feat: add cron job for app nodes
This commit is contained in:
parent
9be7abc9cf
commit
fa700c2315
53
src/data.rs
53
src/data.rs
@ -442,7 +442,8 @@ impl BrainData {
|
||||
let node = match nodes
|
||||
.iter()
|
||||
.find(|n| n.public_key == c.node_pubkey)
|
||||
.cloned() {
|
||||
.cloned()
|
||||
{
|
||||
Some(n) => n,
|
||||
None => return c.locked_nano > 0,
|
||||
};
|
||||
@ -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...");
|
||||
|
@ -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}")
|
||||
|
Loading…
Reference in New Issue
Block a user