#scheduler #job

clokwerk

A simple Rust recurring task scheduler, similar to Python’s schedule

5 unstable releases

0.3.0 Dec 29, 2019
0.2.2 Aug 13, 2019
0.2.1 Jul 5, 2019
0.2.0 Jul 3, 2019
0.1.0 Sep 7, 2018

#7 in Date and time

Download history 2351/week @ 2019-11-01 2115/week @ 2019-11-08 1315/week @ 2019-11-15 1583/week @ 2019-11-22 1441/week @ 2019-11-29 1821/week @ 2019-12-06 850/week @ 2019-12-13 298/week @ 2019-12-20 363/week @ 2019-12-27 303/week @ 2020-01-03 300/week @ 2020-01-10 372/week @ 2020-01-17 194/week @ 2020-01-24 199/week @ 2020-01-31 305/week @ 2020-02-07

6,009 downloads per month
Used in 5 crates (3 directly)

Apache-2.0

45KB
900 lines

Clokwerk, a simple scheduler

Clokwerk is a simple scheduler, inspired by Python's Schedule and Ruby's clockwork. It uses a similar DSL for scheduling, rather than parsing cron strings.

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

Usage

// Scheduler, and trait for .seconds(), .minutes(), etc.
use clokwerk::{Scheduler, TimeUnits};
// 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"));

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

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.

Similar libraries

Dependencies

~760KB
~10K SLoC