mirror of
https://github.com/watchexec/watchexec.git
synced 2024-09-29 22:51:33 +02:00
Replace AtomicTakes with HandleLocks in action
This commit is contained in:
parent
b8ffa0c38a
commit
a651d149b0
@ -16,7 +16,7 @@ use crate::{
|
|||||||
command::Supervisor,
|
command::Supervisor,
|
||||||
error::{CriticalError, RuntimeError},
|
error::{CriticalError, RuntimeError},
|
||||||
event::Event,
|
event::Event,
|
||||||
handler::{rte, Handler},
|
handler::rte,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
@ -42,13 +42,6 @@ pub async fn worker(
|
|||||||
let mut set = Vec::new();
|
let mut set = Vec::new();
|
||||||
let mut process: Option<Supervisor> = None;
|
let mut process: Option<Supervisor> = None;
|
||||||
|
|
||||||
let mut action_handler =
|
|
||||||
{ working.borrow().action_handler.take() }.ok_or(CriticalError::MissingHandler)?;
|
|
||||||
let mut pre_spawn_handler =
|
|
||||||
{ working.borrow().pre_spawn_handler.take() }.ok_or(CriticalError::MissingHandler)?;
|
|
||||||
let mut post_spawn_handler =
|
|
||||||
{ working.borrow().post_spawn_handler.take() }.ok_or(CriticalError::MissingHandler)?;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let maxtime = if set.is_empty() {
|
let maxtime = if set.is_empty() {
|
||||||
trace!("nothing in set, waiting forever for next event");
|
trace!("nothing in set, waiting forever for next event");
|
||||||
@ -119,25 +112,16 @@ pub async fn worker(
|
|||||||
let action = Action::new(Arc::clone(&events));
|
let action = Action::new(Arc::clone(&events));
|
||||||
debug!(?action, "action constructed");
|
debug!(?action, "action constructed");
|
||||||
|
|
||||||
if let Some(h) = working.borrow().action_handler.take() {
|
|
||||||
trace!("action handler updated");
|
|
||||||
action_handler = h;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(h) = working.borrow().pre_spawn_handler.take() {
|
|
||||||
trace!("pre-spawn handler updated");
|
|
||||||
pre_spawn_handler = h;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(h) = working.borrow().post_spawn_handler.take() {
|
|
||||||
trace!("post-spawn handler updated");
|
|
||||||
post_spawn_handler = h;
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("running action handler");
|
debug!("running action handler");
|
||||||
|
let action_handler = {
|
||||||
|
let wrk = working.borrow();
|
||||||
|
wrk.action_handler.clone()
|
||||||
|
};
|
||||||
|
|
||||||
let outcome = action.outcome.clone();
|
let outcome = action.outcome.clone();
|
||||||
let err = action_handler
|
let err = action_handler
|
||||||
.handle(action)
|
.call(action)
|
||||||
|
.await
|
||||||
.map_err(|e| rte("action worker", e));
|
.map_err(|e| rte("action worker", e));
|
||||||
if let Err(err) = err {
|
if let Err(err) = err {
|
||||||
errors.send(err).await?;
|
errors.send(err).await?;
|
||||||
|
@ -4,14 +4,13 @@ use std::{
|
|||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use atomic_take::AtomicTake;
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
process::Command,
|
process::Command,
|
||||||
sync::{Mutex, OwnedMutexGuard},
|
sync::{Mutex, OwnedMutexGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{command::Shell, event::Event, filter::Filterer, handler::Handler};
|
use crate::{command::Shell, event::Event, filter::Filterer, handler::HandlerLock};
|
||||||
|
|
||||||
use super::Outcome;
|
use super::Outcome;
|
||||||
|
|
||||||
@ -43,7 +42,7 @@ pub struct WorkingData {
|
|||||||
/// It's useful to know that the handlers are updated from this working data before any of them
|
/// It's useful to know that the handlers are updated from this working data before any of them
|
||||||
/// run in any given cycle, so changing the pre-spawn and post-spawn handlers from this handler
|
/// run in any given cycle, so changing the pre-spawn and post-spawn handlers from this handler
|
||||||
/// will not affect the running action.
|
/// will not affect the running action.
|
||||||
pub action_handler: Arc<AtomicTake<Box<dyn Handler<Action> + Send>>>,
|
pub action_handler: HandlerLock<Action>,
|
||||||
|
|
||||||
/// A handler triggered before a command is spawned.
|
/// A handler triggered before a command is spawned.
|
||||||
///
|
///
|
||||||
@ -53,7 +52,7 @@ pub struct WorkingData {
|
|||||||
///
|
///
|
||||||
/// Returning an error from the handler will stop the action from processing further, and issue
|
/// Returning an error from the handler will stop the action from processing further, and issue
|
||||||
/// a [`RuntimeError`][crate::error::RuntimeError] to the error channel.
|
/// a [`RuntimeError`][crate::error::RuntimeError] to the error channel.
|
||||||
pub pre_spawn_handler: Arc<AtomicTake<Box<dyn Handler<PreSpawn> + Send>>>,
|
pub pre_spawn_handler: HandlerLock<PreSpawn>,
|
||||||
|
|
||||||
/// A handler triggered immediately after a command is spawned.
|
/// A handler triggered immediately after a command is spawned.
|
||||||
///
|
///
|
||||||
@ -63,7 +62,7 @@ pub struct WorkingData {
|
|||||||
/// Returning an error from the handler will drop the [`Child`][tokio::process::Child], which
|
/// Returning an error from the handler will drop the [`Child`][tokio::process::Child], which
|
||||||
/// will terminate the command without triggering any of the normal Watchexec behaviour, and
|
/// will terminate the command without triggering any of the normal Watchexec behaviour, and
|
||||||
/// issue a [`RuntimeError`][crate::error::RuntimeError] to the error channel.
|
/// issue a [`RuntimeError`][crate::error::RuntimeError] to the error channel.
|
||||||
pub post_spawn_handler: Arc<AtomicTake<Box<dyn Handler<PostSpawn> + Send>>>,
|
pub post_spawn_handler: HandlerLock<PostSpawn>,
|
||||||
|
|
||||||
/// Command to execute.
|
/// Command to execute.
|
||||||
///
|
///
|
||||||
@ -110,9 +109,9 @@ impl Default for WorkingData {
|
|||||||
Self {
|
Self {
|
||||||
// set to 50ms here, but will remain 100ms on cli until 2022
|
// set to 50ms here, but will remain 100ms on cli until 2022
|
||||||
throttle: Duration::from_millis(50),
|
throttle: Duration::from_millis(50),
|
||||||
action_handler: Arc::new(AtomicTake::new(Box::new(()) as _)),
|
action_handler: Default::default(),
|
||||||
pre_spawn_handler: Arc::new(AtomicTake::new(Box::new(()) as _)),
|
pre_spawn_handler: Default::default(),
|
||||||
post_spawn_handler: Arc::new(AtomicTake::new(Box::new(()) as _)),
|
post_spawn_handler: Default::default(),
|
||||||
command: Vec::new(),
|
command: Vec::new(),
|
||||||
shell: Shell::default(),
|
shell: Shell::default(),
|
||||||
grouped: true,
|
grouped: true,
|
||||||
|
@ -2,15 +2,13 @@
|
|||||||
|
|
||||||
use std::{fmt, path::Path, sync::Arc, time::Duration};
|
use std::{fmt, path::Path, sync::Arc, time::Duration};
|
||||||
|
|
||||||
use atomic_take::AtomicTake;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
action::{Action, PostSpawn, PreSpawn},
|
action::{Action, PostSpawn, PreSpawn},
|
||||||
command::Shell,
|
command::Shell,
|
||||||
error::RuntimeError,
|
error::RuntimeError,
|
||||||
filter::Filterer,
|
filter::Filterer,
|
||||||
fs::Watcher,
|
fs::Watcher,
|
||||||
handler::Handler,
|
handler::{Handler, HandlerLock},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Runtime configuration for [`Watchexec`][crate::Watchexec].
|
/// Runtime configuration for [`Watchexec`][crate::Watchexec].
|
||||||
@ -93,13 +91,13 @@ impl RuntimeConfig {
|
|||||||
|
|
||||||
/// Set the action handler.
|
/// Set the action handler.
|
||||||
pub fn on_action(&mut self, handler: impl Handler<Action> + Send + 'static) -> &mut Self {
|
pub fn on_action(&mut self, handler: impl Handler<Action> + Send + 'static) -> &mut Self {
|
||||||
self.action.action_handler = Arc::new(AtomicTake::new(Box::new(handler) as _));
|
self.action.action_handler = HandlerLock::new(Box::new(handler));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the pre-spawn handler.
|
/// Set the pre-spawn handler.
|
||||||
pub fn on_pre_spawn(&mut self, handler: impl Handler<PreSpawn> + Send + 'static) -> &mut Self {
|
pub fn on_pre_spawn(&mut self, handler: impl Handler<PreSpawn> + Send + 'static) -> &mut Self {
|
||||||
self.action.pre_spawn_handler = Arc::new(AtomicTake::new(Box::new(handler) as _));
|
self.action.pre_spawn_handler = HandlerLock::new(Box::new(handler));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +106,7 @@ impl RuntimeConfig {
|
|||||||
&mut self,
|
&mut self,
|
||||||
handler: impl Handler<PostSpawn> + Send + 'static,
|
handler: impl Handler<PostSpawn> + Send + 'static,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
self.action.post_spawn_handler = Arc::new(AtomicTake::new(Box::new(handler) as _));
|
self.action.post_spawn_handler = HandlerLock::new(Box::new(handler));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user