Crates.io | simple-scheduler |
lib.rs | simple-scheduler |
version | 0.1.0 |
source | src |
created_at | 2022-03-02 13:23:40.307239 |
updated_at | 2022-03-02 13:23:40.307239 |
description | Schedule periodic tasks to run asynchronously |
homepage | |
repository | https://git.sr.ht/~rjframe/simple-scheduler |
max_upload_size | |
id | 542261 |
size | 37,063 |
Schedule repeated tasks to run asynchronously.
This is not a precise scheduler; tasks may not run (probably won't run) at the exact interval you set.
Useful links:
simple_scheduler
uses the executor provided by the futures
crate. If you
need or prefer another executor, I would accept PRs to optionally use it.
Why another task scheduler? When I took a look at what was available, everything I found used cron expressions (which I don't want in a library API), did not run the tasks asynchronously, and/or required tokio, which I wasn't using.
use simple_scheduler::{
// Also exports chrono::DateTime<chrono::Utc> as DateTime.
Duration, Time,
Schedule, ScheduleAt, ScheduledTask, task,
};
use chrono::Utc;
fn main() {
let periodic_task = ScheduledTask::new(
ScheduleAt::Interval(Duration::seconds(4)),
task!(async { println!("Do something every four seconds") })
).unwrap();
let onetime_task = ScheduledTask::new(
ScheduleAt::DateTime(Utc::now() + Duration::seconds(15)),
task!(task2())
).unwrap();
let daily_task = ScheduledTask::new(
ScheduleAt::Daily(Time::from_hms(01, 23, 45)),
task!(async { println!("Hello"); })
).unwrap();
let schedule = Schedule::builder()
.tasks([
periodic_task,
onetime_task,
daily_task,
])
.wake_interval(Duration::seconds(1)).unwrap()
.build();
schedule.run();
// run() exits immediately, so we need to wait a bit.
std::thread::sleep(Duration::seconds(30).to_std().unwrap());
}
async fn task2() {
println!("Function executed");
}
I use this to run maintenance tasks on servers; I have arbitrarily set a minimum
wake_interval
of 100 nanoseconds; I have not tested it at that rapid a period
or whether a lower limit makes sense.
All library source code is licensed under the terms of the MPL 2.0 license.
The source code of examples is licensed under the terms of the MIT license.
Patches and pull requests are welcome. For major features or breaking changes, please open a ticket or start a discussion first so we can discuss what you would like to do.
Email: code@ryanjframe.com
Website: www.ryanjframe.com
diaspora*: rjframe@diasp.org