routine methods
This commit is contained in:
parent
4637312da9
commit
ca9361cc93
1 changed files with 131 additions and 23 deletions
|
@ -28,6 +28,7 @@ use core::panic;
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
use std::borrow::BorrowMut;
|
use std::borrow::BorrowMut;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::ops::DerefMut;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use casual_logger::Log;
|
use casual_logger::Log;
|
||||||
|
@ -1134,12 +1135,12 @@ impl Routine {
|
||||||
|
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
format!(
|
format!(
|
||||||
"[ROUTINE][Sent Gracefully Stopp Signal] {} in {}",
|
"[ROUTINE][Sent Gracefully Stop Signal] {} in {}",
|
||||||
self_lock.name,self_lock.channel.0
|
self_lock.name,self_lock.channel.0
|
||||||
)
|
)
|
||||||
.as_str(),
|
.as_str(),
|
||||||
Some(format!(
|
Some(format!(
|
||||||
"Routine > start() > (In Tokio Spawn) > {:?}",
|
"Routine > stop() > {:?}",
|
||||||
self_lock.module
|
self_lock.module
|
||||||
)),
|
)),
|
||||||
Some(&self_lock.parent_params.msg),
|
Some(&self_lock.parent_params.msg),
|
||||||
|
@ -1167,78 +1168,185 @@ impl Routine {
|
||||||
// Err("NOT IMPLEMENTED".to_string())
|
// Err("NOT IMPLEMENTED".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn cancel(&self) -> Result<String,String>
|
pub async fn cancel(&mut self) -> Result<String,String>
|
||||||
// [ ] => 03.27 - WIP - NOT IMPLEMENTED
|
// [ ] => 03.27 - REVIEW FOR COMPLETION
|
||||||
{
|
{
|
||||||
|
|
||||||
// [ ] Likely calls abort()
|
// [ ] Likely calls abort()
|
||||||
// Related :
|
// Related :
|
||||||
// https://docs.rs/tokio/latest/tokio/task/struct.JoinHandle.html#method.abort
|
// https://docs.rs/tokio/latest/tokio/task/struct.JoinHandle.html#method.abort
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let self_rw = Arc::new(RwLock::new(self));
|
let self_rw = Arc::new(RwLock::new(self));
|
||||||
let self_lock = self_rw.read().await;
|
let self_lock = self_rw.read().await;
|
||||||
|
|
||||||
|
|
||||||
|
match &self_lock.join_handle {
|
||||||
|
None => return Err("No Join Handle on the Routine to Cancel".to_string()),
|
||||||
|
Some(a) => {
|
||||||
|
a.read().await.abort();
|
||||||
|
{
|
||||||
|
let mut lock_mut = self_rw.write().await;
|
||||||
|
lock_mut.internal_signal = RoutineSignal::Stopped;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
format!(
|
format!(
|
||||||
"[ERROR][Routine NOT IMPLEMENTED] {} in {}",
|
"[ROUTINE][Cancelled Routine] {} in {}",
|
||||||
self_lock.name,self_lock.channel.0
|
self_lock.name,self_lock.channel.0
|
||||||
)
|
)
|
||||||
.as_str(),
|
.as_str(),
|
||||||
Some(format!(
|
Some(format!(
|
||||||
"Routine > start() > (In Tokio Spawn) > {:?}",
|
"Routine > cancel() > {:?}",
|
||||||
self_lock.module
|
self_lock.module
|
||||||
)),
|
)),
|
||||||
Some(&self_lock.parent_params.msg),
|
Some(&self_lock.parent_params.msg),
|
||||||
);
|
);
|
||||||
|
|
||||||
Log::flush();
|
Log::flush();
|
||||||
Err("NOT IMPLEMENTED".to_string())
|
Ok("Cancelled Successfully".to_string())
|
||||||
|
|
||||||
|
|
||||||
|
// botlog::trace(
|
||||||
|
// format!(
|
||||||
|
// "[ERROR][Routine NOT IMPLEMENTED] {} in {}",
|
||||||
|
// self_lock.name,self_lock.channel.0
|
||||||
|
// )
|
||||||
|
// .as_str(),
|
||||||
|
// Some(format!(
|
||||||
|
// "Routine > start() > (In Tokio Spawn) > {:?}",
|
||||||
|
// self_lock.module
|
||||||
|
// )),
|
||||||
|
// Some(&self_lock.parent_params.msg),
|
||||||
|
// );
|
||||||
|
|
||||||
|
// Log::flush();
|
||||||
|
// Err("NOT IMPLEMENTED".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn restart(
|
pub async fn restart(
|
||||||
&self,
|
// &mut self,
|
||||||
_force : bool
|
self,
|
||||||
|
force : bool
|
||||||
) -> Result<String,String>
|
) -> Result<String,String>
|
||||||
// [ ] => 03.27 - WIP - NOT IMPLEMENTED
|
// [ ] => 03.27 - REVIEW FOR COMPLETION
|
||||||
{
|
{
|
||||||
// force flag aborts the routine immediately (like cancel())
|
// force flag aborts the routine immediately (like cancel())
|
||||||
|
|
||||||
|
|
||||||
let self_rw = Arc::new(RwLock::new(self));
|
let self_rw = Arc::new(RwLock::new(self));
|
||||||
let self_lock = self_rw.read().await;
|
|
||||||
|
if force
|
||||||
|
{
|
||||||
|
let mut self_lock = self_rw.write().await;
|
||||||
|
self_lock.cancel().await?;
|
||||||
|
} else {
|
||||||
|
let mut self_lock = self_rw.write().await;
|
||||||
|
self_lock.stop().await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Routine::start(self_rw.clone()).await?;
|
||||||
|
|
||||||
|
let self_lock = self_rw.read().await;
|
||||||
|
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
format!(
|
format!(
|
||||||
"[ERROR][Routine NOT IMPLEMENTED] {} in {}",
|
"[ROUTINE][Restarted Routine] {} in {}",
|
||||||
self_lock.name,self_lock.channel.0
|
self_lock.name,self_lock.channel.0
|
||||||
)
|
)
|
||||||
.as_str(),
|
.as_str(),
|
||||||
Some(format!(
|
Some(format!(
|
||||||
"Routine > start() > (In Tokio Spawn) > {:?}",
|
"Routine > restart() > {:?}",
|
||||||
self_lock.module
|
self_lock.module
|
||||||
)),
|
)),
|
||||||
Some(&self_lock.parent_params.msg),
|
Some(&self_lock.parent_params.msg),
|
||||||
);
|
);
|
||||||
|
|
||||||
Log::flush();
|
Log::flush();
|
||||||
Err("NOT IMPLEMENTED".to_string())
|
Ok("Restarted successfully".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn change_channel(
|
pub async fn change_channel(
|
||||||
&self,
|
&mut self,
|
||||||
_channel : Channel
|
channel : Channel
|
||||||
) -> Result<String,String>
|
) -> Result<String,String>
|
||||||
// [ ] => 03.27 - WIP - NOT IMPLEMENTED
|
// [ ] => 03.28 - REVIEW FOR COMPLETION
|
||||||
{
|
{
|
||||||
// [ ] Think Ideally it should try to
|
// [x] Think Ideally it should try to
|
||||||
// change the target channel of the
|
// change the target channel of the
|
||||||
// internal process too if possible?
|
// internal process too if possible?
|
||||||
|
|
||||||
Err("NOT IMPLEMENTED".to_string())
|
self.channel = channel;
|
||||||
|
|
||||||
|
|
||||||
|
let self_rw = Arc::new(RwLock::new(self));
|
||||||
|
|
||||||
|
let self_lock = self_rw.read().await;
|
||||||
|
|
||||||
|
botlog::trace(
|
||||||
|
format!(
|
||||||
|
"[ROUTINE][Change Channel] {} in {}",
|
||||||
|
self_lock.name,self_lock.channel.0
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
Some(format!(
|
||||||
|
"Routine > restart() > {:?}",
|
||||||
|
self_lock.module
|
||||||
|
)),
|
||||||
|
Some(&self_lock.parent_params.msg),
|
||||||
|
);
|
||||||
|
|
||||||
|
Log::flush();
|
||||||
|
|
||||||
|
// Err("NOT IMPLEMENTED".to_string())
|
||||||
|
Ok("Changed Successfully".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub async fn set_routine_attributes(
|
||||||
|
&mut self,
|
||||||
|
routine_attr : Vec<RoutineAttr>
|
||||||
|
) -> Result<String,String>
|
||||||
|
// [ ] => 03.27 - WIP - NOT IMPLEMENTED
|
||||||
|
{
|
||||||
|
// This is way to custom set attributes first
|
||||||
|
// They will Be Validated before being applied
|
||||||
|
// IDEALLY the routine also be restarted afterwards externally
|
||||||
|
|
||||||
|
Routine::validate_attr(&routine_attr).await?;
|
||||||
|
|
||||||
|
|
||||||
|
let self_rw = Arc::new(RwLock::new(self));
|
||||||
|
{
|
||||||
|
let mut self_lock = self_rw.write().await;
|
||||||
|
self_lock.routine_attr = routine_attr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// let self_rw = Arc::new(RwLock::new(self));
|
||||||
|
|
||||||
|
let self_lock = self_rw.read().await;
|
||||||
|
|
||||||
|
botlog::trace(
|
||||||
|
format!(
|
||||||
|
"[ROUTINE][Set Routine Attributes] {} in {}",
|
||||||
|
self_lock.name,self_lock.channel.0
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
Some(format!(
|
||||||
|
"Routine > restart() > {:?}",
|
||||||
|
self_lock.module
|
||||||
|
)),
|
||||||
|
Some(&self_lock.parent_params.msg),
|
||||||
|
);
|
||||||
|
|
||||||
|
Log::flush();
|
||||||
|
|
||||||
|
|
||||||
|
Ok("Changed Successfully".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue