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
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;

pub type StopFn = Box<dyn Stop>;

#[async_trait]
pub trait Stop {
    async fn stop(self);
}

#[async_trait]
pub trait Lifecycle {
    async fn start(self: Arc<Self>) -> Option<StopFn>;
}

#[async_trait]
impl<F, R> Stop for F
where
    F: FnOnce() -> R + Send,
    R: Future<Output = ()> + Send,
{
    async fn stop(self) {
        self().await
    }
}

pub fn interval_worker<S, F, R>(s: Arc<S>, period: Duration, fun: F) -> StopFn
where
    S: Send + Sync + 'static,
    F: Fn(Arc<S>) -> R + Send + Sync + 'static,
    R: Future<Output = ()> + Send + Sync,
{
    let (tx, mut rx) = tokio::sync::oneshot::channel();
    let jh = tokio::spawn(async move {
        let sleep = tokio::time::sleep(period);
        tokio::pin!(sleep);
        loop {
            tokio::select! {
                _ = &mut sleep => {
                    fun(s.clone()).await;
                    sleep.as_mut().reset(tokio::time::Instant::now() + period);
                },
                _ = &mut rx => {
                    return;
                }
            }
        }
    });
    Box::new(|| {
        Box::pin(async move {
            let _ = tx.send(());
            jh.await.unwrap();
        })
    })
}