The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Use Control-C To Stop A Rust Process That's Using watchexec

TODO: Pull subtitle into page object
Code
use core::fmt::Error;
use miette::Result;
use watchexec::action::Action;
use watchexec::action::Outcome;
use watchexec::config::InitConfig;
use watchexec::config::RuntimeConfig;
use watchexec::Watchexec;
use watchexec_signals::Signal;

#[tokio::main]
async fn main() -> Result<()> {
    println!("Starting process");
    let init = InitConfig::default();
    let mut runtime = RuntimeConfig::default();
    runtime.on_action(move |action: Action| async move {
         let mut stop: bool = false;
        action.events.iter().for_each(|event| {
            event.signals().for_each(|sig| match sig {
                Signal::Interrupt => {
                    println!("Caught Interrupt: Stopping");
                    stop = true;
                }
                _ => {}
            });
        });
        // TODO: put in if statement here that checks
        // for if the stop toggle has been switched
        action.outcome(Outcome::Exit);
        Ok::<(), Error>(())
    });
    let we = Watchexec::new(init, runtime)?;
    we.main().await.unwrap().unwrap();
    Ok(())
}

That's relatively concise, but I expect there's ways to tighten it up. In the mean time, it's doing what I need.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Use Control-C To Stop A Rust Process That's Using watchexec

I'm using the watchexec^we^^ Rust crate to run my
site generate process every time a file changes. 
It's the same create that powers cargo watch^cw^^. 

I ran into a problem with it where once I started it
it <<kbd|CTRL>><<kbd|C>> wouldn't stop it because
it captured the signal. That makes sense, but meant
the only way I could stop the process was to kill
the terminal window. 

This is what I came up with to catch the Interrupt
signal that's sent by control-c in watchexec
and tell it to stop itself via an 
`Outcome::Exit`rust` signal:


-- code
-- rust

use core::fmt::Error;
use miette::Result;
use watchexec::action::Action;
use watchexec::action::Outcome;
use watchexec::config::InitConfig;
use watchexec::config::RuntimeConfig;
use watchexec::Watchexec;
use watchexec_signals::Signal;

#[tokio::main]
async fn main() -> Result<()> {
    println!("Starting process");
    let init = InitConfig::default();
    let mut runtime = RuntimeConfig::default();
    runtime.on_action(move |action: Action| async move {
         let mut stop: bool = false;
        action.events.iter().for_each(|event| {
            event.signals().for_each(|sig| match sig {
                Signal::Interrupt => {
                    println!("Caught Interrupt: Stopping");
                    stop = true;
                }
                _ => {}
            });
        });
        // TODO: put in if statement here that checks
        // for if the stop toggle has been switched
        action.outcome(Outcome::Exit);
        Ok::<(), Error>(())
    });
    let we = Watchexec::new(init, runtime)?;
    we.main().await.unwrap().unwrap();
    Ok(())
}

-- p

That's relatively concise, but I expect there's ways 
to tighten it up. In the mean time, it's doing what
I need.


-- ref
-- title: cargo-watch
-- url: https://crates.io/crates/cargo-watch
-- id: cw

-- ref
-- title: watchexec GitHub Repo
-- url: https://github.com/watchexec/watchexec

-- ref
-- title: watchexec Rust Crate Documentation
-- url: https://docs.rs/watchexec/latest/watchexec/
-- id: we

-- ref
-- title: watchexec action::Outcome docs
-- url: https://docs.rs/watchexec/latest/watchexec/action/enum.Outcome.html

-- ref
-- title: watchexec MainSignal docs
-- url: https://docs.rs/watchexec/latest/watchexec/signal/source/enum.MainSignal.html

-- ref
-- title: watchexec Signals
-- url: https://docs.rs/watchexec-signals/1.0.0/watchexec_signals/enum.Signal.html


-- categories
-- Rust 
-- watchexec 

-- metadata
-- date: 2023-04-29 18:38:24
-- id: 2p7v37ch
-- site: aws
-- type: post
-- status: published