From a651d149b041da65f2ce96c9c1ae02722c0a13b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Sat, 29 Jan 2022 01:57:28 +1300 Subject: [PATCH] Replace AtomicTakes with HandleLocks in action --- lib/src/action.rs | 32 ++++++++------------------------ lib/src/action/workingdata.rs | 15 +++++++-------- lib/src/config.rs | 10 ++++------ 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/lib/src/action.rs b/lib/src/action.rs index a3da0a6..7507f5c 100644 --- a/lib/src/action.rs +++ b/lib/src/action.rs @@ -16,7 +16,7 @@ use crate::{ command::Supervisor, error::{CriticalError, RuntimeError}, event::Event, - handler::{rte, Handler}, + handler::rte, }; #[doc(inline)] @@ -42,13 +42,6 @@ pub async fn worker( let mut set = Vec::new(); let mut process: Option = 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 { let maxtime = if set.is_empty() { trace!("nothing in set, waiting forever for next event"); @@ -119,25 +112,16 @@ pub async fn worker( let action = Action::new(Arc::clone(&events)); 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"); + let action_handler = { + let wrk = working.borrow(); + wrk.action_handler.clone() + }; + let outcome = action.outcome.clone(); let err = action_handler - .handle(action) + .call(action) + .await .map_err(|e| rte("action worker", e)); if let Err(err) = err { errors.send(err).await?; diff --git a/lib/src/action/workingdata.rs b/lib/src/action/workingdata.rs index c63da6e..7f8a9b5 100644 --- a/lib/src/action/workingdata.rs +++ b/lib/src/action/workingdata.rs @@ -4,14 +4,13 @@ use std::{ time::Duration, }; -use atomic_take::AtomicTake; use once_cell::sync::OnceCell; use tokio::{ process::Command, 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; @@ -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 /// run in any given cycle, so changing the pre-spawn and post-spawn handlers from this handler /// will not affect the running action. - pub action_handler: Arc + Send>>>, + pub action_handler: HandlerLock, /// 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 /// a [`RuntimeError`][crate::error::RuntimeError] to the error channel. - pub pre_spawn_handler: Arc + Send>>>, + pub pre_spawn_handler: HandlerLock, /// 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 /// will terminate the command without triggering any of the normal Watchexec behaviour, and /// issue a [`RuntimeError`][crate::error::RuntimeError] to the error channel. - pub post_spawn_handler: Arc + Send>>>, + pub post_spawn_handler: HandlerLock, /// Command to execute. /// @@ -110,9 +109,9 @@ impl Default for WorkingData { Self { // set to 50ms here, but will remain 100ms on cli until 2022 throttle: Duration::from_millis(50), - action_handler: Arc::new(AtomicTake::new(Box::new(()) as _)), - pre_spawn_handler: Arc::new(AtomicTake::new(Box::new(()) as _)), - post_spawn_handler: Arc::new(AtomicTake::new(Box::new(()) as _)), + action_handler: Default::default(), + pre_spawn_handler: Default::default(), + post_spawn_handler: Default::default(), command: Vec::new(), shell: Shell::default(), grouped: true, diff --git a/lib/src/config.rs b/lib/src/config.rs index 6171574..7b6830c 100644 --- a/lib/src/config.rs +++ b/lib/src/config.rs @@ -2,15 +2,13 @@ use std::{fmt, path::Path, sync::Arc, time::Duration}; -use atomic_take::AtomicTake; - use crate::{ action::{Action, PostSpawn, PreSpawn}, command::Shell, error::RuntimeError, filter::Filterer, fs::Watcher, - handler::Handler, + handler::{Handler, HandlerLock}, }; /// Runtime configuration for [`Watchexec`][crate::Watchexec]. @@ -93,13 +91,13 @@ impl RuntimeConfig { /// Set the action handler. pub fn on_action(&mut self, handler: impl Handler + 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 } /// Set the pre-spawn handler. pub fn on_pre_spawn(&mut self, handler: impl Handler + 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 } @@ -108,7 +106,7 @@ impl RuntimeConfig { &mut self, handler: impl Handler + Send + 'static, ) -> &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 } }