Skip to content

Commit

Permalink
improve cron
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuyen Tran committed Sep 8, 2024
1 parent 4e94e4d commit 301bf7d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: tarpaulin
args: --all --verbose --all-features --out Lcov -- --test-threads 1
args: --all --verbose --all-features --out Lcov -- --test-threads 4

- name: Upload to CodeCov
uses: codecov/codecov-action@v4
Expand Down
31 changes: 15 additions & 16 deletions src/cron.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
Expand All @@ -20,10 +20,14 @@ where
entries: Arc<Mutex<Vec<Entry<Z>>>>,
next_id: Arc<AtomicUsize>,
tz: Z,
add_tx: crossbeam_channel::Sender<Entry<Z>>,
stop_tx: crossbeam_channel::Sender<bool>,
add_rx: crossbeam_channel::Receiver<Entry<Z>>,
stop_rx: crossbeam_channel::Receiver<bool>,
add_channel: (
crossbeam_channel::Sender<Entry<Z>>,
crossbeam_channel::Receiver<Entry<Z>>,
),
stop_channel: (
crossbeam_channel::Sender<bool>,
crossbeam_channel::Receiver<bool>,
),
}

/// Cron contains and executes the scheduled jobs.
Expand All @@ -42,17 +46,12 @@ where
/// cron.start();
/// ```
pub fn new(tz: Z) -> Cron<Z> {
let (add_tx, add_rx) = crossbeam_channel::unbounded();
let (stop_tx, stop_rx) = crossbeam_channel::unbounded();

Cron {
entries: Arc::new(Mutex::new(Vec::new())),
next_id: Arc::new(AtomicUsize::new(0)),
tz,
add_tx,
stop_tx,
add_rx,
stop_rx,
add_channel: crossbeam_channel::unbounded(),
stop_channel: crossbeam_channel::unbounded(),
}
}

Expand Down Expand Up @@ -89,7 +88,7 @@ where
};

entry.next = entry.schedule_next(self.get_timezone());
self.add_tx.send(entry).unwrap();
self.add_channel.0.send(entry).unwrap();

Ok(next_id)
}
Expand Down Expand Up @@ -145,7 +144,7 @@ where
/// cron.stop();
/// ```
pub fn stop(&self) {
self.stop_tx.send(true).unwrap()
self.stop_channel.0.send(true).unwrap()
}

/// Start cron.
Expand Down Expand Up @@ -208,12 +207,12 @@ where
}
},
// wait add new entry
recv(self.add_rx) -> new_entry => {
recv(self.add_channel.1) -> new_entry => {

Check warning on line 210 in src/cron.rs

View check run for this annotation

Codecov / codecov/patch

src/cron.rs#L210

Added line #L210 was not covered by tests
let mut entry = new_entry.unwrap();
entry.next = entry.schedule_next(self.get_timezone());
self.entries.lock().unwrap().push(entry);
},
recv(self.stop_rx) -> _ => {
recv(self.stop_channel.1) -> _ => {

Check warning on line 215 in src/cron.rs

View check run for this annotation

Codecov / codecov/patch

src/cron.rs#L215

Added line #L215 was not covered by tests
return;
},
}
Expand Down

0 comments on commit 301bf7d

Please sign in to comment.