2 个稳定版本
2.0.1 | 2024年1月10日 |
---|---|
1.0.0 | 2022年4月26日 |
#198 in 异步
301 每月下载量
18KB
187 行
async-cron-scheduler
支持 cron 表达式的运行时无关的异步任务调度器
- 轻量级:依赖最少,因为它不依赖于运行时。
- 高效:无计数器引用的无滴答设计。
- 运行时无关:自带运行时。无运行时依赖。
- 异步:一个单一的 future 驱动整个调度器服务。
- 任务调度:按不同时间段安排多个作业。
- Cron 表达式:调度语法的标准格式。
演示
use chrono::offset::Local;
use async_cron_scheduler::*;
use smol::Timer;
use std::time::Duration;
smol::block_on(async move {
// Creates a scheduler based on the Local timezone. Note that the `sched_service`
// contains the background job as a future for the caller to decide how to await
// it. When the scheduler is dropped, the scheduler service will exit as well.
let (mut scheduler, sched_service) = Scheduler::<Local>::launch(Timer::after);
// Creates a job which executes every 1 seconds.
let job = Job::cron("1/1 * * * * *").unwrap();
let fizz_id = scheduler.insert(job, |id| println!("Fizz")).await;
// Creates a job which executes every 3 seconds.
let job = Job::cron("1/3 * * * * *").unwrap();
let buzz_id = scheduler.insert(job, |id| println!("Buzz")).await;
// Creates a job which executes every 5 seconds.
let job = Job::cron("1/5 * * * * *").unwrap();
let bazz_id = scheduler.insert(job, |id| println!("Bazz")).await;
// A future which gradually drops jobs from the scheduler.
let dropper = async move {
Timer::after(Duration::from_secs(7)).await;
scheduler.remove(fizz_id).await;
println!("Fizz gone");
Timer::after(Duration::from_secs(5)).await;
scheduler.remove(buzz_id).await;
println!("Buzz gone");
Timer::after(Duration::from_secs(1)).await;
scheduler.remove(bazz_id).await;
println!("Bazz gone");
// `scheduler` is dropped here, which causes the sched_service to end.
};
// Poll the dropper and scheduler service concurrently until both return.
futures::future::join(sched_service, dropper).await;
});
许可证
根据 Mozilla Public License 2.0 许可。
贡献
您提交的任何有意包含在作品中的贡献都应根据 Mozilla Public License 2.0 (MPL-2.0) 许可。
依赖项
~3–28MB
~399K SLoC