Struct clokwerk::Scheduler[][src]

pub struct Scheduler<Tz = Local, Tp = ChronoTimeProvider> where
    Tz: TimeZone,
    Tp: TimeProvider
{ /* fields omitted */ }

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

impl Scheduler[src]

pub fn new() -> Self[src]

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

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

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

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

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.

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

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

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

pub fn run_pending(&mut self)[src]

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

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

#[must_use = "The scheduler is halted when the returned handle is dropped"]
pub fn watch_thread(self, frequency: Duration) -> ScheduleHandle
[src]

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

impl<Tz: Debug, Tp: Debug> Debug for Scheduler<Tz, Tp> where
    Tz: TimeZone,
    Tp: TimeProvider
[src]

impl Default for Scheduler[src]

Auto Trait Implementations

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

impl<Tz, Tp> Send for Scheduler<Tz, Tp> where
    Tp: Send,
    Tz: 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
    Tp: Unpin,
    Tz: Unpin,
    <Tz as TimeZone>::Offset: Unpin

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.