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
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::{fmt, future::Future, pin::Pin};

use chrono::{DateTime, Local, TimeZone};

use crate::{
    job::Job,
    job_schedule::{JobSchedule, WithSchedule},
    timeprovider::{ChronoTimeProvider, TimeProvider},
    Interval,
};

pub type JobFuture = Box<dyn Future<Output = ()> + Send + 'static>;
/// An asynchronous job to run on the scheduler.
/// Create these by calling [`AsyncScheduler::every()`](crate::AsyncScheduler::every).
///
/// Methods for scheduling the job live in the [Job] trait.
pub struct AsyncJob<Tz = Local, Tp = ChronoTimeProvider>
where
    Tz: TimeZone,
    Tp: TimeProvider,
{
    schedule: JobSchedule<Tz, Tp>,
    job: Option<Box<dyn GiveMeAPinnedFuture + Send>>,
}

trait GiveMeAPinnedFuture {
    fn get_pinned(&mut self) -> Pin<JobFuture>;
}

struct JobWrapper<F, T>
where
    F: FnMut() -> T,
    T: Future,
{
    f: F,
}

impl<F, T> JobWrapper<F, T>
where
    F: FnMut() -> T,
    T: Future,
{
    fn new(f: F) -> Self {
        JobWrapper { f }
    }
}

impl<F, T> GiveMeAPinnedFuture for JobWrapper<F, T>
where
    F: FnMut() -> T,
    T: Future<Output = ()> + Send + 'static,
{
    fn get_pinned(&mut self) -> Pin<JobFuture> {
        Box::pin((self.f)())
    }
}

impl<Tz, Tp> WithSchedule<Tz, Tp> for AsyncJob<Tz, Tp>
where
    Tz: TimeZone,
    Tp: TimeProvider,
{
    fn schedule_mut(&mut self) -> &mut JobSchedule<Tz, Tp> {
        &mut self.schedule
    }

    fn schedule(&self) -> &JobSchedule<Tz, Tp> {
        &self.schedule
    }
}

impl<Tz, Tp> fmt::Debug for AsyncJob<Tz, Tp>
where
    Tz: TimeZone,
    Tp: TimeProvider,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.schedule.fmt(f)
    }
}

impl<Tz, Tp> Job<Tz, Tp> for AsyncJob<Tz, Tp>
where
    Tz: TimeZone + Sync + Send,
    Tp: TimeProvider,
{
}

impl<Tz, Tp> AsyncJob<Tz, Tp>
where
    Tz: chrono::TimeZone + Sync + Send,
    Tp: TimeProvider,
{
    pub(crate) fn new(ival: Interval, tz: Tz) -> Self {
        AsyncJob {
            schedule: JobSchedule::new(ival, tz),
            job: None,
        }
    }

    /// Specify a task to run, and schedule its next run
    ///
    /// The function passed into this method should return a value implementing `Future<Output = ()>`.
    pub fn run<F, T>(&mut self, f: F) -> &mut Self
    where
        F: 'static + FnMut() -> T + Send,
        T: 'static + Future<Output = ()> + Send,
    {
        self.job = Some(Box::new(JobWrapper::new(f)));
        self.schedule.start_schedule();
        self
    }

    /// Run a task and re-schedule it. This is usually only called by
    /// [AsyncScheduler::run_pending()](crate::AsyncScheduler::run_pending).
    pub fn execute(&mut self, now: &DateTime<Tz>) -> Option<Pin<JobFuture>> {
        // Don't do anything if we're run out of runs
        if !self.schedule.can_run_again() {
            return None;
        }
        let rv = self.job.as_mut().map(|f| f.get_pinned());
        self.schedule.schedule_next(now);
        rv
    }
}