WIP: Basic Routine Functionality #40
5 changed files with 160 additions and 54 deletions
|
@ -15,17 +15,19 @@ pub type ActAR = Arc<RwLock<BotAction>>;
|
|||
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<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 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<Channel> {
|
||||
pub async fn get_channel(&self) -> Option<Channel> {
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -469,7 +469,8 @@ pub enum StatusType {
|
|||
pub enum BotAction {
|
||||
C(BotCommand),
|
||||
L(Listener),
|
||||
R(Routine),
|
||||
// R(Routine),
|
||||
R(Arc<RwLock<Routine>>),
|
||||
}
|
||||
|
||||
impl BotAction {
|
||||
|
@ -621,7 +622,7 @@ pub struct Routine {
|
|||
exec_body: bot_actions::actions_util::ExecBody,
|
||||
// parent_params : Option<ExecBodyParams> ,
|
||||
parent_params : ExecBodyParams ,
|
||||
join_handle : Option<JoinHandle<()>> ,
|
||||
pub join_handle : Option<JoinHandle<()>> ,
|
||||
start_time : Option<DateTime<Local>> ,
|
||||
complete_iterations : i64 ,
|
||||
remaining_iterations : Option<i64> ,
|
||||
|
@ -641,7 +642,11 @@ impl Routine {
|
|||
// parent_params : Option<ExecBodyParams>
|
||||
parent_params : ExecBodyParams
|
||||
// ) -> Result<String,String> {
|
||||
) -> Result<Routine,String> {
|
||||
// ) -> Result<Routine,String> {
|
||||
) -> Result<
|
||||
Arc<RwLock<Routine>>,
|
||||
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<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
|
||||
// [ ] & 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<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(
|
||||
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();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue