From 3f8e798050928229a00835bab807e8197c511915 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Wed, 27 Mar 2024 01:29:25 -0400 Subject: [PATCH] (cont) routine start --- src/core/bot_actions.rs | 25 +++--- src/core/botinstance.rs | 10 +-- src/core/botmodules.rs | 99 +++++++++++++++++------- src/core/chat.rs | 2 +- src/custom/experimental/experiment003.rs | 78 ++++++++++++++++--- 5 files changed, 160 insertions(+), 54 deletions(-) diff --git a/src/core/bot_actions.rs b/src/core/bot_actions.rs index 8b88c2a..a88e5de 100644 --- a/src/core/bot_actions.rs +++ b/src/core/bot_actions.rs @@ -15,17 +15,19 @@ pub type ActAR = Arc>; pub struct ExecBodyParams { pub bot : BotAR, pub msg : PrivmsgMessage, - pub parent_act : ActAR , + // pub parent_act : ActAR , + pub curr_act : ActAR , } impl ExecBodyParams { // pub async fn get_parent_module(&self) -> Option { - pub async fn get_parent_module(&self) -> BotModule { + // pub async fn get_parent_module(&self) -> BotModule { + pub async fn get_module(&self) -> BotModule { - let parent_act = Arc::clone(&self.parent_act); - let parent_act_lock = parent_act.read().await; + let curr_act = Arc::clone(&self.curr_act); + let parent_act_lock = curr_act.read().await; let act = &(*parent_act_lock); match act { BotAction::C(c) => { @@ -41,36 +43,37 @@ impl ExecBodyParams { BotAction::R(r) => { // let temp = r.module.clone(); // Some(temp) - r.module.clone() + r.read().await.module.clone() } // _ => None } } - pub async fn get_routine_channel(&self) -> Option { + pub async fn get_channel(&self) -> Option { // THIS IS INCORRECT - BELOW MAY BE PULLING THE PARENT BOTACTION // NOT THE CURRENT BOT ACTION - let parent_act = Arc::clone(&self.parent_act); - let parent_act_lock = parent_act.read().await; + + let curr_act = Arc::clone(&self.curr_act); + let parent_act_lock = curr_act.read().await; let act = &(*parent_act_lock); match act { BotAction::C(_) => { // let temp = c.module.clone(); // Some(temp) - None + Some(Channel(self.msg.channel_login.clone())) }, BotAction::L(_) => { // let temp = l.module.clone(); // Some(temp) // l.module.clone() - None + Some(Channel(self.msg.channel_login.clone())) }, BotAction::R(r) => { // let temp = r.module.clone(); // Some(temp) - Some(r.channel.clone()) + Some(r.read().await.channel.clone()) } // _ => None } diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index 07b481d..563856f 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -404,7 +404,7 @@ impl BotInstance { let params = ExecBodyParams { bot : Arc::clone(&bot), msg : (*msg).clone(), - parent_act : Arc::clone(&act_clone), + curr_act : Arc::clone(&act_clone), }; // When sending a BotMsgTypeNotif, send_botmsg does Roles related validation as required @@ -461,7 +461,7 @@ impl BotInstance { let params = ExecBodyParams { bot : Arc::clone(&bot), msg : (*msg).clone(), - parent_act : Arc::clone(&act_clone), + curr_act : Arc::clone(&act_clone), }; @@ -491,7 +491,7 @@ impl BotInstance { let params = ExecBodyParams { bot : Arc::clone(&bot), msg : (*msg).clone(), - parent_act : Arc::clone(&act_clone), + curr_act : Arc::clone(&act_clone), }; @@ -516,7 +516,7 @@ impl BotInstance { c.execute(ExecBodyParams { bot : a, msg : msg.clone() , - parent_act : Arc::clone(&act_clone), + curr_act : Arc::clone(&act_clone), }).await; botlog::trace( @@ -564,7 +564,7 @@ impl BotInstance { l.execute(ExecBodyParams { bot : a, msg : msg.clone() , - parent_act : Arc::clone(&act_clone), + curr_act : Arc::clone(&act_clone), } ).await; } diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index f327266..34c6b02 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -469,7 +469,8 @@ pub enum StatusType { pub enum BotAction { C(BotCommand), L(Listener), - R(Routine), + // R(Routine), + R(Arc>), } impl BotAction { @@ -621,7 +622,7 @@ pub struct Routine { exec_body: bot_actions::actions_util::ExecBody, // parent_params : Option , parent_params : ExecBodyParams , - join_handle : Option> , + pub join_handle : Option> , start_time : Option> , complete_iterations : i64 , remaining_iterations : Option , @@ -641,7 +642,11 @@ impl Routine { // parent_params : Option parent_params : ExecBodyParams // ) -> Result { - ) -> Result { + // ) -> Result { + ) -> Result< + Arc>, + String + > { // [ ] Validation is made against parent_params // to ensure those params don't conflict @@ -650,7 +655,7 @@ impl Routine { // parent_params.unwrap(). if routine_attr.contains(&RoutineAttr::RunOnce) { - return Ok(Routine { + return Ok(Arc::new(RwLock::new(Routine { name , module , channel , @@ -661,7 +666,7 @@ impl Routine { complete_iterations : 0 , remaining_iterations : None , routine_attr : routine_attr - }) ; + }))) ; } @@ -679,57 +684,80 @@ impl Routine { Err("NOT IMPLEMENTED".to_string()) } - pub async fn start(self) -> Result + // pub async fn start(self) -> Result + // pub async fn start(self : &mut Self) -> Result + pub async fn start( + trg_routine_ar : Arc> + ) -> Result { // [ ] Asyncio Spawn likely around here // [ ] & Assigns self.join_handle + // let self_rw = Arc::new(RwLock::new(self)); + // let mut self_wr_lock = self_rw.write().await; + // let self_ar = Arc::new(RwLock::new(trg_routine_ar)); - let self_rw = Arc::new(RwLock::new(self)); - let mut self_wr_lock = self_rw.write().await; - if self_wr_lock.routine_attr.contains(&RoutineAttr::RunOnce) { - self_wr_lock.remaining_iterations = Some(1); + if trg_routine_ar.read().await.routine_attr.contains(&RoutineAttr::RunOnce) { + // if self_wr_lock.routine_attr.contains(&RoutineAttr::RunOnce) { + // let = self_wr_lock.remaining_iterations + + // let mut self_wr_lock = self_rw.write().await; + + + // let self_rw_copy = Arc::new(self); + // let self_rw_copy = Arc::clone(&self_rw); + // let mut a = *(*self_wr_lock); + // a.remaining_iterations = Some(1); + // (*self_wr_lock).remaining_iterations = Some(1); + // let routine = *self_rw_copy.read().await; + + // fn innerhelper(self_rw : Arc>) -> Option> { + // fn innerhelper(self_rw : Arc>) -> Option> { + // fn innerhelper(inroutine : &Routine) -> Option> { + // fn innerhelper(inroutine : Routine) -> Option> { + fn innerhelper(inroutine : Arc>) -> Option> { - fn innerhelper(self_rw : Arc>) -> Option> { Some( tokio::spawn(async move { - let mut self_wr_lock = self_rw.write().await; - let mut remainingiter = self_wr_lock.remaining_iterations.unwrap(); + // let mut self_wr_lock = self_rw.write().await; + // let mut remainingiter = self_wr_lock.remaining_iterations.unwrap(); + let mut remainingiter = inroutine.write().await.remaining_iterations.unwrap_or(0); botlog::trace( format!( "[TRACE][Routine Started] {} in {}", - self_wr_lock.name,self_wr_lock.channel.0 + inroutine.read().await.name, + inroutine.read().await.channel.0 ) .as_str(), Some(format!( "Routine > start() > (In Tokio Spawn) > {:?}", - self_wr_lock.module + inroutine.read().await.module )), - Some(&self_wr_lock.parent_params.msg), + Some(&inroutine.read().await.parent_params.msg), ); - self_wr_lock.start_time = Some(chrono::offset::Local::now()); + inroutine.write().await.start_time = Some(chrono::offset::Local::now()); // Loop iteration while remainingiter > 1 { // execution body - self_wr_lock.loopbody().await; + inroutine.read().await.loopbody().await; // end of loop iteration remainingiter -= 1; { - self_wr_lock.remaining_iterations = Some(remainingiter); - self_wr_lock.complete_iterations += 1; + inroutine.write().await.remaining_iterations = Some(remainingiter); + inroutine.write().await.complete_iterations += 1; } } @@ -737,14 +765,15 @@ impl Routine { botlog::trace( format!( "[TRACE][Routine Completed] {} in {}", - self_wr_lock.name,self_wr_lock.channel.0 + inroutine.read().await.name, + inroutine.read().await.channel.0 ) .as_str(), Some(format!( "Routine > start() > (In Tokio Spawn) > {:?}", - self_wr_lock.module + inroutine.read().await.module )), - Some(&self_wr_lock.parent_params.msg), + Some(&inroutine.read().await.parent_params.msg), ); @@ -752,7 +781,20 @@ impl Routine { })) } - self_wr_lock.join_handle = innerhelper(self_rw.clone()) ; + + // let self_rw = Arc::new(RwLock::new(self)); + // let mut self_wr_lock = self_rw.write().await; + + // self_wr_lock.join_handle = innerhelper(&routine) ; + + // let mut routine = self_rw_copy.write().await; + // (**routine).join_handle = innerhelper(*routine); + // routine.join_handle = innerhelper(*routine); + // let routine = *self_rw_copy.read().await; + // self_rw_copy.write().await.join_handle = innerhelper(&routine); + // let self_ref = Arc::clone(&trg_routine_ar); + trg_routine_ar.write().await.join_handle = innerhelper(trg_routine_ar.clone()); + return Ok("Successfully Started Routine".to_string()); } @@ -762,14 +804,17 @@ impl Routine { botlog::trace( format!( "[ERROR][Routine NOT IMPLEMENTED] {} in {}", - self_wr_lock.name,self_wr_lock.channel.0 + // self_wr_lock.name,self_wr_lock.channel.0 + trg_routine_ar.read().await.name, + trg_routine_ar.read().await.channel.0 ) .as_str(), Some(format!( "Routine > start() > (In Tokio Spawn) > {:?}", - self_wr_lock.module + // self_wr_lock.module + trg_routine_ar.read().await.module )), - Some(&self_wr_lock.parent_params.msg), + Some(&trg_routine_ar.read().await.parent_params.msg), ); Log::flush(); diff --git a/src/core/chat.rs b/src/core/chat.rs index d0b85e6..a83bd8c 100644 --- a/src/core/chat.rs +++ b/src/core/chat.rs @@ -101,7 +101,7 @@ impl Chat { Some(¶ms.msg), ); - let parent_module = params.get_parent_module().await; + let parent_module = params.get_module().await; let params_clone = params.clone(); let botclone = Arc::clone(¶ms_clone.bot); diff --git a/src/custom/experimental/experiment003.rs b/src/custom/experimental/experiment003.rs index 0bb98b9..1e25591 100644 --- a/src/custom/experimental/experiment003.rs +++ b/src/custom/experimental/experiment003.rs @@ -16,6 +16,7 @@ const OF_CMD_CHANNEL:Channel = Channel(String::new()); use casual_logger::Log; use rand::Rng; +use tokio::sync::RwLock; use std::sync::Arc; use crate::core::bot_actions::ExecBodyParams; @@ -71,13 +72,19 @@ async fn test3_body(params : ExecBodyParams) { // [x] Get the module from params - let parentmodule = params.get_parent_module().await; - let channel = params.get_routine_channel().await; + // let parentmodule = params.get_parent_module().await; + let module = params.get_module().await; + let channel = params.get_channel().await; let routine_attr = vec![ RoutineAttr::RunOnce ]; - let exec_body = actions_util::asyncbox(rtestbody); - let parent_params = params.clone(); + // let exec_body = actions_util::asyncbox(rtestbody); + let exec_body = actions_util::asyncbox(rtestbody); // <-- 03.27 - when below is uncommented, this is throwing an issue + + + // let parent_params = params.clone(); + // let params_clone = params.clone(); + async fn rtestbody(params : ExecBodyParams) { @@ -94,19 +101,64 @@ async fn test3_body(params : ExecBodyParams) { String::from("Inner Routine Loop Message"), params.clone() ).await; + + let logmsg_botact = match *params.curr_act.read().await { + BotAction::C(_) => "command", + BotAction::R(_) => "routine", + BotAction::L(_) => "listener", + } ; + + botlog::trace( + // format!("Params > Curr_act : {:?}", params.curr_act).as_str(), // BotAction doesn't imblement debug + format!("Params > Curr_act type : {:?}", logmsg_botact).as_str(), + Some("Experiments003 > test3 command body".to_string()), + Some(¶ms.msg), + ); + Log::flush(); } + botlog::debug( + format!("TEST3_BODY BEFORE FROM CALL : module - {:?} ; channel - {:?}", + module,channel + ).as_str(), + Some("experiment003 > test3_body".to_string()), + Some(¶ms.msg), + ); + + + let a = Routine::from( "Routine Test".to_string(), - parentmodule, + module, channel.unwrap(), routine_attr, exec_body, - parent_params + params.clone() ); + + if let Ok(newr) = a { - let rslt = newr.start().await; + + // [ ] before execute , be sure to adjust curr_act + + let mut params_mut = params.clone(); + // let newr_ar = Arc::new(RwLock::new(newr)); + let newr_ar = newr.clone(); + + params_mut.curr_act = Arc::new(RwLock::new( + BotAction::R(newr_ar.clone()) + )); + + // let a = newr_ar.read().await; + // let rslt = (&*a).start().await; + + // let mut newr_lock = newr_ar.write().await; + + // let rslt = newr_ar.write().await.start().await; + let rslt = Routine::start(newr_ar.clone()).await; + + // let rslt = newr_ar.read().await.start().await; botlog::debug( @@ -117,7 +169,8 @@ async fn test3_body(params : ExecBodyParams) { Some(¶ms.msg), ); - + Log::flush(); + let bot = Arc::clone(¶ms.bot); let botlock = bot.read().await; @@ -132,13 +185,18 @@ async fn test3_body(params : ExecBodyParams) { params.clone() ).await; + newr.clone().write().await.join_handle.take().expect("Issue with join handle").await.unwrap(); + + // newr.read().await.join_handle.unwrap().await.unwrap(); + + } - - Log::flush(); + + }