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
use std::{sync::Arc, time::Duration};
use triggered::{trigger, Listener, Trigger};

use super::service::{AsyncService, AsyncServiceFuture};

const TICK: &str = "tick";

pub enum TickReason {
    /// Sleep went to completion, time to wake-up
    Wakeup,

    /// Service was shutdown
    Shutdown,
}

pub struct TickService {
    shutdown_trigger: Trigger,
    shutdown_listener: Listener,
}

impl TickService {
    pub fn new() -> Self {
        let (shutdown, monitor) = trigger();
        Self { shutdown_trigger: shutdown, shutdown_listener: monitor }
    }

    /// Waits until `duration` has elapsed when the service is started.
    ///
    /// Returns immediately when the service is stopped.
    pub async fn tick(&self, duration: Duration) -> TickReason {
        match tokio::time::timeout(duration, self.shutdown_listener.clone()).await {
            Ok(()) => TickReason::Shutdown,
            Err(_) => TickReason::Wakeup,
        }
    }

    pub fn shutdown(&self) {
        self.shutdown_trigger.trigger();
    }
}

impl Default for TickService {
    fn default() -> Self {
        Self::new()
    }
}

// service trait implementation for TickService
impl AsyncService for TickService {
    fn ident(self: Arc<Self>) -> &'static str {
        TICK
    }

    fn start(self: Arc<Self>) -> AsyncServiceFuture {
        Box::pin(async move { Ok(()) })
    }

    fn signal_exit(self: Arc<Self>) {
        self.shutdown();
    }

    fn stop(self: Arc<Self>) -> AsyncServiceFuture {
        Box::pin(async move { Ok(()) })
    }
}