Struct 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§

Source§

impl Scheduler

Source

pub fn new() -> Self

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

Source

pub fn with_tz<Tz: TimeZone>(tz: Tz) -> Scheduler<Tz>

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

Source

pub fn with_tz_and_provider<Tz: TimeZone, Tp: TimeProvider>( tz: Tz, ) -> Scheduler<Tz, Tp>

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.

Source§

impl<Tz, Tp> Scheduler<Tz, Tp>
where Tz: TimeZone + Sync + Send, Tp: TimeProvider,

Source

pub fn every(&mut self, ival: Interval) -> &mut SyncJob<Tz, Tp>

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"));
Source

pub fn run_pending(&mut self)

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));
}
Source§

impl<Tz> Scheduler<Tz>
where Tz: TimeZone + Sync + Send + 'static, <Tz as TimeZone>::Offset: Send,

Source

pub fn watch_thread(self, frequency: Duration) -> ScheduleHandle

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§

Source§

impl<Tz, Tp> Debug for Scheduler<Tz, Tp>
where Tz: TimeZone + Debug, Tp: TimeProvider + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Scheduler

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Tz, Tp> Freeze for Scheduler<Tz, Tp>
where Tz: Freeze,

§

impl<Tz = Local, Tp = ChronoTimeProvider> !RefUnwindSafe for Scheduler<Tz, Tp>

§

impl<Tz, Tp> Send for Scheduler<Tz, Tp>
where Tz: Send, Tp: Send, <Tz as TimeZone>::Offset: Send,

§

impl<Tz = Local, Tp = ChronoTimeProvider> !Sync for Scheduler<Tz, Tp>

§

impl<Tz, Tp> Unpin for Scheduler<Tz, Tp>
where Tz: Unpin, Tp: Unpin, <Tz as TimeZone>::Offset: Unpin,

§

impl<Tz = Local, Tp = ChronoTimeProvider> !UnwindSafe for Scheduler<Tz, Tp>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.