forked from lholden/job_scheduler
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Set MSRV to v1.61.0 to match chrono's v0.4.34 MSRV - Updated dev dependency of tokio to v1.37.0 or higher - Several clippy check added - Fixed all clippy reported items - Set JobScheduler::new() as `const fn` - Updated examples to use a `log` function and always print the current thread id - Added a very simple hash to better differentiate the tokio 5th second example
- Loading branch information
Showing
7 changed files
with
149 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "job_scheduler_ng" | ||
version = "2.0.4" | ||
version = "2.0.5" | ||
authors = ["Mathijs van Veluw <[email protected]>"] | ||
description = "A simple cron-like job scheduling library for Rust (Updated since 2022)." | ||
documentation = "https://docs.rs/job_scheduler_ng/" | ||
|
@@ -10,12 +10,12 @@ license = "MIT OR Apache-2.0" | |
keywords = ["cron", "crontab", "scheduler", "job"] | ||
categories = ["date-and-time"] | ||
edition = "2021" | ||
rust-version = "1.56.1" # Examples need v1.58 to be able to run. | ||
rust-version = "1.61.0" | ||
|
||
[dependencies] | ||
cron = "0.12.0" | ||
chrono = { version = "~0.4.20", default-features = false, features = ["clock"] } | ||
chrono = { version = "~0.4.34", default-features = false, features = ["clock"] } | ||
uuid = { version = "1", features = ["v4"] } | ||
|
||
[dev-dependencies] | ||
tokio = { version = ">=1.25.0", features = ["macros", "time", "rt-multi-thread"] } | ||
tokio = { version = ">=1.37", features = ["macros", "time", "rt-multi-thread"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
use core::time::Duration; | ||
use job_scheduler_ng::{Job, JobScheduler}; | ||
use std::time::Duration; | ||
use std::time::Instant; | ||
|
||
fn main() { | ||
const WAIT_SECONDS: u64 = 40; | ||
|
||
let mut sched = JobScheduler::new(); | ||
|
||
sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || { | ||
println!( | ||
"{:?} - I get executed every 10 seconds!", | ||
chrono::Utc::now() | ||
); | ||
sched.add(Job::new("0/10 * * * * *".parse().unwrap(), || { | ||
log("I get executed every 10th second!"); | ||
})); | ||
|
||
sched.add(Job::new("*/4 * * * * *".parse().unwrap(), || { | ||
println!("{:?} - I get executed every 4 seconds!", chrono::Utc::now()); | ||
log("I get executed every 4 seconds!"); | ||
})); | ||
|
||
println!("{:?} - Starting loop", chrono::Utc::now()); | ||
log(&format!("Run for about {WAIT_SECONDS} seconds!")); | ||
log("Starting loop"); | ||
let start = Instant::now(); | ||
loop { | ||
sched.tick(); | ||
|
||
std::thread::sleep(Duration::from_millis(500)); | ||
|
||
// Check if we have waited long enough | ||
if start.elapsed().as_secs() >= WAIT_SECONDS { | ||
break; | ||
} | ||
} | ||
log("Finished. Goodby!"); | ||
std::process::exit(0); | ||
} | ||
|
||
fn log(msg: &str) { | ||
println!( | ||
"{:?} - {:?} - {msg}", | ||
chrono::Utc::now(), | ||
std::thread::current().id() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
use core::time::Duration; | ||
use job_scheduler_ng::{Job, JobScheduler}; | ||
use std::time::Duration; | ||
|
||
fn main() { | ||
const WAIT_SECONDS: u64 = 40; | ||
|
||
let mut sched = JobScheduler::new(); | ||
|
||
sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || { | ||
println!( | ||
"{:?} - I get executed every 10 seconds!", | ||
chrono::Utc::now() | ||
); | ||
sched.add(Job::new("0/10 * * * * *".parse().unwrap(), || { | ||
log("I get executed every 10th second!"); | ||
})); | ||
|
||
sched.add(Job::new("*/4 * * * * *".parse().unwrap(), || { | ||
println!("{:?} - I get executed every 4 seconds!", chrono::Utc::now()); | ||
log("I get executed every 4 seconds!"); | ||
})); | ||
|
||
std::thread::Builder::new() | ||
.name(String::from("job-scheduler")) | ||
.spawn(move || { | ||
println!("{:?} - Starting loop within thread", chrono::Utc::now()); | ||
log("Starting loop within thread"); | ||
loop { | ||
sched.tick(); | ||
std::thread::sleep(Duration::from_millis(500)); | ||
} | ||
}) | ||
.expect("Error spawning job-scheduler thread"); | ||
|
||
let wait_seconds: u64 = 40; | ||
println!("Waiting for {wait_seconds} seconds!"); | ||
std::thread::sleep(Duration::from_secs(wait_seconds)); | ||
println!("Finished. Goodby!"); | ||
log(&format!("Run for about {WAIT_SECONDS} seconds!")); | ||
std::thread::sleep(Duration::from_secs(WAIT_SECONDS)); | ||
log("Finished. Goodby!"); | ||
std::process::exit(0); | ||
} | ||
|
||
fn log(msg: &str) { | ||
println!( | ||
"{:?} - {:?} - {msg}", | ||
chrono::Utc::now(), | ||
std::thread::current().id() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.