Improve error handling in Tasks
This commit is contained in:
parent
4e80badc92
commit
e40003482d
3 changed files with 36 additions and 22 deletions
|
|
@ -359,7 +359,7 @@ impl App {
|
||||||
// Get System32 path
|
// Get System32 path
|
||||||
let system32 = if cfg!(windows) {
|
let system32 = if cfg!(windows) {
|
||||||
match env::var("SYSTEMROOT") {
|
match env::var("SYSTEMROOT") {
|
||||||
Ok(path) => path,
|
Ok(path) => format!("{path}/System32"),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
self.action_tx.send(Action::Error(String::from(
|
self.action_tx.send(Action::Error(String::from(
|
||||||
"ERROR\n\n\nFailed to find SYSTEMROOT",
|
"ERROR\n\n\nFailed to find SYSTEMROOT",
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ pub fn get_disks() -> Vec<Disk> {
|
||||||
} else {
|
} else {
|
||||||
info!("Get (fake) disks");
|
info!("Get (fake) disks");
|
||||||
disks = get_fake_disks();
|
disks = get_fake_disks();
|
||||||
sleep(Duration::from_millis(500));
|
sleep(Duration::from_millis(250));
|
||||||
}
|
}
|
||||||
disks
|
disks
|
||||||
}
|
}
|
||||||
|
|
@ -315,9 +315,7 @@ pub fn get_disk_serial_number(id: usize) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh_disk_info(disk: &mut Disk) -> Disk {
|
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;
|
let mut new_disk: Disk;
|
||||||
info!("-- Before: {:?}", &disk.parts);
|
|
||||||
if cfg!(windows) {
|
if cfg!(windows) {
|
||||||
info!("Refresh disk via Diskpart");
|
info!("Refresh disk via Diskpart");
|
||||||
new_disk = diskpart::refresh_disk_info(disk);
|
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.parts = refresh_fake_disk_info();
|
||||||
new_disk.generate_descriptions();
|
new_disk.generate_descriptions();
|
||||||
}
|
}
|
||||||
info!("-------------------------------------------------");
|
|
||||||
info!("-- After: {:?}", &new_disk.parts);
|
|
||||||
new_disk
|
new_disk
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
50
src/tasks.rs
50
src/tasks.rs
|
|
@ -79,8 +79,6 @@ impl Tasks {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn poll(&mut self) -> Result<()> {
|
pub fn poll(&mut self) -> Result<()> {
|
||||||
info!("Polling tasks {:?} // {:?}", &self.handle, &self.task_list);
|
|
||||||
|
|
||||||
// Forward any actions to main app
|
// Forward any actions to main app
|
||||||
if let Ok(action) = self.task_rx.try_recv() {
|
if let Ok(action) = self.task_rx.try_recv() {
|
||||||
let result = self.action_tx.send(action.clone());
|
let result = self.action_tx.send(action.clone());
|
||||||
|
|
@ -107,16 +105,12 @@ impl Tasks {
|
||||||
} else if !self.task_list.is_empty() {
|
} else if !self.task_list.is_empty() {
|
||||||
// No current task but one is available
|
// No current task but one is available
|
||||||
self.start()?;
|
self.start()?;
|
||||||
} else {
|
|
||||||
// No current task or any queued, continue
|
|
||||||
self.task_tx.send(Action::NextScreen)?;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(&mut self) -> Result<()> {
|
pub fn start(&mut self) -> Result<()> {
|
||||||
if let Some(task) = self.task_list.pop_front() {
|
if let Some(task) = self.task_list.pop_front() {
|
||||||
info!("Starting task: {task:?}");
|
|
||||||
let task_str = format!("{task:?}");
|
let task_str = format!("{task:?}");
|
||||||
let task_tx = self.task_tx.clone();
|
let task_tx = self.task_tx.clone();
|
||||||
match task {
|
match task {
|
||||||
|
|
@ -125,21 +119,39 @@ impl Tasks {
|
||||||
let cmd_args = cmd_args.clone();
|
let cmd_args = cmd_args.clone();
|
||||||
if cfg!(windows) {
|
if cfg!(windows) {
|
||||||
self.handle = Some(thread::spawn(move || {
|
self.handle = Some(thread::spawn(move || {
|
||||||
let output = Command::new(cmd_path)
|
let result = Command::new(cmd_path)
|
||||||
.args(cmd_args)
|
.args(cmd_args)
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.output();
|
.output();
|
||||||
if output.is_err() {
|
if let Some(action) = match result {
|
||||||
if let Err(_) = task_tx
|
Ok(output) => {
|
||||||
.send(Action::Error(String::from("Failed to run command")))
|
if output.status.success() {
|
||||||
{
|
None
|
||||||
panic!("Failed to send Action: {task_str:?}");
|
} 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 {
|
} else {
|
||||||
// Simulate task if not running under Windows
|
// 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) => {
|
Task::Diskpart(ref script) => {
|
||||||
|
|
@ -157,7 +169,7 @@ impl Tasks {
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
// Simulate task if not running under Windows
|
// 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 => {
|
Task::ScanDisks => {
|
||||||
|
|
@ -172,7 +184,7 @@ impl Tasks {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
Task::Sleep => {
|
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) => {
|
Task::UpdateDestDisk(index) => {
|
||||||
self.action_tx.send(Action::DisplayPopup(
|
self.action_tx.send(Action::DisplayPopup(
|
||||||
|
|
@ -194,7 +206,13 @@ impl Tasks {
|
||||||
}
|
}
|
||||||
Task::UpdateDiskList => {
|
Task::UpdateDiskList => {
|
||||||
let disks = self.disk_list.lock().unwrap();
|
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:?}");
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue