WIP: Basic Routine Functionality #40

Draft
modulatingforce wants to merge 23 commits from routines-functionality into master
Showing only changes of commit 226da4362a - Show all commits

View file

@ -76,7 +76,6 @@ pub async fn init(mgr: Arc<ModulesManager>) {
// 2. Add the BotAction to ModulesManager
botc1.add_core_to_modmgr(Arc::clone(&mgr)).await;
// async fn cmd_enable(bot: BotAR, msg: PrivmsgMessage) {
async fn cmd_enable(params : ExecBodyParams) {
/*
There should be additional validation checks
@ -260,7 +259,6 @@ pub async fn init(mgr: Arc<ModulesManager>) {
// 2. Add the BotAction to ModulesManager
botc1.add_core_to_modmgr(Arc::clone(&mgr)).await;
// async fn cmd_disable(bot: BotAR, msg: PrivmsgMessage) {
async fn cmd_disable(params : ExecBodyParams) {
/*
There should be additional validation checks
@ -469,7 +467,6 @@ pub enum StatusType {
pub enum BotAction {
C(BotCommand),
L(Listener),
// R(Routine),
R(Arc<RwLock<Routine>>),
}
@ -617,14 +614,9 @@ pub enum RoutineAttr {
pub struct Routine {
pub name : String ,
pub module : BotModule , // from() can determine this if passed parents_params
// pub channel : Option<Channel> , // Routines generally run by Channel ; but can be left None
pub channel : Channel , // Requiring some channel context
exec_body: bot_actions::actions_util::ExecBody,
// parent_params : Option<ExecBodyParams> ,
parent_params : ExecBodyParams ,
// pub join_handle : Option<JoinHandle<()>> ,
// pub join_handle : Option<JoinHandle<()>> ,
// pub join_handle : Option<Arc<RwLock<JoinHandle<()>>>> ,
pub join_handle : Option<Arc<RwLock<JoinHandle<()>>>> ,
start_time : Option<DateTime<Local>> ,
complete_iterations : i64 ,
@ -642,10 +634,7 @@ impl Routine {
channel : Channel,
routine_attr : Vec<RoutineAttr> ,
exec_body : bot_actions::actions_util::ExecBody ,
// parent_params : Option<ExecBodyParams>
parent_params : ExecBodyParams
// ) -> Result<String,String> {
// ) -> Result<Routine,String> {
) -> Result<
Arc<RwLock<Routine>>,
String
@ -655,8 +644,6 @@ impl Routine {
// to ensure those params don't conflict
// conlicts would throw an error
// parent_params.unwrap().
if routine_attr.contains(&RoutineAttr::RunOnce) {
return Ok(Arc::new(RwLock::new(Routine {
name ,
@ -687,42 +674,26 @@ impl Routine {
Err("NOT IMPLEMENTED".to_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>
) -> Result<Arc<RwLock<Routine>>,String>
) -> Result<Arc<RwLock<Routine>>,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));
/*
GENERAL LOGIC
1. Create a loop scenario based on routine_attr such as RunOnce
2. Run the loop depending on how the Routine is setup
*/
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<()>> {
// async fn innerhelper(inroutine : Arc<RwLock<Routine>>) -> Option<JoinHandle<()>> {
async fn runonce_innerhelper(inroutine : Arc<RwLock<Routine>>) -> JoinHandle<()> {
botlog::trace(
@ -734,79 +705,6 @@ impl Routine {
);
Log::flush();
// Some(
// tokio::spawn(async move {
// botlog::trace(
// ">> Within Spawn",
// Some(format!(
// "Routine > start() > (In Tokio Spawn)",
// )),
// Some(&inroutine.read().await.parent_params.msg),
// );
// Log::flush();
// // 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 {}",
// inroutine.read().await.name,
// inroutine.read().await.channel.0
// )
// .as_str(),
// Some(format!(
// "Routine > start() > (In Tokio Spawn) > {:?}",
// inroutine.read().await.module
// )),
// Some(&inroutine.read().await.parent_params.msg),
// );
// Log::flush();
// inroutine.write().await.start_time = Some(chrono::offset::Local::now());
// // Loop iteration
// while remainingiter > 1 {
// // execution body
// inroutine.read().await.loopbody().await;
// // end of loop iteration
// remainingiter -= 1;
// {
// inroutine.write().await.remaining_iterations = Some(remainingiter);
// inroutine.write().await.complete_iterations += 1;
// }
// }
// botlog::trace(
// format!(
// "[TRACE][Routine Completed] {} in {}",
// inroutine.read().await.name,
// inroutine.read().await.channel.0
// )
// .as_str(),
// Some(format!(
// "Routine > start() > (In Tokio Spawn) > {:?}",
// inroutine.read().await.module
// )),
// Some(&inroutine.read().await.parent_params.msg),
// );
// Log::flush();
// }))
return tokio::spawn(async move {
@ -819,131 +717,25 @@ impl Routine {
);
Log::flush();
{
let mut a = inroutine.write().await;
a.start_time = Some(chrono::offset::Local::now());
a.complete_iterations += 1;
if let Some(i) = a.remaining_iterations{
if i > 0 {
a.remaining_iterations = Some(i-1)
} else {
// do nothing for now
// [ ] If this was in a loop, please validate this
}
}
}
// botlog::trace(
// &format!(
// ">> Within Spawn - Reamining Iter Eval : {:?}"
// ,inroutine.read().await.remaining_iterations),
// Some(format!(
// "Routine > start() > (In Tokio Spawn)",
// )),
// Some(&inroutine.read().await.parent_params.msg),
// );
// execution body
inroutine.read().await.loopbody().await;
// Log::flush();
botlog::trace(
">> Within Spawn >> After remaining iter eval",
Some(format!(
"Routine > start() > (In Tokio Spawn)",
)),
Some(&inroutine.read().await.parent_params.msg),
);
Log::flush();
botlog::trace(
">> Within Spawn >> After remaining iter eval 2 ",
Some(format!(
"Routine > start() > (In Tokio Spawn)",
)),
Some(&inroutine.read().await.parent_params.msg),
);
Log::flush();
botlog::trace(
">> Within Spawn >> After remaining iter eval 3 ",
Some(format!(
"Routine > start() > (In Tokio Spawn)",
)),
Some(&inroutine.read().await.parent_params.msg),
);
Log::flush();
// 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);
// let mut remainingiter = inroutine.read().await.remaining_iterations.unwrap_or(0);
// let mut remainingiter = inroutine.read().await.remaining_iterations.unwrap_or(0);
// async fn setremain(inroutine : Arc<RwLock<Routine>> , number : Option<i64>) {
// inroutine.write().await.remaining_iterations = number;
// }
// if inroutine.read().await.remaining_iterations.is_none() {
// // inroutine.write().await.remaining_iterations = Some(0);
// // setremain(inroutine.clone(),Some(0)).await;
// }
// botlog::trace(
// format!(
// "[TRACE][Routine Started] {} in {}",
// inroutine.read().await.name,
// inroutine.read().await.channel.0
// )
// .as_str(),
// Some(format!(
// "Routine > start() > (In Tokio Spawn) > {:?}",
// inroutine.read().await.module
// )),
// Some(&inroutine.read().await.parent_params.msg),
// );
// botlog::trace(
// format!(
// "[TRACE][Routine Started] {} in {}",
// inroutine.read().await.name,
// inroutine.read().await.channel.0
// )
// .as_str(),
// Some(format!(
// "Routine > start() > (In Tokio Spawn) > {:?}",
// inroutine.read().await.module
// )),
// Some(&inroutine.read().await.parent_params.msg),
// );
// Log::flush();
// inroutine.write().await.start_time = Some(chrono::offset::Local::now());
// {
// inroutine.write().await.start_time = Some(chrono::offset::Local::now());
// }
// Loop iteration
// while remainingiter > 1 {
// while inroutine.read().await.remaining_iterations.unwrap_or(0) > 1 {
// execution body
inroutine.read().await.loopbody().await;
// end of loop iteration
// let remain_iter = inroutine.read().await.remaining_iterations.unwrap_or(0);
// {
// // inroutine.write().await.remaining_iterations = Some(remain_iter - 1);
// setremain(inroutine.clone(),Some(remain_iter - 1)).await;
// }
// {
// inroutine.write().await.complete_iterations += 1;
// }
// remainingiter -= 1;
// {
// inroutine.write().await.remaining_iterations = Some(remainingiter);
// inroutine.write().await.complete_iterations += 1;
// }
// }
botlog::trace(
format!(
"[TRACE][Routine Completed] {} in {}",
@ -958,6 +750,20 @@ impl Routine {
Some(&inroutine.read().await.parent_params.msg),
);
botlog::trace(
format!(
"[TRACE][Routine Completed][Routine Header Test] {} in {} > Completed Iterations : {}",
inroutine.read().await.name,
inroutine.read().await.channel.0 ,
inroutine.read().await.complete_iterations,
)
.as_str(),
Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}",
inroutine.read().await.module
)),
Some(&inroutine.read().await.parent_params.msg),
);
Log::flush();
})
@ -967,27 +773,10 @@ impl Routine {
}
// 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()).await;
{
trg_routine_ar.write().await.join_handle = Some(Arc::new(RwLock::new(runonce_innerhelper(trg_routine_ar.clone()).await)));
}
// if let Some(a) = &trg_routine_ar.read().await.join_handle {
// (*a).await.unwrap();
// };
// return Ok("Successfully Started Routine".to_string());
return Ok(trg_routine_ar);
}
@ -997,14 +786,12 @@ impl Routine {
botlog::trace(
format!(
"[ERROR][Routine NOT IMPLEMENTED] {} in {}",
// 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
trg_routine_ar.read().await.module
)),
Some(&trg_routine_ar.read().await.parent_params.msg),