feat(sched): tokio: schedule a future

This commit is contained in:
Lucas Colombo 2024-04-21 13:21:07 -03:00
parent 55118835a8
commit ebb4e94f1a
Signed by: lucas
GPG Key ID: EF34786CFEFFAE35
2 changed files with 72 additions and 7 deletions

View File

@ -0,0 +1,46 @@
use {
eyre::{set_hook, DefaultHandler, Result},
lool::{
logger::ConsoleLogger,
sched::{recur, ruleset, scheduler::tokio::Scheduler},
},
tokio::time::sleep,
};
fn setup_eyre() {
let _ = set_hook(Box::new(DefaultHandler::default_with));
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
setup_eyre();
ConsoleLogger::default_setup(log::Level::Trace, "lool::sched::recur")?;
let mut sched = Scheduler::new();
log::debug!("scheduler created");
let now = chrono::Local::now();
let handler = sched.schedule_fut("test-task", async move {
println!("I'm running at {}", &now.format("%Y-%m-%d %H:%M:%S"));
}, recur(ruleset().at_second(0))).await;
sleep(std::time::Duration::from_secs(1)).await;
loop {
let name = handler.name();
println!("{:?}", handler);
handler.get_next_run();
sleep(std::time::Duration::from_secs(60)).await;
let result = sched.remove(&handler).await;
if result.is_ok() {
println!("task {name} removed");
} else {
println!("task {name} not present in the scheduler");
}
}
}

View File

@ -77,9 +77,10 @@ impl Scheduler {
} }
} }
/// 🧉 » schedule a task /// 🧉 » schedule an async task
/// ///
/// schedules a task to be executed at times determined by the provided rules. /// schedules an async function to be executed as a task at time intervals determined by the
/// provided /// rules.
pub async fn schedule<F, Fut, Str>( pub async fn schedule<F, Fut, Str>(
&mut self, &mut self,
name: Str, name: Str,
@ -92,25 +93,43 @@ impl Scheduler {
Str: AsRef<str>, Str: AsRef<str>,
{ {
let name = name.as_ref(); let name = name.as_ref();
self.schedule_many_rules(name, func, vec![rules]).await let mut func = func;
self.schedule_many_rules(name, func(), vec![rules]).await
}
/// 🧉 » schedule a future
///
/// schedules a future to be executed as a task at time intervals determined by the provided
/// rules.
pub async fn schedule_fut<Fut, Str>(
&mut self,
name: Str,
future: Fut,
rules: SchedulingRule,
) -> TaskHandler
where
Fut: Future<Output = ()> + Send + 'static,
Str: AsRef<str>,
{
let name = name.as_ref();
self.schedule_many_rules(name, future, vec![rules]).await
} }
/// 🧉 » schedule a task /// 🧉 » schedule a task
/// ///
/// schedules a task to be executed at times determined by the provided rules. /// schedules a task to be executed at times determined by the provided rules.
pub async fn schedule_many_rules<F, Fut>( pub async fn schedule_many_rules<Fut>(
&mut self, &mut self,
name: &str, name: &str,
mut func: F, future: Fut,
rules: Vec<SchedulingRule>, rules: Vec<SchedulingRule>,
) -> TaskHandler ) -> TaskHandler
where where
F: FnMut() -> Fut + Send + 'static,
Fut: Future<Output = ()> + Send + 'static, Fut: Future<Output = ()> + Send + 'static,
{ {
let task = Arc::new(Mutex::new(ScheduledTask { let task = Arc::new(Mutex::new(ScheduledTask {
name: name.to_string(), name: name.to_string(),
action: Box::pin(func()), action: Box::pin(future),
rules: Arc::new(rules), rules: Arc::new(rules),
is_running: Arc::new(AtomicBool::new(false)), is_running: Arc::new(AtomicBool::new(false)),
is_stopped: Arc::new(AtomicBool::new(false)), is_stopped: Arc::new(AtomicBool::new(false)),