tokio_process_tools/
inspector.rsuse thiserror::Error;
use tokio::sync::oneshot::Sender;
use tokio::task::JoinHandle;
#[derive(Debug, Error)]
pub enum InspectorError {
#[error("The inspector task could not be joined/terminated: {0}")]
TaskJoin(#[from] tokio::task::JoinError),
}
pub struct Inspector {
pub(crate) task: Option<JoinHandle<()>>,
pub(crate) task_termination_sender: Option<Sender<()>>,
}
impl Inspector {
pub async fn abort(mut self) -> Result<(), InspectorError> {
if let Some(task_termination_sender) = self.task_termination_sender.take() {
if let Err(_err) = task_termination_sender.send(()) {
tracing::error!(
"Unexpected failure when sending termination signal to inspector task."
);
};
}
if let Some(task) = self.task.take() {
return task.await.map_err(InspectorError::TaskJoin);
}
unreachable!("The inspector task was already aborted");
}
}
impl Drop for Inspector {
fn drop(&mut self) {
if let Some(task_termination_sender) = self.task_termination_sender.take() {
if let Err(_err) = task_termination_sender.send(()) {
tracing::error!(
"Unexpected failure when sending termination signal to inspector task."
);
}
}
if let Some(task) = self.task.take() {
task.abort();
}
}
}