ExecBody & routine addl references

This commit is contained in:
ModulatingForce 2024-03-28 04:39:03 -04:00
parent ca9361cc93
commit 2a1a7f8503
4 changed files with 74 additions and 26 deletions

View file

@ -16,7 +16,7 @@ pub type RoutineAR = Arc<RwLock<Routine>>;
pub struct ExecBodyParams {
pub bot : BotAR,
pub msg : PrivmsgMessage,
// pub parent_act : ActAR ,
pub parent_act : Option<ActAR> ,
pub curr_act : ActAR ,
}

View file

@ -404,6 +404,7 @@ impl BotInstance {
let params = ExecBodyParams {
bot : Arc::clone(&bot),
msg : (*msg).clone(),
parent_act : None,
curr_act : Arc::clone(&act_clone),
};
@ -461,6 +462,7 @@ impl BotInstance {
let params = ExecBodyParams {
bot : Arc::clone(&bot),
msg : (*msg).clone(),
parent_act : None,
curr_act : Arc::clone(&act_clone),
};
@ -491,6 +493,7 @@ impl BotInstance {
let params = ExecBodyParams {
bot : Arc::clone(&bot),
msg : (*msg).clone(),
parent_act : None,
curr_act : Arc::clone(&act_clone),
};
@ -516,6 +519,7 @@ impl BotInstance {
c.execute(ExecBodyParams {
bot : a,
msg : msg.clone() ,
parent_act : None,
curr_act : Arc::clone(&act_clone),
}).await;
@ -564,6 +568,7 @@ impl BotInstance {
l.execute(ExecBodyParams {
bot : a,
msg : msg.clone() ,
parent_act : None,
curr_act : Arc::clone(&act_clone),
} ).await;
}

View file

@ -637,12 +637,39 @@ pub struct Routine {
remaining_iterations : Option<i64> ,
routine_attr : Vec<RoutineAttr> ,
pub internal_signal : RoutineSignal ,
pub self_routine_ar : Option<RoutineAR> ,
pub self_act_ar : Option<ActAR> ,
}
impl Routine {
// pub fn set
// pub async fn refresh_self_ref(self) {
pub async fn refresh_routine_internal(routine_ar : RoutineAR)
{
/*
Execute after the Routine is constructed
- If not, a start() will also call this
*/
// 1. Update the self reference to itself
let mut mut_lock = routine_ar.write().await;
mut_lock.self_routine_ar = Some(routine_ar.clone());
// 2. Update the current self_act_ar
mut_lock.self_act_ar = Some(Arc::new(RwLock::new(BotAction::R(routine_ar.clone()))));
}
pub async fn validate_attr(routine_attr : &Vec<RoutineAttr>)
-> Result<String,String>
@ -819,6 +846,8 @@ impl Routine {
remaining_iterations : None ,
routine_attr : routine_attr ,
internal_signal : RoutineSignal::NotStarted ,
self_routine_ar : None ,
self_act_ar : None ,
}))) ;
@ -831,6 +860,10 @@ impl Routine {
// [ ] => 03.27 - REVIEW FOR COMPLETION
{
// [x] Prep by updating it's own self reference
Routine::refresh_routine_internal(trg_routine_ar.clone()).await;
// [x] Asyncio Spawn likely around here
// [x] & Assigns self.join_handle
@ -1005,7 +1038,8 @@ impl Routine {
// [x] execution body
trg_routine_ar.read().await.loopbody().await;
// trg_routine_ar.read().await.loopbody().await;
trg_routine_ar.write().await.loopbody().await;
{ // [x] End of Loop iteration
@ -1100,7 +1134,7 @@ impl Routine {
}
async fn loopbody(&self)
async fn loopbody(&mut self)
// [x] => 03.27 - COMPLETED
{
botlog::trace(
@ -1113,9 +1147,22 @@ impl Routine {
Log::flush();
(self.exec_body)(
self.parent_params.clone()
let self_ar = Arc::new(RwLock::new(self));
{
let mut mutlock = self_ar.write().await;
mutlock.parent_params.parent_act = Some(mutlock.parent_params.curr_act.clone());
mutlock.parent_params.curr_act = mutlock.self_act_ar.to_owned().unwrap();
}
(self_ar.read().await.exec_body)(
self_ar.read().await.parent_params.clone()
).await;
// (self.exec_body)(
// self.parent_params.clone()
// ).await;
}
pub async fn stop(&mut self) -> Result<String,String>

View file

@ -141,28 +141,24 @@ async fn test3_body(params : ExecBodyParams) {
if let Ok(newr) = a {
// NOTE : The below is a "Playing aound" feature
// In the below, we're unnecessarily adjusting the ExecBodyParams of the parent
// To get the reference to the Routine or the BotAction there are now refernces to itself
// [ ] before execute , be sure to adjust curr_act
// let mut params_mut = params.clone();
let mut params_mut = params;
// let newr_ar = Arc::new(RwLock::new(newr));
// let mut params_mut = params;
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;
// params_mut.curr_act = Arc::new(RwLock::new(
// BotAction::R(newr_ar.clone())
// ));
{
newr_ar.write().await.parent_params = params_mut.clone();
}
// {
// newr_ar.write().await.parent_params = params_mut.clone();
// }
let rslt = Routine::start(newr_ar.clone()).await;
@ -179,12 +175,12 @@ async fn test3_body(params : ExecBodyParams) {
rsltstr
).as_str(),
Some("experiment003 > test3_body".to_string()),
Some(&params_mut.msg),
Some(&params.msg),
);
Log::flush();
let bot = Arc::clone(&params_mut.bot);
let bot = Arc::clone(&params.bot);
let botlock = bot.read().await;
@ -193,9 +189,9 @@ async fn test3_body(params : ExecBodyParams) {
.botmgrs
.chat
.say_in_reply_to(
&params_mut.msg,
&params.msg,
format!("Routine Result : {:?}",rsltstr),
params_mut.clone()
params.clone()
).await;
// [x] Will not be handling JoinHandles here . If immediate abort() handling is required, below is an example that works