8 个版本
0.6.2 | 2024 年 7 月 27 日 |
---|---|
0.6.1 | 2024 年 6 月 14 日 |
0.5.1 | 2024 年 6 月 3 日 |
0.5.0 | 2024 年 5 月 13 日 |
0.4.1 | 2024 年 4 月 29 日 |
#240 in 异步
145 每月下载量
160KB
3.5K SLoC
SACS - 简单异步 Cron 调度器
SACS
是一个易于使用、轻量级的 Tokio
运行时的可重复异步任务调度器和执行器。
功能
- 以不同类型的计划运行任务:一次性、延迟、间隔、cron 计划。
- 使用当前的
Tokio
运行时或创建具有指定类型、线程数和有限并行性的新运行时。 - 允许取消任务、获取任务当前状态和运行时统计信息。
- 可以限制任务执行时间。
- 轻量级,小巧,易于使用。
快速入门
只需创建 Scheduler
并将其添加到 Task
中。有关更多示例和可能的用法详情,请参阅 crate's 文档
。
use sacs::{
scheduler::{Scheduler, ShutdownOpts, TaskScheduler},
task::{CronOpts, Task, TaskSchedule},
Result,
};
use std::time::Duration;
use tracing::info;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
// Create scheduler with default config
let scheduler = Scheduler::default();
// Create task with cron schedule: repeat it every 3 seconds
let cron = TaskSchedule::Cron("*/3 * * * * *".try_into()?, CronOpts::default());
let task = Task::new(cron, |id| {
Box::pin(async move {
info!("Job {id} started.");
// Actual async workload here
tokio::time::sleep(Duration::from_secs(2)).await;
// ...
info!("Job {id} finished.");
})
});
// Post task to the scheduler and forget it :)
let _task_id = scheduler.add(task).await?;
// ... and do any other async work in parallel
tokio::time::sleep(Duration::from_secs(10)).await;
// It's not mandatory but good to shutdown scheduler
// Wait for completion of all running jobs
scheduler.shutdown(ShutdownOpts::WaitForFinish).await
}
待办事项
许可证
本项目采用 MIT 许可证。
依赖关系
~6–15MB
~180K SLoC