1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Scheduler for sync and async cron jobs.

use std::{future::Future, time::Duration};

mod async_job;
mod job;

pub use async_job::{AsyncCronJob, AsyncJob, AsyncJobScheduler};
pub use job::{CronJob, Job, JobScheduler};

/// An interface for scheduling sync jobs.
pub trait Scheduler {
    /// Returns `true` if the scheduler is ready to run.
    fn is_ready(&self) -> bool;

    /// Returns the duration till the next job is supposed to run.
    fn time_till_next_job(&self) -> Duration;

    /// Increments time for the scheduler and executes any pending jobs.
    fn tick(&mut self);
}

/// An interface for scheduling async jobs.
pub trait AsyncScheduler {
    /// Returns `true` if the scheduler is ready to run.
    fn is_ready(&self) -> bool;

    /// Returns the duration till the next job is supposed to run.
    fn time_till_next_job(&self) -> Duration;

    /// Increments time for the scheduler and executes any pending jobs asynchronously.
    fn tick(&mut self) -> impl Future<Output = ()> + Send;
}