diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index 6e467cf..c1e92a7 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -91,7 +91,7 @@ impl Core { // timer initialize let mut timer = self.timer.lock(); timer.set_core(self.clone()); - log_if_err!(timer.refresh()); + log_if_err!(timer.restore()); } /// save the window instance diff --git a/src-tauri/src/core/timer.rs b/src-tauri/src/core/timer.rs index 3e5ccb9..57341f9 100644 --- a/src-tauri/src/core/timer.rs +++ b/src-tauri/src/core/timer.rs @@ -3,6 +3,8 @@ use crate::log_if_err; use anyhow::{bail, Context, Result}; use delay_timer::prelude::{DelayTimer, DelayTimerBuilder, TaskBuilder}; use std::collections::HashMap; +use std::ops::Mul; +use std::time::{SystemTime, UNIX_EPOCH}; type TaskID = u64; @@ -63,6 +65,38 @@ impl Timer { Ok(()) } + /// restore timer + pub fn restore(&mut self) -> Result<()> { + log_if_err!(self.refresh()); + let profiles = self.core.as_ref().unwrap().profiles.lock(); + let cur_timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() as usize; + + for item in profiles.get_items().unwrap() { + // if current_time - last_update_time >= interval, cron job should execute immediately. + if cur_timestamp - item.updated.unwrap() + >= item + .option + .as_ref() + .unwrap() + .update_interval + .unwrap_or(0xffffffff) + .mul(60) // minute to secs + .try_into() + .unwrap() + { + let (task_id, _) = self + .timer_map + .get(&item.uid.as_ref().unwrap().clone()) + .unwrap(); + log_if_err!(self.delay_timer.advance_task(*task_id)); + } + } + Ok(()) + } + /// generate a uid -> update_interval map fn gen_map(&self) -> HashMap { let profiles = self.core.as_ref().unwrap().profiles.lock();