Improve error handling in Tasks

This commit is contained in:
2Shirt 2024-11-17 14:37:35 -08:00
parent 4e80badc92
commit e40003482d
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
3 changed files with 36 additions and 22 deletions

View file

@ -359,7 +359,7 @@ impl App {
// Get System32 path
let system32 = if cfg!(windows) {
match env::var("SYSTEMROOT") {
Ok(path) => path,
Ok(path) => format!("{path}/System32"),
Err(_) => {
self.action_tx.send(Action::Error(String::from(
"ERROR\n\n\nFailed to find SYSTEMROOT",

View file

@ -141,7 +141,7 @@ pub fn get_disks() -> Vec<Disk> {
} else {
info!("Get (fake) disks");
disks = get_fake_disks();
sleep(Duration::from_millis(500));
sleep(Duration::from_millis(250));
}
disks
}
@ -315,9 +315,7 @@ pub fn get_disk_serial_number(id: usize) -> String {
}
pub fn refresh_disk_info(disk: &mut Disk) -> Disk {
sleep(Duration::from_millis(5000)); // Wait for clone tool / disk locks
let mut new_disk: Disk;
info!("-- Before: {:?}", &disk.parts);
if cfg!(windows) {
info!("Refresh disk via Diskpart");
new_disk = diskpart::refresh_disk_info(disk);
@ -327,8 +325,6 @@ pub fn refresh_disk_info(disk: &mut Disk) -> Disk {
new_disk.parts = refresh_fake_disk_info();
new_disk.generate_descriptions();
}
info!("-------------------------------------------------");
info!("-- After: {:?}", &new_disk.parts);
new_disk
}

View file

@ -79,8 +79,6 @@ impl Tasks {
}
pub fn poll(&mut self) -> Result<()> {
info!("Polling tasks {:?} // {:?}", &self.handle, &self.task_list);
// Forward any actions to main app
if let Ok(action) = self.task_rx.try_recv() {
let result = self.action_tx.send(action.clone());
@ -107,16 +105,12 @@ impl Tasks {
} else if !self.task_list.is_empty() {
// No current task but one is available
self.start()?;
} else {
// No current task or any queued, continue
self.task_tx.send(Action::NextScreen)?;
}
Ok(())
}
pub fn start(&mut self) -> Result<()> {
if let Some(task) = self.task_list.pop_front() {
info!("Starting task: {task:?}");
let task_str = format!("{task:?}");
let task_tx = self.task_tx.clone();
match task {
@ -125,21 +119,39 @@ impl Tasks {
let cmd_args = cmd_args.clone();
if cfg!(windows) {
self.handle = Some(thread::spawn(move || {
let output = Command::new(cmd_path)
let result = Command::new(cmd_path)
.args(cmd_args)
.stdout(Stdio::piped())
.output();
if output.is_err() {
if let Err(_) = task_tx
.send(Action::Error(String::from("Failed to run command")))
{
panic!("Failed to send Action: {task_str:?}");
if let Some(action) = match result {
Ok(output) => {
if output.status.success() {
None
} else {
// Command returned an error status
let mut msg = String::new();
if let Ok(stdout) = String::from_utf8(output.stdout) {
msg = String::from(stdout.trim());
}
if msg.is_empty() {
msg = String::from("Generic error");
}
Some(Action::Error(format!("Command failed: {msg}",)))
}
}
Err(err) => {
Some(Action::Error(format!("Failed to run command: {:?}", err)))
}
} {
let msg = format!("{:?}", &action);
if let Err(_) = task_tx.send(action) {
panic!("Failed to send Action: {msg}");
}
}
}));
} else {
// Simulate task if not running under Windows
self.handle = Some(thread::spawn(|| sleep(Duration::from_secs(1))));
self.handle = Some(thread::spawn(|| sleep(Duration::from_millis(250))));
}
}
Task::Diskpart(ref script) => {
@ -157,7 +169,7 @@ impl Tasks {
}));
} else {
// Simulate task if not running under Windows
self.handle = Some(thread::spawn(|| sleep(Duration::from_secs(1))));
self.handle = Some(thread::spawn(|| sleep(Duration::from_millis(250))));
}
}
Task::ScanDisks => {
@ -172,7 +184,7 @@ impl Tasks {
}))
}
Task::Sleep => {
self.handle = Some(thread::spawn(|| sleep(Duration::from_secs(1))));
self.handle = Some(thread::spawn(|| sleep(Duration::from_millis(250))));
}
Task::UpdateDestDisk(index) => {
self.action_tx.send(Action::DisplayPopup(
@ -194,7 +206,13 @@ impl Tasks {
}
Task::UpdateDiskList => {
let disks = self.disk_list.lock().unwrap();
self.action_tx.send(Action::UpdateDiskList(disks.clone()))?;
let disks_copy = disks.clone();
let action_tx = self.action_tx.clone();
self.handle = Some(thread::spawn(move || {
if let Err(err) = action_tx.send(Action::UpdateDiskList(disks_copy)) {
panic!("Failed to send Action: {err:?}");
}
}));
}
}
}