(cont) routine start
All checks were successful
ci/woodpecker/pr/cargo-checks Pipeline was successful

This commit is contained in:
ModulatingForce 2024-03-27 01:29:25 -04:00
parent 5249c3af25
commit 3f8e798050
5 changed files with 160 additions and 54 deletions

View file

@ -15,17 +15,19 @@ pub type ActAR = Arc<RwLock<BotAction>>;
pub struct ExecBodyParams { pub struct ExecBodyParams {
pub bot : BotAR, pub bot : BotAR,
pub msg : PrivmsgMessage, pub msg : PrivmsgMessage,
pub parent_act : ActAR , // pub parent_act : ActAR ,
pub curr_act : ActAR ,
} }
impl ExecBodyParams { impl ExecBodyParams {
// pub async fn get_parent_module(&self) -> Option<BotModule> { // pub async fn get_parent_module(&self) -> Option<BotModule> {
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 curr_act = Arc::clone(&self.curr_act);
let parent_act_lock = parent_act.read().await; let parent_act_lock = curr_act.read().await;
let act = &(*parent_act_lock); let act = &(*parent_act_lock);
match act { match act {
BotAction::C(c) => { BotAction::C(c) => {
@ -41,36 +43,37 @@ impl ExecBodyParams {
BotAction::R(r) => { BotAction::R(r) => {
// let temp = r.module.clone(); // let temp = r.module.clone();
// Some(temp) // Some(temp)
r.module.clone() r.read().await.module.clone()
} }
// _ => None // _ => None
} }
} }
pub async fn get_routine_channel(&self) -> Option<Channel> { pub async fn get_channel(&self) -> Option<Channel> {
// THIS IS INCORRECT - BELOW MAY BE PULLING THE PARENT BOTACTION // THIS IS INCORRECT - BELOW MAY BE PULLING THE PARENT BOTACTION
// NOT THE CURRENT BOT ACTION // 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); let act = &(*parent_act_lock);
match act { match act {
BotAction::C(_) => { BotAction::C(_) => {
// let temp = c.module.clone(); // let temp = c.module.clone();
// Some(temp) // Some(temp)
None Some(Channel(self.msg.channel_login.clone()))
}, },
BotAction::L(_) => { BotAction::L(_) => {
// let temp = l.module.clone(); // let temp = l.module.clone();
// Some(temp) // Some(temp)
// l.module.clone() // l.module.clone()
None Some(Channel(self.msg.channel_login.clone()))
}, },
BotAction::R(r) => { BotAction::R(r) => {
// let temp = r.module.clone(); // let temp = r.module.clone();
// Some(temp) // Some(temp)
Some(r.channel.clone()) Some(r.read().await.channel.clone())
} }
// _ => None // _ => None
} }

View file

@ -404,7 +404,7 @@ impl BotInstance {
let params = ExecBodyParams { let params = ExecBodyParams {
bot : Arc::clone(&bot), bot : Arc::clone(&bot),
msg : (*msg).clone(), 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 // When sending a BotMsgTypeNotif, send_botmsg does Roles related validation as required
@ -461,7 +461,7 @@ impl BotInstance {
let params = ExecBodyParams { let params = ExecBodyParams {
bot : Arc::clone(&bot), bot : Arc::clone(&bot),
msg : (*msg).clone(), msg : (*msg).clone(),
parent_act : Arc::clone(&act_clone), curr_act : Arc::clone(&act_clone),
}; };
@ -491,7 +491,7 @@ impl BotInstance {
let params = ExecBodyParams { let params = ExecBodyParams {
bot : Arc::clone(&bot), bot : Arc::clone(&bot),
msg : (*msg).clone(), msg : (*msg).clone(),
parent_act : Arc::clone(&act_clone), curr_act : Arc::clone(&act_clone),
}; };
@ -516,7 +516,7 @@ impl BotInstance {
c.execute(ExecBodyParams { c.execute(ExecBodyParams {
bot : a, bot : a,
msg : msg.clone() , msg : msg.clone() ,
parent_act : Arc::clone(&act_clone), curr_act : Arc::clone(&act_clone),
}).await; }).await;
botlog::trace( botlog::trace(
@ -564,7 +564,7 @@ impl BotInstance {
l.execute(ExecBodyParams { l.execute(ExecBodyParams {
bot : a, bot : a,
msg : msg.clone() , msg : msg.clone() ,
parent_act : Arc::clone(&act_clone), curr_act : Arc::clone(&act_clone),
} ).await; } ).await;
} }

View file

@ -469,7 +469,8 @@ pub enum StatusType {
pub enum BotAction { pub enum BotAction {
C(BotCommand), C(BotCommand),
L(Listener), L(Listener),
R(Routine), // R(Routine),
R(Arc<RwLock<Routine>>),
} }
impl BotAction { impl BotAction {
@ -621,7 +622,7 @@ pub struct Routine {
exec_body: bot_actions::actions_util::ExecBody, exec_body: bot_actions::actions_util::ExecBody,
// parent_params : Option<ExecBodyParams> , // parent_params : Option<ExecBodyParams> ,
parent_params : ExecBodyParams , parent_params : ExecBodyParams ,
join_handle : Option<JoinHandle<()>> , pub join_handle : Option<JoinHandle<()>> ,
start_time : Option<DateTime<Local>> , start_time : Option<DateTime<Local>> ,
complete_iterations : i64 , complete_iterations : i64 ,
remaining_iterations : Option<i64> , remaining_iterations : Option<i64> ,
@ -641,7 +642,11 @@ impl Routine {
// parent_params : Option<ExecBodyParams> // parent_params : Option<ExecBodyParams>
parent_params : ExecBodyParams parent_params : ExecBodyParams
// ) -> Result<String,String> { // ) -> Result<String,String> {
) -> Result<Routine,String> { // ) -> Result<Routine,String> {
) -> Result<
Arc<RwLock<Routine>>,
String
> {
// [ ] Validation is made against parent_params // [ ] Validation is made against parent_params
// to ensure those params don't conflict // to ensure those params don't conflict
@ -650,7 +655,7 @@ impl Routine {
// parent_params.unwrap(). // parent_params.unwrap().
if routine_attr.contains(&RoutineAttr::RunOnce) { if routine_attr.contains(&RoutineAttr::RunOnce) {
return Ok(Routine { return Ok(Arc::new(RwLock::new(Routine {
name , name ,
module , module ,
channel , channel ,
@ -661,7 +666,7 @@ impl Routine {
complete_iterations : 0 , complete_iterations : 0 ,
remaining_iterations : None , remaining_iterations : None ,
routine_attr : routine_attr routine_attr : routine_attr
}) ; }))) ;
} }
@ -679,57 +684,80 @@ impl Routine {
Err("NOT IMPLEMENTED".to_string()) Err("NOT IMPLEMENTED".to_string())
} }
pub async fn start(self) -> Result<String,String> // pub async fn start(self) -> Result<String,String>
// pub async fn start(self : &mut Self) -> Result<String,String>
pub async fn start(
trg_routine_ar : Arc<RwLock<Routine>>
) -> Result<String,String>
{ {
// [ ] Asyncio Spawn likely around here // [ ] Asyncio Spawn likely around here
// [ ] & Assigns self.join_handle // [ ] & 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) { if trg_routine_ar.read().await.routine_attr.contains(&RoutineAttr::RunOnce) {
self_wr_lock.remaining_iterations = Some(1); // 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<RwLock<&Routine>>) -> Option<JoinHandle<()>> {
// fn innerhelper(self_rw : Arc<RwLock<&Routine>>) -> Option<JoinHandle<()>> {
// fn innerhelper(inroutine : &Routine) -> Option<JoinHandle<()>> {
// fn innerhelper(inroutine : Routine) -> Option<JoinHandle<()>> {
fn innerhelper(inroutine : Arc<RwLock<Routine>>) -> Option<JoinHandle<()>> {
fn innerhelper(self_rw : Arc<RwLock<Routine>>) -> Option<JoinHandle<()>> {
Some( Some(
tokio::spawn(async move { tokio::spawn(async move {
let mut self_wr_lock = self_rw.write().await; // let mut self_wr_lock = self_rw.write().await;
let mut remainingiter = self_wr_lock.remaining_iterations.unwrap(); // let mut remainingiter = self_wr_lock.remaining_iterations.unwrap();
let mut remainingiter = inroutine.write().await.remaining_iterations.unwrap_or(0);
botlog::trace( botlog::trace(
format!( format!(
"[TRACE][Routine Started] {} in {}", "[TRACE][Routine Started] {} in {}",
self_wr_lock.name,self_wr_lock.channel.0 inroutine.read().await.name,
inroutine.read().await.channel.0
) )
.as_str(), .as_str(),
Some(format!( Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}", "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 // Loop iteration
while remainingiter > 1 { while remainingiter > 1 {
// execution body // execution body
self_wr_lock.loopbody().await; inroutine.read().await.loopbody().await;
// end of loop iteration // end of loop iteration
remainingiter -= 1; remainingiter -= 1;
{ {
self_wr_lock.remaining_iterations = Some(remainingiter); inroutine.write().await.remaining_iterations = Some(remainingiter);
self_wr_lock.complete_iterations += 1; inroutine.write().await.complete_iterations += 1;
} }
} }
@ -737,14 +765,15 @@ impl Routine {
botlog::trace( botlog::trace(
format!( format!(
"[TRACE][Routine Completed] {} in {}", "[TRACE][Routine Completed] {} in {}",
self_wr_lock.name,self_wr_lock.channel.0 inroutine.read().await.name,
inroutine.read().await.channel.0
) )
.as_str(), .as_str(),
Some(format!( Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}", "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()); return Ok("Successfully Started Routine".to_string());
} }
@ -762,14 +804,17 @@ impl Routine {
botlog::trace( botlog::trace(
format!( format!(
"[ERROR][Routine NOT IMPLEMENTED] {} in {}", "[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(), .as_str(),
Some(format!( Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}", "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(); Log::flush();

View file

@ -101,7 +101,7 @@ impl Chat {
Some(&params.msg), Some(&params.msg),
); );
let parent_module = params.get_parent_module().await; let parent_module = params.get_module().await;
let params_clone = params.clone(); let params_clone = params.clone();
let botclone = Arc::clone(&params_clone.bot); let botclone = Arc::clone(&params_clone.bot);

View file

@ -16,6 +16,7 @@ const OF_CMD_CHANNEL:Channel = Channel(String::new());
use casual_logger::Log; use casual_logger::Log;
use rand::Rng; use rand::Rng;
use tokio::sync::RwLock;
use std::sync::Arc; use std::sync::Arc;
use crate::core::bot_actions::ExecBodyParams; use crate::core::bot_actions::ExecBodyParams;
@ -71,13 +72,19 @@ async fn test3_body(params : ExecBodyParams) {
// [x] Get the module from params // [x] Get the module from params
let parentmodule = params.get_parent_module().await; // let parentmodule = params.get_parent_module().await;
let channel = params.get_routine_channel().await; let module = params.get_module().await;
let channel = params.get_channel().await;
let routine_attr = vec![ let routine_attr = vec![
RoutineAttr::RunOnce RoutineAttr::RunOnce
]; ];
let exec_body = actions_util::asyncbox(rtestbody); // let exec_body = actions_util::asyncbox(rtestbody);
let parent_params = params.clone(); 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) { async fn rtestbody(params : ExecBodyParams) {
@ -94,19 +101,64 @@ async fn test3_body(params : ExecBodyParams) {
String::from("Inner Routine Loop Message"), String::from("Inner Routine Loop Message"),
params.clone() params.clone()
).await; ).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(&params.msg),
);
Log::flush();
} }
botlog::debug(
format!("TEST3_BODY BEFORE FROM CALL : module - {:?} ; channel - {:?}",
module,channel
).as_str(),
Some("experiment003 > test3_body".to_string()),
Some(&params.msg),
);
let a = Routine::from( let a = Routine::from(
"Routine Test".to_string(), "Routine Test".to_string(),
parentmodule, module,
channel.unwrap(), channel.unwrap(),
routine_attr, routine_attr,
exec_body, exec_body,
parent_params params.clone()
); );
if let Ok(newr) = a { 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( botlog::debug(
@ -117,6 +169,7 @@ async fn test3_body(params : ExecBodyParams) {
Some(&params.msg), Some(&params.msg),
); );
Log::flush();
let bot = Arc::clone(&params.bot); let bot = Arc::clone(&params.bot);
@ -132,13 +185,18 @@ async fn test3_body(params : ExecBodyParams) {
params.clone() params.clone()
).await; ).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(); Log::flush();
} }