use super::scheduled_task::ScheduledTask;
use crate::error::BeatError;
use std::{collections::BinaryHeap, future::Future, pin::Pin, time::Duration};
mod redis;
pub use redis::{RedisBackendConfig, RedisSchedulerBackend};
pub trait SchedulerBackend {
fn should_sync(&self) -> bool;
fn sync(&mut self, scheduled_tasks: &mut BinaryHeap<ScheduledTask>) -> Result<(), BeatError>;
fn as_distributed(&mut self) -> Option<&mut dyn DistributedScheduler> {
None
}
}
pub struct TickDecision {
pub execute_tasks: bool,
pub sleep_hint: Option<Duration>,
}
impl TickDecision {
pub fn execute() -> Self {
TickDecision {
execute_tasks: true,
sleep_hint: None,
}
}
pub fn execute_with_hint(sleep_hint: Duration) -> Self {
TickDecision {
execute_tasks: true,
sleep_hint: Some(sleep_hint),
}
}
pub fn skip(sleep_hint: Duration) -> Self {
TickDecision {
execute_tasks: false,
sleep_hint: Some(sleep_hint),
}
}
}
pub trait DistributedScheduler {
fn before_tick<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = Result<TickDecision, BeatError>> + 'a>>;
fn after_tick<'a>(
&'a mut self,
scheduled_tasks: &'a mut BinaryHeap<ScheduledTask>,
) -> Pin<Box<dyn Future<Output = Result<(), BeatError>> + 'a>>;
fn shutdown<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = Result<(), BeatError>> + 'a>> {
Box::pin(async { Ok(()) })
}
}
pub struct LocalSchedulerBackend {}
#[allow(clippy::new_without_default)]
impl LocalSchedulerBackend {
pub fn new() -> Self {
Self {}
}
}
impl SchedulerBackend for LocalSchedulerBackend {
fn should_sync(&self) -> bool {
false
}
#[allow(unused_variables)]
fn sync(&mut self, scheduled_tasks: &mut BinaryHeap<ScheduledTask>) -> Result<(), BeatError> {
unimplemented!()
}
}