Added logic for ringer and scheduler. still wip
This commit is contained in:
40
src/scheduler.rs
Normal file
40
src/scheduler.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use chrono::Local;
|
||||
use cron_tab::Cron;
|
||||
|
||||
use crate::ringer::Ringer;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Scheduler<T: Ringer + 'static> {
|
||||
ringer: Arc<Mutex<T>>,
|
||||
cron: Arc<Mutex<Cron<Local>>>,
|
||||
}
|
||||
|
||||
impl<T: Ringer> Scheduler<T> {
|
||||
pub fn new(ringer: Arc<Mutex<T>>, cron: Arc<Mutex<Cron<Local>>>) -> Self {
|
||||
Self { ringer, cron }
|
||||
}
|
||||
|
||||
pub fn schedule(&self, cron_schedule: &str) -> Result<(), String> {
|
||||
let ringer = self.ringer.clone();
|
||||
let cron = self.cron.clone();
|
||||
|
||||
let job_result = cron
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.add_fn(cron_schedule, move || {
|
||||
let guard = ringer.lock();
|
||||
if let Ok(ringer) = guard {
|
||||
ringer.ring().unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
job_result.expect("Faild to add job");
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn start(&self) {
|
||||
self.cron.lock().expect("Failed to lock cron").start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user