Skip to main content

Crate uhrwerk

Crate uhrwerk 

Source
Expand description

Uhrwerk is a simple scheduler, forked from clokwerk, which itself is inspired by Python’s Schedule and Ruby’s clockwork. It uses a similar DSL for scheduling, rather than parsing cron strings.

Uhrwerk supports both synchronous and asynchronous tasks.

§Usage

// Import week days and WeekDay
use uhrwerk::Interval::*;
// Scheduler and trait for .seconds(), .minutes(), etc.
use uhrwerk::{Job as _, Scheduler, TimeUnits as _};

// Create a new scheduler
let mut scheduler = Scheduler::new();
// or a scheduler with a given timezone
let mut scheduler = Scheduler::new_with_utc_offset(UtcOffset::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(Tuesday)
	.at("14:20:17")
	.and_every(Thursday)
	.at("15:00")
	.run(|| println!("Biweekly task"));

// 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();

By default, dates and times are relative to the local timezone, but the scheduler can be made to use a different timezone using Scheduler::new_with_utc_offset.

For more details, see Scheduler or its async counterpart, AsyncScheduler.

§Caveats

Some combinations of times or intervals are permissible, but make little sense, e.g. every(10.seconds()).at("16:00"), which would next run at the next 4 PM after the next multiple of 10 seconds.

§Trivia

Uhrwerk is the German word for clockwork.

Modules§

timeprovider

Structs§

AsyncJob
An asynchronous job to run on the scheduler. Create these by calling AsyncScheduler::every().
AsyncScheduler
An asynchronous job scheduler, for use with Futures.
ScheduleHandle
Guard object for the scheduler background thread. The thread is terminated if this object is dropped, or ScheduleHandle::stop() is called
Scheduler
Synchronous job scheduler
SyncJob
A job to run on the scheduler. Create these by calling Scheduler::every().

Enums§

Interval

Traits§

Job
This trait provides an abstraction over SyncJob and AsyncJob, covering all the methods relating to scheduling, rather than execution.
NextTime
TimeUnits
A trait for easily expressing common intervals. Each method generates an appropriate Interval. Plural and non-plural forms behave identically, but exist to make code more grammatical.