| Crates.io | tsuki-scheduler |
| lib.rs | tsuki-scheduler |
| version | 0.1.4 |
| created_at | 2024-06-09 08:33:46.050283+00 |
| updated_at | 2025-03-17 08:21:40.267006+00 |
| description | A simple, light wight, composable and extensible scheduler for every runtime. |
| homepage | |
| repository | https://github.com/4t145/tsuki-scheduler |
| max_upload_size | |
| id | 1266251 |
| size | 102,236 |
A simple, light wight, composable and extensible scheduler for every runtime.
Scheduler = Schedule × Runtime
This small crate can help you running tasks in
with a combination of
Schedule.For a more detailed document, check the rust doc.
cargo add tsuki-scheduler
or
tsuki-scheduler = "0.1"
use tsuki_scheduler::prelude::*;
let mut scheduler = Scheduler::new(Tokio);
let mut scheduler = Scheduler::new(AsyncStd);
let mut scheduler = Scheduler::new(Promise);
let mut scheduler = Scheduler::new(Thread);
// or you may use the async wrapper
let mut scheduler_runner = AsyncSchedulerRunner::<Tokio>::default();
let client = scheduler_runner.client();
What if I want to create a very complex schedule like this:
And you can actually do it:
use tsuki_scheduler::prelude::*;
use chrono::TimeDelta;
let start_time = now() + TimeDelta::seconds(10);
let schedule = Once::new(start_time)
.then(
Cron::utc_from_cron_expr("00 10 * * * *")
.expect("invalid cron")
.or(Period::new(
TimeDelta::minutes(80),
start_time + TimeDelta::minutes(80),
))
.throttling(TimeDelta::minutes(30)),
)
.before(start_time + TimeDelta::days(100));
For some case you want to use a certain type for all schedule, you can use Box<dyn Schedule>, and there's a builder api for it.
use tsuki_scheduler::prelude::*;
use chrono::{Utc, TimeDelta};
let cron_list = Vec::<Cron<Utc>>::new();
// add some cron expr
// ...
// build schedule
let start_time = now() + TimeDelta::seconds(10);
let mut schedule_builder = Once::new(start_time).dyn_builder()
.then(
Cron::utc_from_cron_expr("00 10 * * * *")
.expect("invalid cron")
.or(Period::new(
TimeDelta::minutes(80),
start_time + TimeDelta::minutes(80),
))
.throttling(TimeDelta::minutes(30)),
)
.before(start_time + TimeDelta::days(100));
// collect all cron expr
schedule_builder = cron_list.into_iter().fold(schedule_builder, ScheduleDynBuilder::or);
let schedule = schedule_builder.build();
use tsuki_scheduler::prelude::*;
let mut scheduler = Scheduler::new(Tokio);
let hello_tsuki_task = Task::new_async(
Cron::local_from_cron_expr("*/2 * * * * *").unwrap(),
|
task_uid: TaskUid,
// you can extract any numbers of args as it can be extracted from struct `TaskRun`
// time: Dtu
| async move {
println!("Hello, {task_uid} tsuki!");
},
);
let id = TaskUid::uuid();
scheduler.add_task(id, hello_tsuki_task);
scheduler.execute_by_now();
scheduler.delete_task(id);
You may ignore all the task handles, if you want to manage the handles, implement your own manager by implementing the trait HandleManager.
In a async runtime, you may spawn a task for scheduler to execute periodically driven by event loop. This crate provides an implementation, you can check the example for tokio runtime.
| flag | description |
|---|---|
| uuid | allow to create TaskUid by uuid-v4 |
| cron | allow to create a schedule described by a cron expression |
| tokio | enable tokio runtime |
| async_std | enable async_std runtime |
| thread | enable thread runtime |
| promise | enable js promise runtime |
| async-scheduler | a default async wrapper for async runtime |