Skip to main content

gosh_runner/
stop.rs

1// [[file:../runners.note::809ad587][809ad587]]
2//! A simple file based handler for user interruption.
3use super::*;
4use std::path::PathBuf;
5
6/// A simple file based user interruption handler: return Err if STOP file
7/// exits.
8pub struct StopFileHandler {
9    stop_file: PathBuf,
10}
11
12impl StopFileHandler {
13    pub fn new() -> Self {
14        let stop_file = PathBuf::from("STOP");
15        if stop_file.exists() {
16            println!("Removing existing STOP file ...");
17            let _ = std::fs::remove_file(&stop_file);
18        }
19        Self {
20            stop_file: PathBuf::from("STOP"),
21        }
22    }
23
24    fn is_interrupted(&self) -> bool {
25        self.stop_file.exists()
26    }
27
28    /// Return error if finding a STOP file.
29    pub fn handle_user_interruption(&self) -> Result<()> {
30        if self.is_interrupted() {
31            bail!("found STOP file, stopping now ...");
32        } else {
33            Ok(())
34        }
35    }
36}
37// 809ad587 ends here