delay-timer周期任务lib
delay-timer是一个基于时间轮算法构建的lib,它可以很方便地管理定时任务,或者周期性地执行任意任务。
轻松打造一个延迟/周期任务的运行容器。可以想象成crontab,但可以处理同步/异步任务,但支持动态添加/取消/删除/更新,
单个任务支持配置任务的最大并行数量,运行时间等。
底层运行时基于的smol和tokio(可选的feature),你可以用其中一个来构建你的应用程序。
v0.4.0 新功能:
1.支持动态的修改运行中的任务。
2.支持了insert任务后获取句柄`TaskInstancesChain`,可以动态获取运行中的任务实例`TaskInstance`。
2.1. 运行中任务的任务实例可以动态取消。
2.2. 取消分为三种方式:同步阻塞取消、超时限制取消、异步取消。
2.3. 支持读取运行中任务的运行状态。
3.支持获取内部异步子任务进程的输出。
更新依赖:
替换 waitmap -> dashmap .
升级 cron_clock .
更新examples:
增加,async-std & tokio 使用案例。
增加,动态取消运行中任务实例案例。
丰富了文档。
小例子:
创建一个异步的调度任务,支持如下。
任务描述: 每秒运行一次、只运行9次、最大并行运行数3、唯一id为:1.
``` rust
let body = create_async_fn_body!({
// do something.
});
let task = TaskBuilder::default()
.set_frequency_by_candy(CandyFrequency::CountDown(9, CandyCron::Secondly))
.set_task_id(1)
.set_maximun_parallel_runable_num(3)
.spawn(body)?;
```
async-std 中的应用 :
``` rust
#[async_std::main]
async fn main() -> Result<()> {
// Build an DelayTimer that uses the default configuration of the Smol runtime internally.
let delay_timer = DelayTimerBuilder::default().build();
// Develop a print job that runs in an asynchronous cycle.
let task_instance_chain = delay_timer.insert_task(build_task_async_print())?;
// Get the running instance of task 1.
let task_instance = task_instance_chain.next_with_async_wait().await?;
// Cancel running task instances.
task_instance.cancel_with_async_wait().await?;
// Remove task which id is 1.
delay_timer.remove_task(1)?;
// No new tasks are accepted; running tasks are not affected.
delay_timer.stop_delay_timer()
}
```
tokio中的应用:
``` rust
#[tokio::main]
async fn main() -> Result<()> {
// In addition to the mixed (smol & tokio) runtime
// You can also share a tokio runtime with delayTimer, please see api `DelayTimerBuilder::tokio_runtime` for details.
// Build an DelayTimer that uses the default configuration of the Smol runtime internally.
let delay_timer = DelayTimerBuilder::default().build();
// Develop a print job that runs in an asynchronous cycle.
let task_instance_chain = delay_timer.insert_task(build_task_async_print())?;
// Get the running instance of task 1.
let task_instance = task_instance_chain.next_with_async_wait().await?;
// Cancel running task instances.
task_instance.cancel_with_async_wait().await?;
// Remove task which id is 1.
delay_timer.remove_task(1)?;
// No new tasks are accepted; running tasks are not affected.
delay_timer.stop_delay_timer()
}
```
非异步上下文的应用:
```
fn main() -> Result<()> {
let delay_timer = DelayTimerBuilder::default().build();
// Develop a print job that runs in an asynchronous cycle.
let task_instance_chain = delay_timer.insert_task(build_task_async_print())?;
// Develop an http request task that runs in an asynchronous cycle.
delay_timer.add_task(build_task_async_request())?;
// Get the running instance of task 1.
let task_instance = task_instance_chain.next_with_wait()?;
// Cancel running task instances.
task_instance.cancel_with_wait()?;
// Remove task which id is 1.
delay_timer.remove_task(1)?;
// No new tasks are accepted; running tasks are not affected.
delay_timer.stop_delay_timer()?;
Ok(())
}
```
## repo: [delay-timer](https://github.com/BinChengZhao/delay-timer)
## doc: [delay-timer-doc](https://docs.rs/delay_timer)
## crates: [delay-timer-crates](https://crates.io/crates/delay_timer)