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 异步

Download history 188/week @ 2024-04-22 299/week @ 2024-04-29 35/week @ 2024-05-06 150/week @ 2024-05-13 44/week @ 2024-05-20 56/week @ 2024-05-27 158/week @ 2024-06-03 338/week @ 2024-06-10 46/week @ 2024-06-17 24/week @ 2024-06-24 7/week @ 2024-07-01 9/week @ 2024-07-08 102/week @ 2024-07-22 43/week @ 2024-07-29

145 每月下载量

MIT 许可证

160KB
3.5K SLoC

SACS - 简单异步 Cron 调度器

SACS 是一个易于使用、轻量级的 Tokio 运行时的可重复异步任务调度器和执行器。

CI status Audit status Crates.io publishing status docs.rs status Version at Crates.io License

功能

  • 以不同类型的计划运行任务:一次性、延迟、间隔、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
}

待办事项

  • 使 TaskIdJobId 更灵活,更方便地创建和引用任务。
  • 跟踪。
  • 限制执行时间的任务。
  • 更多示例。

许可证

本项目采用 MIT 许可证

依赖关系

~6–15MB
~180K SLoC