Rucron: A Pure Rust Implementation Job Scheduling Package.

Crates.io version
docs.rs docs Download

Rucron is a lightweight job scheduler, it is similar to gocron or linux crontab, and it is very easy to use. But now it only supports asynchronous job by the runtime provided by tokio.

Usage

Add this to your Cargo.toml

toml [dependencies] rucron = "*"

Quick start:

```rust use rucron::{sync_execute, execute, EmptyTask, Metric, Scheduler}; use std::{error::Error, sync::atomic::Ordering}; use chrono::{Local, DateTime, Duration};

async fn foo() -> Result<(), Box>{ println!("foo"); Ok(()) }

async fn bar() -> Result<(), Box>{ println!("bar"); Ok(()) }

async fn ping() -> Result<(), Box>{ println!("ping"); Ok(()) }

fn once(m: &Metric, last: &DateTime) -> Duration { let n = m.n_scheduled.load(Ordering::Relaxed); if n < 1 { Duration::seconds(2) } else if n == 1 { Duration::seconds(last.timestamp() * 2) } else { Duration::seconds(0) } }

fn synctask() -> Result<(), Box> { std::thread::sleep(std::time::Duration::fromsecs(2)); println!("sync task"); Ok(()) }

[tokio::main]

async fn main(){ // Create a scheduler with 10 capacity, it will checkout all runnable jobs every second let sch = Scheduler::execute(synctask)).await; // Start running all jobs. sch.start().await; } ``` Schedule parameterized job:

```rust use rucron::{execute, ArgStorage, EmptyTask, ParseArgs, Scheduler}; use std::error::Error; use asynctrait::asynctrait;

[derive(Clone)]

struct Person { age: i32, }

[async_trait]

impl ParseArgs for Person { type Err = std::io::Error; fn parse_args(args: &ArgStorage) -> Result { return Ok(args.get::().unwrap().clone()); } }

async fn iseightyears_old(p: Person) -> Result<(), Box> { if p.age == 8 { println!("I am eight years old!"); } else { println!("Oops!"); }; Ok(()) }

[tokio::main]

async fn main() { let child = Person { age: 8 }; // Storage stores all arguments. let mut arg = ArgStorage::new(); arg.insert(child); let mut sch = Scheduler::argstorage(arg); let sch = sch.every(2).second().todo(execute(iseightyears_old)).await; sch.start().await; } ```

You could also schedule blocking or CPU-bound tasks.

```rust use rucron::{sync_execute, ArgStorage, EmptyTask, ParseArgs, Scheduler}; use std::error::Error;

[derive(Clone)]

struct Person { age: i32, }

impl ParseArgs for Person { type Err = std::io::Error; fn parse_args(args: &ArgStorage) -> Result { return Ok(args.get::().unwrap().clone()); } }

fn syncsetage(p: Person) -> Result<(), Box> { if p.age == 8 { println!("I am eight years old!"); }; Ok(()) }

[tokio::main]

async fn main() { let child = Person { age: 8 }; let mut arg = ArgStorage::new(); arg.insert(child); let mut sch = Scheduler::argstorage(arg); let sch = sch.every(2).second().todo(syncexecute(syncset_age)).await; sch.start().await; } ```

If you want to schedule jobs with distributed locks, please see [examples] directory.

To Do List

License

Rucron is licensed under the MIT license.

Contributing

Contributions are welcome. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the MIT license, shall be dual licensed as above, without any additional terms or conditions.