Skip to content

Commit 3b837e8

Browse files
authored
Some cleanups and clippy fixes (#4)
- 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
1 parent 9745e4c commit 3b837e8

File tree

7 files changed

+149
-87
lines changed

7 files changed

+149
-87
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "job_scheduler_ng"
3-
version = "2.0.4"
3+
version = "2.0.5"
44
authors = ["Mathijs van Veluw <black.dex@gmail.com>"]
55
description = "A simple cron-like job scheduling library for Rust (Updated since 2022)."
66
documentation = "https://docs.rs/job_scheduler_ng/"
@@ -10,12 +10,12 @@ license = "MIT OR Apache-2.0"
1010
keywords = ["cron", "crontab", "scheduler", "job"]
1111
categories = ["date-and-time"]
1212
edition = "2021"
13-
rust-version = "1.56.1" # Examples need v1.58 to be able to run.
13+
rust-version = "1.61.0"
1414

1515
[dependencies]
1616
cron = "0.12.0"
17-
chrono = { version = "~0.4.20", default-features = false, features = ["clock"] }
17+
chrono = { version = "~0.4.34", default-features = false, features = ["clock"] }
1818
uuid = { version = "1", features = ["v4"] }
1919

2020
[dev-dependencies]
21-
tokio = { version = ">=1.25.0", features = ["macros", "time", "rt-multi-thread"] }
21+
tokio = { version = ">=1.37", features = ["macros", "time", "rt-multi-thread"] }

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ This is a fork which i try to maintain and maybe even improve where needed.
99

1010
## Updates
1111

12+
**2024-04-24 (v2.0.5):**
13+
- Set MSRV to v1.61.0 to match chrono's v0.4.34 MSRV
14+
- Updated dev dependency of tokio to v1.37.0 or higher
15+
- Several clippy check added
16+
- Fixed all clippy reported items
17+
- Set JobScheduler::new() as `const fn`
18+
- Updated examples to use a `log` function and always print the current thread id
19+
- Added a very simple hash to better differentiate the tokio 5th second example
20+
21+
1222
**2023-02-01 (v2.0.4):**
1323
- Validated uuid v1.3.0 works
1424
- Used miri to check examples

examples/simple_job.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
1+
use core::time::Duration;
12
use job_scheduler_ng::{Job, JobScheduler};
2-
use std::time::Duration;
3+
use std::time::Instant;
34

45
fn main() {
6+
const WAIT_SECONDS: u64 = 40;
7+
58
let mut sched = JobScheduler::new();
69

7-
sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || {
8-
println!(
9-
"{:?} - I get executed every 10 seconds!",
10-
chrono::Utc::now()
11-
);
10+
sched.add(Job::new("0/10 * * * * *".parse().unwrap(), || {
11+
log("I get executed every 10th second!");
1212
}));
1313

1414
sched.add(Job::new("*/4 * * * * *".parse().unwrap(), || {
15-
println!("{:?} - I get executed every 4 seconds!", chrono::Utc::now());
15+
log("I get executed every 4 seconds!");
1616
}));
1717

18-
println!("{:?} - Starting loop", chrono::Utc::now());
18+
log(&format!("Run for about {WAIT_SECONDS} seconds!"));
19+
log("Starting loop");
20+
let start = Instant::now();
1921
loop {
2022
sched.tick();
2123

2224
std::thread::sleep(Duration::from_millis(500));
25+
26+
// Check if we have waited long enough
27+
if start.elapsed().as_secs() >= WAIT_SECONDS {
28+
break;
29+
}
2330
}
31+
log("Finished. Goodby!");
32+
std::process::exit(0);
33+
}
34+
35+
fn log(msg: &str) {
36+
println!(
37+
"{:?} - {:?} - {msg}",
38+
chrono::Utc::now(),
39+
std::thread::current().id()
40+
);
2441
}

examples/simple_job_mpsc.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
use core::time::Duration;
12
use job_scheduler_ng::{Job, JobScheduler};
23
use std::sync::mpsc::{channel, Receiver, Sender};
3-
use std::time::Duration;
44

55
fn main() {
6+
const WAIT_SECONDS: u64 = 40;
7+
68
let mut sched = JobScheduler::new();
79

8-
sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || {
9-
println!(
10-
"{:?} - I get executed every 10 seconds!",
11-
chrono::Utc::now()
12-
);
10+
sched.add(Job::new("0/10 * * * * *".parse().unwrap(), || {
11+
log("I get executed every 10th second!");
1312
}));
1413

1514
sched.add(Job::new("*/4 * * * * *".parse().unwrap(), || {
16-
println!("{:?} - I get executed every 4 seconds!", chrono::Utc::now());
15+
log("I get executed every 4 seconds!");
1716
}));
1817

1918
// Create a Send/Receive channel using a String
@@ -23,24 +22,21 @@ fn main() {
2322
std::thread::Builder::new()
2423
.name(String::from("channel-receiver"))
2524
.spawn(move || {
26-
println!(
27-
"{:?} - Starting channel receiver loop within thread",
28-
chrono::Utc::now()
29-
);
25+
log("Starting channel receiver loop within thread");
3026
loop {
3127
if let Ok(msg) = rx.recv() {
32-
println!("{:?} - rx: {msg}", chrono::Utc::now());
28+
log(&format!("rx: {msg}"));
3329
}
3430
std::thread::sleep(Duration::from_millis(500));
3531
}
3632
})
3733
.expect("Error spawning channel-receiver thread");
3834

3935
// Create a job which sends a message via the channel
40-
sched.add(Job::new("*/5 * * * * *".parse().unwrap(), {
36+
sched.add(Job::new("0/5 * * * * *".parse().unwrap(), {
4137
move || {
4238
tx.send(String::from(
43-
"I get executed every 5 seconds and send an mpsc!",
39+
"I get executed every 5th second and send an mpsc!",
4440
))
4541
.unwrap();
4642
}
@@ -49,20 +45,24 @@ fn main() {
4945
std::thread::Builder::new()
5046
.name(String::from("job-scheduler"))
5147
.spawn(move || {
52-
println!(
53-
"{:?} - Starting job scheduler loop within thread",
54-
chrono::Utc::now()
55-
);
48+
log("Starting job scheduler loop within thread");
5649
loop {
5750
sched.tick();
5851
std::thread::sleep(Duration::from_millis(500));
5952
}
6053
})
6154
.expect("Error spawning job-scheduler thread");
6255

63-
let wait_seconds: u64 = 40;
64-
println!("Waiting for {wait_seconds} seconds!");
65-
std::thread::sleep(Duration::from_secs(wait_seconds));
66-
println!("Finished. Goodby!");
56+
log(&format!("Run for about {WAIT_SECONDS} seconds!"));
57+
std::thread::sleep(Duration::from_secs(WAIT_SECONDS));
58+
log("Finished. Goodby!");
6759
std::process::exit(0);
6860
}
61+
62+
fn log(msg: &str) {
63+
println!(
64+
"{:?} - {:?} - {msg}",
65+
chrono::Utc::now(),
66+
std::thread::current().id()
67+
);
68+
}

examples/simple_job_thread.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1+
use core::time::Duration;
12
use job_scheduler_ng::{Job, JobScheduler};
2-
use std::time::Duration;
33

44
fn main() {
5+
const WAIT_SECONDS: u64 = 40;
6+
57
let mut sched = JobScheduler::new();
68

7-
sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || {
8-
println!(
9-
"{:?} - I get executed every 10 seconds!",
10-
chrono::Utc::now()
11-
);
9+
sched.add(Job::new("0/10 * * * * *".parse().unwrap(), || {
10+
log("I get executed every 10th second!");
1211
}));
1312

1413
sched.add(Job::new("*/4 * * * * *".parse().unwrap(), || {
15-
println!("{:?} - I get executed every 4 seconds!", chrono::Utc::now());
14+
log("I get executed every 4 seconds!");
1615
}));
1716

1817
std::thread::Builder::new()
1918
.name(String::from("job-scheduler"))
2019
.spawn(move || {
21-
println!("{:?} - Starting loop within thread", chrono::Utc::now());
20+
log("Starting loop within thread");
2221
loop {
2322
sched.tick();
2423
std::thread::sleep(Duration::from_millis(500));
2524
}
2625
})
2726
.expect("Error spawning job-scheduler thread");
2827

29-
let wait_seconds: u64 = 40;
30-
println!("Waiting for {wait_seconds} seconds!");
31-
std::thread::sleep(Duration::from_secs(wait_seconds));
32-
println!("Finished. Goodby!");
28+
log(&format!("Run for about {WAIT_SECONDS} seconds!"));
29+
std::thread::sleep(Duration::from_secs(WAIT_SECONDS));
30+
log("Finished. Goodby!");
3331
std::process::exit(0);
3432
}
33+
34+
fn log(msg: &str) {
35+
println!(
36+
"{:?} - {:?} - {msg}",
37+
chrono::Utc::now(),
38+
std::thread::current().id()
39+
);
40+
}

examples/simple_job_tokio.rs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
use core::time::Duration;
12
use job_scheduler_ng::{Job, JobScheduler};
2-
use std::time::Duration;
3+
use std::hash::{DefaultHasher, Hash, Hasher};
34

45
#[tokio::main]
56
async fn main() {
6-
println!("Initializing scheduler!");
7-
init_scheduler().await;
7+
const WAIT_SECONDS: u64 = 40;
88

9-
let wait_seconds: u64 = 40;
10-
println!("Waiting for {wait_seconds} seconds!");
11-
tokio::time::sleep(Duration::from_secs(wait_seconds)).await;
12-
println!("Finished. Goodby!");
9+
log("Initializing scheduler!");
10+
init_scheduler();
11+
12+
log(&format!("Run for about {WAIT_SECONDS} seconds!"));
13+
tokio::time::sleep(Duration::from_secs(WAIT_SECONDS)).await;
14+
log("Finished. Goodby!");
1315
std::process::exit(0);
1416
}
1517

16-
async fn init_scheduler() {
18+
fn init_scheduler() {
1719
// Start a new runtime to not mess with the current running one
1820
let runtime = tokio::runtime::Runtime::new().unwrap();
1921

@@ -24,26 +26,23 @@ async fn init_scheduler() {
2426

2527
let mut sched = JobScheduler::new();
2628

27-
sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || {
28-
println!(
29-
"{:?} - I get executed every 10 seconds!",
30-
chrono::Utc::now()
31-
);
29+
sched.add(Job::new("0/10 * * * * *".parse().unwrap(), || {
30+
log("I get executed every 10th second!");
3231
}));
3332

3433
sched.add(Job::new("*/4 * * * * *".parse().unwrap(), || {
35-
println!("{:?} - I get executed every 4 seconds!", chrono::Utc::now());
34+
log("I get executed every 4 seconds!");
3635
}));
3736

38-
sched.add(Job::new("*/5 * * * * *".parse().unwrap(), || {
37+
sched.add(Job::new("0/5 * * * * *".parse().unwrap(), || {
3938
runtime.spawn(test_job_every_five());
4039
}));
4140

4241
sched.add(Job::new("*/8 * * * * *".parse().unwrap(), || {
4342
runtime.spawn(test_job_every_eight());
4443
}));
4544

46-
println!("{:?} - Starting loop", chrono::Utc::now());
45+
log("Starting loop");
4746
loop {
4847
sched.tick();
4948
runtime.block_on(async move {
@@ -55,19 +54,32 @@ async fn init_scheduler() {
5554
}
5655

5756
async fn test_job_every_five() {
58-
println!(
59-
"{:?} - I get executed every 5 seconds (begin)!",
60-
chrono::Utc::now()
61-
);
62-
// Wait 6 seconds, this will demonstrate this call will be async and not blocking.
63-
tokio::time::sleep(Duration::from_secs(6)).await;
64-
println!(
65-
"{:?} - I get executed every 5 seconds (end)!",
66-
chrono::Utc::now()
67-
);
57+
let hash = hash(&chrono::Utc::now().timestamp_millis());
58+
log(&format!(
59+
"I get executed every 5th second (begin - {hash})!"
60+
));
61+
// Wait 7 seconds, this will demonstrate this call will be async and not blocking.
62+
tokio::time::sleep(Duration::from_secs(7)).await;
63+
log(&format!(
64+
"I get executed every 5th second (end - {hash})!"
65+
));
6866
}
6967

7068
async fn test_job_every_eight() {
7169
tokio::time::sleep(Duration::from_millis(500)).await;
72-
println!("{:?} - I get executed every 8 seconds!", chrono::Utc::now());
70+
log("I get executed every 8 seconds!");
71+
}
72+
73+
fn log(msg: &str) {
74+
println!(
75+
"{:?} - {:?} - {msg}",
76+
chrono::Utc::now(),
77+
std::thread::current().id()
78+
);
79+
}
80+
81+
fn hash<T: Hash>(t: &T) -> u64 {
82+
let mut s = DefaultHasher::new();
83+
t.hash(&mut s);
84+
s.finish()
7385
}

0 commit comments

Comments
 (0)