Split signal and fs examples

This commit is contained in:
Félix Saparelli 2021-08-19 20:31:29 +12:00
parent 6f3abdeaea
commit 319729582f
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
3 changed files with 53 additions and 17 deletions

View File

@ -1,14 +1,10 @@
use std::{process::exit, time::Duration};
use std::time::Duration;
use tokio::{
sync::{mpsc, watch},
time::sleep,
};
use watchexec::{
event::{Event, Particle},
fs,
signal::{self, Signal},
};
use watchexec::{event::Event, fs};
// Run with: `env RUST_LOG=debug cargo run --example fs`,
// then touch some files within the first 15 seconds, and afterwards.
@ -28,12 +24,6 @@ async fn main() -> color_eyre::eyre::Result<()> {
tokio::spawn(async move {
while let Some(e) = ev_r.recv().await {
tracing::info!("event: {:?}", e);
if e.particulars.contains(&Particle::Signal(Signal::Interrupt))
|| e.particulars.contains(&Particle::Signal(Signal::Terminate))
{
exit(0);
}
}
});
@ -43,8 +33,6 @@ async fn main() -> color_eyre::eyre::Result<()> {
}
});
tokio::spawn(signal::worker(er_s.clone(), ev_s.clone()));
let wd_sh = tokio::spawn(async move {
sleep(Duration::from_secs(15)).await;
wkd.pathset = Vec::new();

40
lib/examples/signal.rs Normal file
View File

@ -0,0 +1,40 @@
use std::process::exit;
use tokio::sync::mpsc;
use watchexec::{
event::{Event, Particle},
signal::{self, Signal},
};
// 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]
async fn main() -> color_eyre::eyre::Result<()> {
tracing_subscriber::fmt::init();
color_eyre::install()?;
let (ev_s, mut ev_r) = mpsc::channel::<Event>(1024);
let (er_s, mut er_r) = mpsc::channel(64);
tokio::spawn(async move {
while let Some(e) = ev_r.recv().await {
tracing::info!("event: {:?}", e);
if e.particulars.contains(&Particle::Signal(Signal::Terminate)) {
exit(0);
}
}
});
tokio::spawn(async move {
while let Some(e) = er_r.recv().await {
tracing::error!("{}", e);
}
});
tracing::info!("PID is {}", std::process::id());
signal::worker(er_s.clone(), ev_s.clone()).await?;
Ok(())
}

View File

@ -1,11 +1,19 @@
//! Watchexec: a library for utilities and programs which respond to events;
//! file changes, human interaction, and more.
//!
//! Also see the CLI tool: https://watchexec.github.io/
//! Also see the CLI tool: <https://watchexec.github.io/>
//!
//! This library is powered by [Tokio](https://tokio.rs), minimum version 1.10.
//! This library is powered by [Tokio](https://tokio.rs), minimum version 1.10. This requirement may
//! change (upwards) in the future without breaking change.
//!
//! The main way to use this crate involves constructing a [`Handler`] and running it.
//! The main way to use this crate involves constructing a [`Watchexec`] around a [`Config`] and
//! running it. The config may contain some instances of [`Handler`]s, which hook into watchexec
//! processing at various points.
//!
//! Alternatively, one can use the modules exposed by the crate and the external crates such as
//! [ClearScreen][clearscreen] and [Command Group][command_group] to build something more advanced,
//! at the cost of reimplementing the glue code. See the examples folder for some basic/demo tools
//! written with the individual modules.
//!
//! This crate does not itself use `unsafe`. However, it depends on a number of libraries which do,
//! most because they interact with the operating system.