Struct clokwerk::Scheduler

source ·
pub struct Scheduler<Tz = Local, Tp = ChronoTimeProvider>where
    Tz: TimeZone,
    Tp: TimeProvider,
{ /* private fields */ }
Expand description

Synchronous job scheduler

Usage examples

// Scheduler, trait for .seconds(), .minutes(), etc., and trait with job scheduling methods
use clokwerk::{Scheduler, TimeUnits, Job};
// Import week days and WeekDay
use clokwerk::Interval::*;
use std::thread;
use std::time::Duration;

// Create a new scheduler
let mut scheduler = Scheduler::new();
// or a scheduler with a given timezone
let mut scheduler = Scheduler::with_tz(chrono::Utc);
// Add some tasks to it
scheduler
    .every(10.minutes())
        .plus(30.seconds())
    .run(|| println!("Periodic task"));
scheduler
    .every(1.day())
        .at("3:20 pm")
    .run(|| println!("Daily task"));
scheduler
    .every(Wednesday)
        .at("14:20:17")
    .run(|| println!("Weekly task"));
scheduler
    .every(Tuesday)
        .at("14:20:17")
    .and_every(Thursday)
        .at("15:00")
    .run(|| println!("Biweekly task"));
scheduler
    .every(Weekday)
    .run(|| println!("Every weekday at midnight"));
scheduler
    .every(1.day())
        .at("3:20 pm")
    .run(|| println!("I only run once")).once();
scheduler
    .every(Weekday)
        .at("12:00").count(10)
    .run(|| println!("Countdown"));
scheduler
    .every(1.day())
        .at("10:00 am")
        .repeating_every(30.minutes())
            .times(6)
    .run(|| println!("I run every half hour from 10 AM to 1 PM inclusive."));
scheduler
    .every(1.day())
        .at_time(chrono::NaiveTime::from_hms(13, 12, 14))
    .run(|| println!("You can also pass chrono::NaiveTimes to `at_time`."));

// Manually run the scheduler in an event loop
for _ in 1..10 {
    scheduler.run_pending();
    thread::sleep(Duration::from_millis(10));
}

// Or run it in a background thread
let thread_handle = scheduler.watch_thread(Duration::from_millis(100));
// The scheduler stops when `thread_handle` is dropped, or `stop` is called
thread_handle.stop();

Implementations

Create a new scheduler. Dates and times will be interpretted using the local timezone

Create a new scheduler. Dates and times will be interpretted using the specified timezone.

Create a new scheduler. Dates and times will be interpretted using the specified timezone. In addition, you can provide an alternate time provider. This is mostly useful for writing tests.

Add a new job to the scheduler to be run on the given interval

let mut scheduler = Scheduler::new();
scheduler.every(10.minutes()).plus(30.seconds()).run(|| println!("Periodic task"));
scheduler.every(1.day()).at("3:20 pm").run(|| println!("Daily task"));
scheduler.every(Wednesday).at("14:20:17").run(|| println!("Weekly task"));
scheduler.every(Weekday).run(|| println!("Every weekday at midnight"));

Run all jobs that should run at this time.

This method blocks while jobs are being run. If a job takes a long time, it may prevent other tasks from running as scheduled. If you have a long-running task, you might consider having the job move the work into another thread so that it can return promptly.

use std::thread;
use std::time::Duration;
loop {
    scheduler.run_pending();
    thread::sleep(Duration::from_millis(100));
}

Start a background thread to call Scheduler::run_pending() repeatedly. The frequency argument controls how long the thread will sleep between calls to Scheduler::run_pending(). If the returned ScheduleHandle is dropped, the resulting thread will end cleanly when Scheduler::run_pending() would have next been called.

Passing large durations for frequency can cause long delays when ScheduleHandle::stop() is called, or the ScheduleHandle is dropped, as it waits for the thread to finish sleeping. This could affect how long it takes for the program to exit.

Reasonable values for frequency would be between 100 ms and 10 seconds. If in doubt, choose a smaller value.

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.