1 unstable release
0.1.0 | Mar 2, 2022 |
---|
#830 in Asynchronous
22 downloads per month
13KB
125 lines
Simple Task Scheduler
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:
- Code at SourceHut or at Github
- Issues/requests at SourceHut
- Discussions at SourceHut
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.
License
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.
Contributing
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.
Contact
- Email: code@ryanjframe.com
- Website: www.ryanjframe.com
- diaspora*: rjframe@diasp.org
Related Projects
Dependencies
~2MB
~33K SLoC