use std::time::Duration;
use super::times::Timestamp;
#[derive(candid::CandidType, candid::Deserialize, Debug, Clone)]
pub struct HeartbeatConfig {
pub enabled: bool, pub last: Timestamp, pub sleep: Timestamp, }
impl Default for HeartbeatConfig {
fn default() -> Self {
Self {
enabled: true,
last: 0,
sleep: 1000000 * 1000 * 3600, }
}
}
impl HeartbeatConfig {
pub fn beat(&mut self) -> Option<Timestamp> {
if !self.enabled {
return None;
}
let now = super::times::now();
if now < self.last + self.sleep {
return None;
}
self.last = now;
Some(now)
}
}
pub use ic_cdk_timers::TimerId;
#[derive(candid::CandidType, candid::Deserialize, Debug, Clone)]
pub struct ScheduleConfig {
pub enabled: bool, pub interval: Timestamp, }
impl Default for ScheduleConfig {
fn default() -> Self {
Self {
enabled: true,
interval: 1000000 * 1000 * 3600, }
}
}
impl ScheduleConfig {
pub fn clear(&self, timer_id: Option<TimerId>) {
if timer_id.is_some() {
ic_cdk_timers::clear_timer(timer_id.unwrap());
}
}
pub fn check(&self, task: impl FnMut() + 'static) -> Option<TimerId> {
if !self.enabled {
return None;
}
let timer_id = ic_cdk_timers::set_timer_interval(Duration::from_nanos(self.interval), task);
Some(timer_id)
}
}