diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index 08225a9..e320e7f 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -28,6 +28,7 @@ use core::panic; use std::borrow::Borrow; use std::borrow::BorrowMut; use std::collections::HashMap; +use std::ops::DerefMut; use std::sync::Arc; use casual_logger::Log; @@ -1134,12 +1135,12 @@ impl Routine { botlog::trace( format!( - "[ROUTINE][Sent Gracefully Stopp Signal] {} in {}", + "[ROUTINE][Sent Gracefully Stop Signal] {} in {}", self_lock.name,self_lock.channel.0 ) .as_str(), Some(format!( - "Routine > start() > (In Tokio Spawn) > {:?}", + "Routine > stop() > {:?}", self_lock.module )), Some(&self_lock.parent_params.msg), @@ -1167,78 +1168,185 @@ impl Routine { // Err("NOT IMPLEMENTED".to_string()) } - pub async fn cancel(&self) -> Result - // [ ] => 03.27 - WIP - NOT IMPLEMENTED + pub async fn cancel(&mut self) -> Result + // [ ] => 03.27 - REVIEW FOR COMPLETION { // [ ] Likely calls abort() // Related : // https://docs.rs/tokio/latest/tokio/task/struct.JoinHandle.html#method.abort - - + let self_rw = Arc::new(RwLock::new(self)); let self_lock = self_rw.read().await; - + + + match &self_lock.join_handle { + None => return Err("No Join Handle on the Routine to Cancel".to_string()), + Some(a) => { + a.read().await.abort(); + { + let mut lock_mut = self_rw.write().await; + lock_mut.internal_signal = RoutineSignal::Stopped; + + } + } + } + botlog::trace( format!( - "[ERROR][Routine NOT IMPLEMENTED] {} in {}", + "[ROUTINE][Cancelled Routine] {} in {}", self_lock.name,self_lock.channel.0 ) .as_str(), Some(format!( - "Routine > start() > (In Tokio Spawn) > {:?}", + "Routine > cancel() > {:?}", self_lock.module )), Some(&self_lock.parent_params.msg), ); Log::flush(); - Err("NOT IMPLEMENTED".to_string()) + Ok("Cancelled Successfully".to_string()) + + + // botlog::trace( + // format!( + // "[ERROR][Routine NOT IMPLEMENTED] {} in {}", + // self_lock.name,self_lock.channel.0 + // ) + // .as_str(), + // Some(format!( + // "Routine > start() > (In Tokio Spawn) > {:?}", + // self_lock.module + // )), + // Some(&self_lock.parent_params.msg), + // ); + + // Log::flush(); + // Err("NOT IMPLEMENTED".to_string()) } pub async fn restart( - &self, - _force : bool + // &mut self, + self, + force : bool ) -> Result - // [ ] => 03.27 - WIP - NOT IMPLEMENTED + // [ ] => 03.27 - REVIEW FOR COMPLETION { // force flag aborts the routine immediately (like cancel()) let self_rw = Arc::new(RwLock::new(self)); - let self_lock = self_rw.read().await; + + if force + { + let mut self_lock = self_rw.write().await; + self_lock.cancel().await?; + } else { + let mut self_lock = self_rw.write().await; + self_lock.stop().await?; + } + + Routine::start(self_rw.clone()).await?; + let self_lock = self_rw.read().await; botlog::trace( format!( - "[ERROR][Routine NOT IMPLEMENTED] {} in {}", + "[ROUTINE][Restarted Routine] {} in {}", self_lock.name,self_lock.channel.0 ) .as_str(), Some(format!( - "Routine > start() > (In Tokio Spawn) > {:?}", + "Routine > restart() > {:?}", self_lock.module )), Some(&self_lock.parent_params.msg), ); Log::flush(); - Err("NOT IMPLEMENTED".to_string()) + Ok("Restarted successfully".to_string()) } - pub fn change_channel( - &self, - _channel : Channel + pub async fn change_channel( + &mut self, + channel : Channel ) -> Result - // [ ] => 03.27 - WIP - NOT IMPLEMENTED + // [ ] => 03.28 - REVIEW FOR COMPLETION { - // [ ] Think Ideally it should try to + // [x] Think Ideally it should try to // change the target channel of the // internal process too if possible? - Err("NOT IMPLEMENTED".to_string()) + self.channel = channel; + + + let self_rw = Arc::new(RwLock::new(self)); + + let self_lock = self_rw.read().await; + + botlog::trace( + format!( + "[ROUTINE][Change Channel] {} in {}", + self_lock.name,self_lock.channel.0 + ) + .as_str(), + Some(format!( + "Routine > restart() > {:?}", + self_lock.module + )), + Some(&self_lock.parent_params.msg), + ); + + Log::flush(); + + // Err("NOT IMPLEMENTED".to_string()) + Ok("Changed Successfully".to_string()) + } + + + pub async fn set_routine_attributes( + &mut self, + routine_attr : Vec + ) -> Result + // [ ] => 03.27 - WIP - NOT IMPLEMENTED + { + // This is way to custom set attributes first + // They will Be Validated before being applied + // IDEALLY the routine also be restarted afterwards externally + + Routine::validate_attr(&routine_attr).await?; + + + let self_rw = Arc::new(RwLock::new(self)); + { + let mut self_lock = self_rw.write().await; + self_lock.routine_attr = routine_attr; + } + + // let self_rw = Arc::new(RwLock::new(self)); + + let self_lock = self_rw.read().await; + + botlog::trace( + format!( + "[ROUTINE][Set Routine Attributes] {} in {}", + self_lock.name,self_lock.channel.0 + ) + .as_str(), + Some(format!( + "Routine > restart() > {:?}", + self_lock.module + )), + Some(&self_lock.parent_params.msg), + ); + + Log::flush(); + + + Ok("Changed Successfully".to_string()) }