Handle panic

This commit is contained in:
Bojan Serafimov
2022-05-22 16:51:12 -04:00
parent 858ce6a8b5
commit 13ed1e32b4
2 changed files with 23 additions and 8 deletions

View File

@@ -55,11 +55,19 @@ impl<J: Job> Sched<J> {
report = self.report.1.recv() => {
// Reschedule job to run again
let send_work = self.work.0.clone();
let job = report.unwrap().for_job;
tokio::spawn(async move {
sleep(Duration::from_millis(10)).await;
send_work.send(job).await.unwrap();
});
let report = report.unwrap();
let job = report.for_job;
match report.result {
Ok(()) => {
tokio::spawn(async move {
sleep(Duration::from_millis(10)).await;
send_work.send(job).await.unwrap();
});
},
Err(e) => {
println!("task panicked");
}
}
}
}
}

View File

@@ -3,6 +3,9 @@
//!
use crate::thread_mgr::shutdown_watcher;
use tokio::sync::mpsc::{Sender, channel};
use std::any::Any;
use std::panic::AssertUnwindSafe;
use std::panic::catch_unwind;
pub trait Job: std::fmt::Debug + Send + 'static {
fn run(&self);
@@ -14,6 +17,7 @@ pub struct Worker<J: Job>(pub Sender<J>);
#[derive(Debug)]
pub struct Report<J: Job> {
pub for_job: J,
pub result: Result<(), Box<dyn Any + Send>>
}
pub fn run_worker<J: Job>(enlist: Sender<Worker<J>>, report: Sender<Report<J>>) -> anyhow::Result<()> {
@@ -31,13 +35,16 @@ pub fn run_worker<J: Job>(enlist: Sender<Worker<J>>, report: Sender<Report<J>>)
_ = shutdown_watcher => break,
j = get_work.recv() => {
if let Some(job) = j {
job.run();
let result = catch_unwind(AssertUnwindSafe(|| {
job.run();
}));
report.send(Report {
for_job: job,
result: result,
}).await.unwrap();
} else {
println!("fffffffff")
// TODO ??
// channel closed
return;
}
}
};