Added logic for ringer and scheduler. still wip

This commit is contained in:
2026-01-16 19:50:41 +01:00
parent d6ef884325
commit 45c332e610
8 changed files with 287 additions and 37 deletions

40
src/scheduler.rs Normal file
View 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();
}
}