Bring over some overlord minion-exit handling we forgot from the tauri branch

This commit is contained in:
Mike Dilger 2022-12-24 20:13:30 +13:00
parent a9a4688b7e
commit 79d2e2ca3a

View File

@ -13,13 +13,16 @@ use std::collections::HashMap;
use tokio::sync::broadcast::Sender;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::{select, task};
use tracing::{debug, error, info};
use tracing::{debug, error, info, warn};
pub struct Overlord {
settings: Settings,
to_minions: Sender<BusMessage>,
from_minions: UnboundedReceiver<BusMessage>,
// All the minion tasks running.
minions: task::JoinSet<()>,
minions_task_url: HashMap<task::Id, Url>,
#[allow(dead_code)]
private_key: Option<PrivateKey>, // note that PrivateKey already zeroizes on drop
@ -217,6 +220,44 @@ impl Overlord {
};
keepgoing = self.handle_bus_message(bus_message).await?;
},
task_next_joined = self.minions.join_next_with_id() => {
if task_next_joined.is_none() {
return Ok(true); // rare but possible
}
match task_next_joined.unwrap() {
Err(join_error) => {
let id = join_error.id();
let maybe_url = self.minions_task_url.get(&id);
match maybe_url {
Some(url) => {
// JoinError also has is_cancelled, is_panic, into_panic, try_into_panic
// Minion probably alreaedy logged, this may be redundant.
warn!("Minion {} completed with error: {}", &url, join_error);
// Minion probably already logged failure in relay table
// Remove from our hashmap
self.minions_task_url.remove(&id);
},
None => {
warn!("Minion UNKNOWN completed with error: {}", join_error);
}
}
},
Ok((id, _)) => {
let maybe_url = self.minions_task_url.get(&id);
match maybe_url {
Some(url) => {
warn!("Relay Task {} completed", &url);
// Remove from our hashmap
self.minions_task_url.remove(&id);
},
None => warn!("Relay Task UNKNOWN completed"),
}
}
}
}
}
Ok(keepgoing)
@ -244,6 +285,7 @@ impl Overlord {
"overlord" => match &*bus_message.kind {
"minion_is_ready" => {}
"save_settings" => {
// from ui
let settings: Settings = serde_json::from_str(&bus_message.json_payload)?;
// Save to database