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

This commit is contained in:
ModulatingForce 2024-03-27 10:18:13 -04:00
parent 3f8e798050
commit b08d91af5d
2 changed files with 237 additions and 32 deletions

View file

@ -688,7 +688,8 @@ impl Routine {
// pub async fn start(self : &mut Self) -> Result<String,String> // pub async fn start(self : &mut Self) -> Result<String,String>
pub async fn start( pub async fn start(
trg_routine_ar : Arc<RwLock<Routine>> trg_routine_ar : Arc<RwLock<Routine>>
) -> Result<String,String> // ) -> Result<String,String>
) -> Result<Arc<RwLock<Routine>>,String>
{ {
// [ ] Asyncio Spawn likely around here // [ ] Asyncio Spawn likely around here
@ -718,49 +719,227 @@ impl Routine {
// 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 : Routine) -> Option<JoinHandle<()>> { // fn innerhelper(inroutine : Routine) -> Option<JoinHandle<()>> {
fn innerhelper(inroutine : Arc<RwLock<Routine>>) -> Option<JoinHandle<()>> { // async fn innerhelper(inroutine : Arc<RwLock<Routine>>) -> Option<JoinHandle<()>> {
async fn runonce_innerhelper(inroutine : Arc<RwLock<Routine>>) -> JoinHandle<()> {
Some( botlog::trace(
tokio::spawn(async move { "innerhelper() started",
Some(format!(
"Routine > start() > (In Tokio Spawn)",
)),
Some(&inroutine.read().await.parent_params.msg),
);
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 {
botlog::trace(
">> Within Spawn",
Some(format!(
"Routine > start() > (In Tokio Spawn)",
)),
Some(&inroutine.read().await.parent_params.msg),
);
Log::flush();
// 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),
// );
// 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 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); // 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( // botlog::trace(
format!( // format!(
"[TRACE][Routine Started] {} in {}", // "[TRACE][Routine Started] {} in {}",
inroutine.read().await.name, // inroutine.read().await.name,
inroutine.read().await.channel.0 // inroutine.read().await.channel.0
) // )
.as_str(), // .as_str(),
Some(format!( // Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}", // "Routine > start() > (In Tokio Spawn) > {:?}",
inroutine.read().await.module // inroutine.read().await.module
)), // )),
Some(&inroutine.read().await.parent_params.msg), // Some(&inroutine.read().await.parent_params.msg),
); // );
inroutine.write().await.start_time = Some(chrono::offset::Local::now()); // 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 // Loop iteration
while remainingiter > 1 { // while remainingiter > 1 {
// while inroutine.read().await.remaining_iterations.unwrap_or(0) > 1 {
// execution body // execution body
inroutine.read().await.loopbody().await; inroutine.read().await.loopbody().await;
// end of loop iteration // end of loop iteration
remainingiter -= 1;
{
inroutine.write().await.remaining_iterations = Some(remainingiter);
inroutine.write().await.complete_iterations += 1;
}
} // 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( botlog::trace(
format!( format!(
@ -778,7 +957,10 @@ impl Routine {
Log::flush(); Log::flush();
})) })
} }
@ -793,9 +975,17 @@ impl Routine {
// let routine = *self_rw_copy.read().await; // let routine = *self_rw_copy.read().await;
// self_rw_copy.write().await.join_handle = innerhelper(&routine); // self_rw_copy.write().await.join_handle = innerhelper(&routine);
// let self_ref = Arc::clone(&trg_routine_ar); // let self_ref = Arc::clone(&trg_routine_ar);
trg_routine_ar.write().await.join_handle = innerhelper(trg_routine_ar.clone()); // trg_routine_ar.write().await.join_handle = innerhelper(trg_routine_ar.clone()).await;
{
trg_routine_ar.write().await.join_handle = Some(runonce_innerhelper(trg_routine_ar.clone()).await);
}
return Ok("Successfully Started Routine".to_string()); // 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);
} }
@ -825,6 +1015,16 @@ impl Routine {
async fn loopbody(&self) async fn loopbody(&self)
{ {
botlog::trace(
"loopbody() started",
Some(format!(
"Routine > start() > (During Tokio Spawn) > Execution body",
)),
None,
);
Log::flush();
(self.exec_body)( (self.exec_body)(
self.parent_params.clone() self.parent_params.clone()
).await; ).await;

View file

@ -160,10 +160,15 @@ async fn test3_body(params : ExecBodyParams) {
// let rslt = newr_ar.read().await.start().await; // let rslt = newr_ar.read().await.start().await;
let rsltstr = match rslt {
Ok(_) => "successful".to_string(),
Err(a) => a,
};
botlog::debug( botlog::debug(
format!("TEST3_BODY RESULT : {:?}", format!("TEST3_BODY RESULT : {:?}",
rslt rsltstr
).as_str(), ).as_str(),
Some("experiment003 > test3_body".to_string()), Some("experiment003 > test3_body".to_string()),
Some(&params.msg), Some(&params.msg),
@ -181,7 +186,7 @@ async fn test3_body(params : ExecBodyParams) {
.chat .chat
.say_in_reply_to( .say_in_reply_to(
&params.msg, &params.msg,
format!("Routine Result : {:?}",rslt), format!("Routine Result : {:?}",rsltstr),
params.clone() params.clone()
).await; ).await;