Skip to main content

actl_core/
wait_control.rs

1//! Scoped cancellation for workflow waits; never interrupts an in-flight COM call.
2use crate::{CtlError, ErrorCode};
3use std::{cell::RefCell, marker::PhantomData, path::PathBuf, rc::Rc};
4
5thread_local! {
6    static PAUSE: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
7}
8pub struct Scope(Option<PathBuf>, PhantomData<Rc<()>>);
9impl Scope {
10    pub fn enter(path: PathBuf) -> Self {
11        Self(PAUSE.with(|p| p.replace(Some(path))), PhantomData)
12    }
13}
14impl Drop for Scope {
15    fn drop(&mut self) {
16        PAUSE.with(|p| p.replace(self.0.take()));
17    }
18}
19pub fn check() -> Result<(), CtlError> {
20    crate::stop::check_current()?;
21    PAUSE.with(|p| {
22        if let Some(path) = p.borrow().as_ref()
23            && path.exists()
24        {
25            return Err(CtlError::new(
26                ErrorCode::Aborted,
27                "workflow wait interrupted",
28            ));
29        }
30        Ok(())
31    })
32}