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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! A simple schedule task runner on tokio runtime that support one way message pushing.
//!
//! # example:
//! ```rust
//! use std::time::Duration;
//! use heng_rs::{Scheduler, Time, ToDuration};
//!
//! struct TestTask(u32);
//!
//! impl Scheduler for TestTask {
//!     type Message = u32;
//! }
//!
//! #[tokio::main]
//! async fn main() -> std::io::Result<()> {
//!     let task = TestTask(0);
//!     let time = Time::new()
//!         .every(1.d())
//!         .plus(2.h())
//!         .plus(3.m())
//!         .plus(4.s());
//!     // run task with a 1 day, 2 hours, 3 minutes and 4 seconds interval.
//!     let addr = task.start(time, |task, ctx| {
//!         if let Some(msg) = ctx.get_msg_front() {
//!             /* do something with message */
//!         }
//!
//!         // do something with task.
//!         task.0 += 1;
//!
//!         // run a future.
//!         async {
//!             Ok::<(),()>(())
//!         }
//!     });
//!
//!     // use address to push message to task's context;
//!     addr.send(1u32).await;
//!     Ok(())
//! }
//! ```

use std::any::TypeId;
use std::collections::VecDeque;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::{Duration, Instant};

use futures_channel::mpsc::{channel, unbounded, Receiver, Sender, UnboundedSender};
use futures_util::{SinkExt, StreamExt};

// re export futures channel's SendError
pub use futures_channel::mpsc::SendError;
use tokio_timer::{Interval, Timeout};

pub use time::{Time, ToDuration};

mod time;

pub struct SharedSchedulerSender<M>(Arc<Mutex<SchedulerSender<M>>>);

pub struct SchedulerSender<M> {
    tx: Option<UnboundedSender<M>>,
    tx_sig: Sender<Signal>,
}

impl<M: Send + 'static> SharedSchedulerSender<M> {
    pub async fn send(&self, msg: M) -> Result<(), SendError> {
        self.0
            .lock()
            .unwrap()
            .tx
            .as_mut()
            .expect("SchedulerSender is None")
            .send(msg)
            .await
    }

    /// send message and ignore the result.
    pub fn do_send(&self, msg: M) {
        let sender = self.0.clone();
        tokio_executor::current_thread::spawn(async move {
            let _ = sender.lock().unwrap().tx.as_mut().unwrap().send(msg).await;
        });
    }

    pub fn start(&self) -> impl Future<Output = Result<(), SendError>> + '_ {
        self.send_signal(Signal::Start)
    }

    pub fn stop(&self) -> impl Future<Output = Result<(), SendError>> + '_ {
        self.send_signal(Signal::Stop)
    }

    pub fn change_time(
        &self,
        time: impl Into<Duration>,
    ) -> impl Future<Output = Result<(), SendError>> + '_ {
        self.send_signal(Signal::ChangeDur(time.into()))
    }

    async fn send_signal(&self, signal: Signal) -> Result<(), SendError> {
        let mut inner = self.0.lock().unwrap();

        inner.tx_sig.send(signal).await
    }
}

pub trait Scheduler: Sized + Send + 'static {
    type Message: Send;

    fn start<F, Fut, R>(
        self,
        time: impl Into<Duration>,
        f: F,
    ) -> SharedSchedulerSender<Self::Message>
    where
        F: FnMut(&mut Self, &mut Context<Self>) -> Fut + Send + 'static,
        Fut: Future<Output = R> + Send + 'static,
    {
        // setup context.
        let (mut ctx, tx_sig) = Context::new();

        // setup message channel.
        let tx = spawn_message_channel::<Self>(&ctx.msg);

        // set duration.
        ctx.signal(Signal::ChangeDur(time.into()));

        // run the interval.
        self.run(f, ctx);

        // return shared channel sender.
        SharedSchedulerSender(Arc::new(Mutex::new(SchedulerSender { tx, tx_sig })))
    }

    fn run<F, Fut, R>(mut self, mut f: F, mut ctx: Context<Self>)
    where
        F: FnMut(&mut Self, &mut Context<Self>) -> Fut + Send + 'static,
        Fut: Future<Output = R> + Send + 'static,
    {
        tokio_executor::spawn(async move {
            let dur = ctx.dur;
            let mut interval = Interval::new(Instant::now(), dur);
            while let Some(_instant) = interval.next().await {
                // if we are not running we just ignore F.
                if ctx.running {
                    f(&mut self, &mut ctx).await;
                }

                if ctx.should_restart(dur).await {
                    drop(interval);
                    return self.run(f, ctx);
                }
            }
        });
    }

    fn handler<'a>(
        &'a mut self,
        _ctx: &'a mut Context<Self>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
        Box::pin(async {})
    }

    /// You can access `&mut Self` and `&mut Context<Self>` in a future if you manually impl `handler` method for your Type
    /// and start the task with `start_with_handler`
    ///```rust
    /// use std::pin::Pin;
    /// use std::future::Future;
    /// use std::time::{Duration, Instant};
    ///
    /// use heng_rs::{Scheduler, Context};
    ///
    /// struct TestTask {
    ///     field: u32
    /// }
    ///
    /// impl Scheduler for TestTask {
    ///     type Message = u32;
    ///
    ///     fn handler<'a>(&'a mut self, ctx: &'a mut Context<Self>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> {
    ///         Box::pin(async move {
    ///             assert_eq!(self.field, 0u32);
    ///             // we can modify the context and self in the async block.
    ///             if let Some(msg) = ctx.get_msg_front() {
    ///                 self.field = msg;
    ///             };
    ///             assert_eq!(self.field, 3u32);
    ///         })
    ///     }
    /// }
    /// #[tokio::main]
    /// async fn main() -> std::io::Result<()> {
    ///     let task = TestTask { field: 0 };
    ///     let addr = task.start_with_handler(Duration::from_secs(1));
    ///
    ///     // use address to send message to task;
    ///     addr.send(3u32).await;
    ///     tokio::timer::delay(Instant::now() + Duration::from_secs(2)).await;
    ///     Ok(())
    /// }
    ///```
    fn start_with_handler(self, time: impl Into<Duration>) -> SharedSchedulerSender<Self::Message> {
        let (mut ctx, tx_sig) = Context::new();

        let tx = spawn_message_channel::<Self>(&ctx.msg);

        ctx.signal(Signal::ChangeDur(time.into()));

        self.run_with_handler(ctx);

        SharedSchedulerSender(Arc::new(Mutex::new(SchedulerSender { tx, tx_sig })))
    }

    fn run_with_handler(mut self, mut ctx: Context<Self>) {
        tokio_executor::spawn(async move {
            let dur = ctx.dur;
            let mut interval = Interval::new(Instant::now(), dur);
            while let Some(_instant) = interval.next().await {
                // if we are not running we just ignore F.
                if ctx.running {
                    self.handler(&mut ctx).await;
                }

                // we listen to signal for a period of self duration after handle
                if ctx.should_restart(dur).await {
                    drop(interval);
                    return self.run_with_handler(ctx);
                }
            }
        });
    }
}

fn spawn_message_channel<S: Scheduler>(
    msg: &Arc<Mutex<VecDeque<S::Message>>>,
) -> Option<UnboundedSender<S::Message>> {
    // If we have a message type other than ().
    // Then we pass message queue and message channel to spawn future and push new message to it.
    if TypeId::of::<S::Message>() == TypeId::of::<()>() {
        None
    } else {
        // setup message channel.
        let (tx, mut rx) = unbounded::<S::Message>();

        // spawn a future to inject message to context.msg with channel sender.
        let msg = Arc::downgrade(&msg);
        tokio_executor::spawn(async move {
            while let Some(m) = rx.next().await {
                if let Some(msg) = msg.upgrade() {
                    msg.lock()
                        .expect("Failed to acquire Message Mutex lock")
                        .push_back(m);
                } else {
                    panic!("Fail to upgrade Arc<Mutex<VecDequeue<Scheduler::Message>. It's likely the Scheduler has already been dropped");
                }
            }
        });

        Some(tx)
    }
}

enum Signal {
    Stop,
    Start,
    ChangeDur(Duration),
}

pub struct Context<S: Scheduler> {
    msg: Arc<Mutex<VecDeque<S::Message>>>,
    dur: Duration,
    running: bool,
    rx_sig: Receiver<Signal>,
}

impl<S: Scheduler> Context<S> {
    fn new() -> (Self, Sender<Signal>) {
        let (tx, rx_sig) = channel::<Signal>(1);
        let ctx = Context {
            msg: Arc::new(Mutex::new(VecDeque::new())),
            dur: Duration::default(),
            running: true,
            rx_sig,
        };

        (ctx, tx)
    }

    fn signal(&mut self, signal: Signal) -> &mut Self {
        match signal {
            Signal::Stop => self.running = false,
            Signal::Start => self.running = true,
            Signal::ChangeDur(dur) => self.dur = dur,
        };
        self
    }

    fn lock(&self) -> MutexGuard<'_, VecDeque<S::Message>> {
        self.msg
            .lock()
            .expect("Failed to acquire Message Mutex lock")
    }

    pub fn get_msg_front(&self) -> Option<S::Message> {
        self.lock().pop_front()
    }

    pub fn get_msg_back(&self) -> Option<S::Message> {
        self.lock().pop_back()
    }

    async fn should_restart(&mut self, dur: Duration) -> bool {
        // we listen to signal for a period of self duration after handle
        if let Ok(signal) = Timeout::new(self.rx_sig.next(), dur).await {
            if let Some(signal) = signal {
                // if we have a changed duration we drop the interval and start a new run.
                if self.signal(signal).dur != dur {
                    return true;
                }
            }
        }
        false
    }
}

#[cfg(test)]
mod test_lib {
    use std::sync::Arc;
    use std::time::{Duration, Instant};

    use futures_util::{lock::Mutex, SinkExt, StreamExt};

    use crate::time::{Time, ToDuration};
    use crate::Scheduler;

    struct TestSchedule;

    impl Scheduler for TestSchedule {
        type Message = u32;
    }

    #[tokio::test]
    async fn stop_send_restart() -> std::io::Result<()> {
        let test = TestSchedule;

        let (tx, mut rx) = futures_channel::mpsc::channel::<u32>(1);
        let tx = Arc::new(Mutex::new(tx));

        let time = Time::new().every(400.millis()).plus(100.millis());

        let addr = test.start(time, move |_task, ctx| {
            let sender = tx.clone();
            let msg = ctx.get_msg_front();
            async move {
                if let Some(msg) = msg {
                    let _ = sender.lock().await.send(msg).await;
                }
                Ok::<(), ()>(())
            }
        });

        let _ = addr.stop().await;
        let _ = addr.send(32u32).await;

        let now = Instant::now();

        tokio::timer::delay(Instant::now() + Duration::from_secs(2)).await;

        let _ = addr.start().await;

        assert_eq!(rx.next().await.unwrap(), 32u32);
        assert!(Instant::now().duration_since(now) > Duration::from_secs(2));

        Ok(())
    }
}