Crates.io | sacs |
lib.rs | sacs |
version | |
source | src |
created_at | 2024-04-22 15:21:02.352223 |
updated_at | 2024-11-30 11:24:10.551408 |
description | Simple Async Cron Scheduler for Tokio |
homepage | https://github.com/alex-karpenko/sacs |
repository | https://github.com/alex-karpenko/sacs |
max_upload_size | |
id | 1216297 |
Cargo.toml error: | TOML parse error at line 28, column 1 | 28 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
SACS
is easy to use, lightweight scheduler and executor of repeatable async tasks for Tokio
runtime.
Tokio
runtime or creates new one with specified type, number of threads and limited parallelism.Just create Scheduler
and
add Task
to it.
Refer to the crate's documentation
for more examples and details of possible usage.
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
}
TaskId
and
JobId
more flexible and
convenient to create and refer tasks.This project is licensed under the MIT license.