watchexec/crates/lib/examples/signal.rs

43 lines
1 KiB
Rust
Raw Normal View History

2021-08-19 10:31:29 +02:00
use std::process::exit;
use async_priority_channel as priority;
2021-10-10 10:04:40 +02:00
use miette::Result;
2021-08-19 10:31:29 +02:00
use tokio::sync::mpsc;
use watchexec::{
event::{Event, Priority, Tag},
2023-03-18 09:32:24 +01:00
signal,
2021-08-19 10:31:29 +02:00
};
2023-03-18 09:32:24 +01:00
use watchexec_signals::Signal;
2021-08-19 10:31:29 +02:00
// Run with: `env RUST_LOG=debug cargo run --example signal`,
// then issue some signals to the printed PID, or hit e.g. Ctrl-C.
// Send a SIGTERM (unix) or Ctrl-Break (windows) to exit.
#[tokio::main]
2021-10-10 10:04:40 +02:00
async fn main() -> Result<()> {
2021-08-19 10:31:29 +02:00
tracing_subscriber::fmt::init();
let (ev_s, ev_r) = priority::bounded::<Event, Priority>(1024);
2021-08-19 10:31:29 +02:00
let (er_s, mut er_r) = mpsc::channel(64);
tokio::spawn(async move {
while let Ok((event, priority)) = ev_r.recv().await {
tracing::info!("event {priority:?}: {event:?}");
2021-08-19 10:31:29 +02:00
2023-03-18 09:32:24 +01:00
if event.tags.contains(&Tag::Signal(Signal::Terminate)) {
2021-08-19 10:31:29 +02:00
exit(0);
}
}
});
tokio::spawn(async move {
while let Some(error) = er_r.recv().await {
tracing::error!("error: {error}");
2021-08-19 10:31:29 +02:00
}
});
tracing::info!("PID is {}", std::process::id());
2023-03-18 09:32:24 +01:00
signal::worker(er_s.clone(), ev_s.clone()).await?;
2021-08-19 10:31:29 +02:00
Ok(())
}