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
impl Scheduler
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new scheduler. Dates and times will be interpretted using the local timezone
Sourcepub fn with_tz<Tz: TimeZone>(tz: Tz) -> Scheduler<Tz>
pub fn with_tz<Tz: TimeZone>(tz: Tz) -> Scheduler<Tz>
Create a new scheduler. Dates and times will be interpretted using the specified timezone.
Sourcepub fn with_tz_and_provider<Tz: TimeZone, Tp: TimeProvider>(
tz: Tz,
) -> Scheduler<Tz, Tp>
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>
impl<Tz, Tp> Scheduler<Tz, Tp>
Sourcepub fn every(&mut self, ival: Interval) -> &mut SyncJob<Tz, Tp>
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"));
Sourcepub fn run_pending(&mut self)
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>
impl<Tz> Scheduler<Tz>
Sourcepub fn watch_thread(self, frequency: Duration) -> ScheduleHandle
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.