mirror of
https://github.com/neondatabase/neon.git
synced 2026-05-24 00:20:37 +00:00
Handle panic
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user