Crate simple_scheduler

Source
Expand description

Simple scheduler for periodic tasks.

Scheduled tasks are run asynchronously.

This is not a precise scheduler; tasks may run at the exact interval or time specified.

§Examples

use simple_scheduler::{
    // `Time` is a chrono::naive::NaiveTime.
    // 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!("Approximately four seconds have passed.") })
    ).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");
}

Macros§

task
Create a task from an async code block.

Structs§

Schedule
A scheduler to run tasks.
ScheduleBuilder
Builder object for a Schedule.
ScheduledTask
A task to run on a schedule.
Time
ISO 8601 time without timezone. Allows for the nanosecond precision and optional leap second representation.

Enums§

ScheduleAt
The execution schedule for a particular task.

Type Aliases§

DateTime
Duration
Alias of TimeDelta.