use crate::{CtlError, ErrorCode};
use std::{cell::RefCell, marker::PhantomData, path::PathBuf, rc::Rc};
thread_local! {
static PAUSE: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
}
pub struct Scope(Option<PathBuf>, PhantomData<Rc<()>>);
impl Scope {
pub fn enter(path: PathBuf) -> Self {
Self(PAUSE.with(|p| p.replace(Some(path))), PhantomData)
}
}
impl Drop for Scope {
fn drop(&mut self) {
PAUSE.with(|p| p.replace(self.0.take()));
}
}
pub fn check() -> Result<(), CtlError> {
crate::stop::check_current()?;
PAUSE.with(|p| {
if let Some(path) = p.borrow().as_ref()
&& path.exists()
{
return Err(CtlError::new(
ErrorCode::Aborted,
"workflow wait interrupted",
));
}
Ok(())
})
}
pub fn pause_pending() -> bool {
PAUSE.with(|p| p.borrow().as_ref().is_some_and(|path| path.exists()))
}