Skip to content

Commit

Permalink
Some details emerging
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Oct 1, 2024
1 parent 549f243 commit bf0caaa
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,38 @@ use std::time::SystemTime;

pub struct Scheduler {
running: Arc<AtomicBool>,
signal: Arc<Condvar>,
data: Arc<Mutex<BTreeSet<String>>>,
job_store: Arc<JobStore>,
scheduler_thread: ManuallyDrop<JoinHandle<()>>,
}

impl Scheduler {
pub fn new() -> Self {
let running = Arc::new(AtomicBool::new(true));
let signal = Condvar::new();
let r = Arc::clone(&running);

let job_store = Arc::new(JobStore::new());
let store = Arc::clone(&job_store);

let handle = thread::Builder::new()
.name("Quartz Scheduler Thread".to_string())
.spawn(move || {
while running.load(Ordering::SeqCst) {}
while r.load(Ordering::SeqCst) {
if let Some(job) = store.next_job() {
job.execute();
}
}
})
.expect("");

Self {
running,
signal,
job_store,
scheduler_thread: ManuallyDrop::new(handle),
}
}

pub fn schedule_job(&mut self, _job: JobDetail, _trigger: Trigger) {
todo!()
self.job_store.signal();
}
}

Expand Down Expand Up @@ -85,6 +93,10 @@ impl JobDetail {
pub fn group(&self) -> &str {
&self.group
}

pub fn execute(&self) {
(self.target_fn)();
}
}

impl JobDetail {
Expand All @@ -100,6 +112,7 @@ impl JobDetail {
pub struct Trigger {
id: String,
group: String,
#[allow(dead_code)]
start_time: SystemTime,
}

Expand All @@ -121,6 +134,35 @@ impl Trigger {
}
}

struct JobStore {
signal: Arc<Condvar>,
#[allow(dead_code)]
data: Arc<Mutex<BTreeSet<String>>>,
}

impl JobStore {
fn new() -> Self {
Self {
signal: Arc::new(Default::default()),
data: Arc::new(Mutex::new(Default::default())),
}
}

fn next_job(&self) -> Option<JobDetail> {
None
}

fn signal(&self) {
self.signal.notify_one()
}
}

impl Default for JobStore {
fn default() -> Self {
JobStore::new()
}
}

#[cfg(test)]
mod tests {
use crate::{JobDetail, Scheduler, Trigger};
Expand Down Expand Up @@ -149,12 +191,6 @@ mod tests {
sched.schedule_job(job, trigger);
println!("{job_id} will run at: {run_time:?}");

// Start up the scheduler (nothing can actually run until the
// scheduler has been started)
sched.start();

println!("------- Started Scheduler -----------------");

// wait long enough so that the scheduler as an opportunity to
// run the job!
println!("------- Waiting 65 seconds... -------------");
Expand Down

0 comments on commit bf0caaa

Please sign in to comment.