feat(sched): ✨ tokio: schedule a future
This commit is contained in:
parent
55118835a8
commit
ebb4e94f1a
46
examples/sched_tokio_future.rs
Normal file
46
examples/sched_tokio_future.rs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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>(
|
||||
&mut self,
|
||||
name: Str,
|
||||
@ -92,25 +93,43 @@ impl Scheduler {
|
||||
Str: AsRef<str>,
|
||||
{
|
||||
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
|
||||
///
|
||||
/// 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,
|
||||
name: &str,
|
||||
mut func: F,
|
||||
future: Fut,
|
||||
rules: Vec<SchedulingRule>,
|
||||
) -> TaskHandler
|
||||
where
|
||||
F: FnMut() -> Fut + Send + 'static,
|
||||
Fut: Future<Output = ()> + Send + 'static,
|
||||
{
|
||||
let task = Arc::new(Mutex::new(ScheduledTask {
|
||||
name: name.to_string(),
|
||||
action: Box::pin(func()),
|
||||
action: Box::pin(future),
|
||||
rules: Arc::new(rules),
|
||||
is_running: Arc::new(AtomicBool::new(false)),
|
||||
is_stopped: Arc::new(AtomicBool::new(false)),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user