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
#[cfg(feature = "postgres")]
pub mod postgres;

#[cfg(feature = "redis")]
pub mod redis;

#[cfg(all(feature = "postgres", feature = "redis"))]
pub mod postgres_redis;

pub mod retry;

use chrono::prelude::*;
pub use cron::Schedule;
use std::{future::Future, time::Duration};
use tokio::{signal::ctrl_c, spawn, task::JoinHandle, time::sleep};
pub use tokio_util::sync::CancellationToken;
use tracing::debug;

///
/// LoopState
///   AllTerminate: stop all threads
///   Continue: continue loop
///   Terminate: terminate this loop
///   Duration(duration): sleep duration
///
pub enum LoopState {
    AllTerminate,
    Continue,
    Terminate,
    Duration(Duration),
}

impl LoopState {
    pub(crate) fn looper(
        &self,
        token: &CancellationToken,
        now: &DateTime<Utc>,
        schedule: &Schedule,
    ) -> Option<DateTime<Utc>> {
        match self {
            LoopState::AllTerminate => {
                token.cancel();
                None
            }
            LoopState::Terminate => None,
            LoopState::Duration(duration) => {
                // 指定時間待つ
                Some(*now + *duration)
            }
            LoopState::Continue => {
                // 次の時間取得
                schedule.upcoming(Utc).next()
            }
        }
    }
    pub(crate) fn worker(
        &self,
        token: &CancellationToken,
        now: &DateTime<Utc>,
    ) -> Option<DateTime<Utc>> {
        match self {
            LoopState::AllTerminate => {
                token.cancel();
                None
            }
            LoopState::Terminate => None,
            LoopState::Duration(duration) => {
                // 指定時間待つ
                Some(*now + *duration)
            }
            LoopState::Continue => {
                // 即時処理を行う
                Some(*now)
            }
        }
    }
}

// 次の処理までスリープする
#[allow(dead_code)]
pub(crate) async fn execute_sleep(
    stop_check_duration: &Duration,
    next_tick: &DateTime<Utc>,
    now: &DateTime<Utc>,
) {
    // next_tickが過去ならsleepせずに終了
    if now >= next_tick {
        return;
    }

    // 上記でチェックしているので、as u64で問題無い。
    let tick_duration = Duration::from_secs((*next_tick - *now).num_seconds() as u64);
    let duration = if stop_check_duration < &tick_duration {
        stop_check_duration
    } else {
        &tick_duration
    };
    sleep(*duration).await;
}

pub fn ctrl_c_handler() -> (JoinHandle<()>, CancellationToken) {
    let token = CancellationToken::new();
    let cloned_token = token.clone();
    (
        spawn(async move {
            if let Err(err) = ctrl_c().await {
                debug!(error = ?err, "ctrl-c error");
            } else {
                debug!("received ctrl-c");
            }
            cloned_token.cancel();
        }),
        token,
    )
}

pub fn make_looper<Fut1, Fut2>(
    token: CancellationToken,
    schedule: Schedule,
    stop_check_duration: Duration,
    task_function: impl Fn(DateTime<Utc>) -> Fut1 + Send + Sync + 'static,
    stop_function: impl Fn() -> Fut2 + Send + Sync + 'static,
) -> JoinHandle<()>
where
    Fut1: Future<Output = LoopState> + Send,
    Fut2: Future<Output = ()> + Send,
{
    spawn(async move {
        let mut next_tick: DateTime<Utc> = match schedule.upcoming(Utc).next() {
            Some(next_tick) => next_tick,
            None => {
                stop_function().await;
                return;
            }
        };
        loop {
            // グレースフルストップのチェック
            if token.is_cancelled() {
                stop_function().await;
                break;
            }

            let now = Utc::now();
            if now >= next_tick {
                // 定期的に行う処理実行
                if let Some(res) = task_function(now).await.looper(&token, &now, &schedule) {
                    next_tick = res;
                } else {
                    stop_function().await;
                    break;
                }
            }

            execute_sleep(&stop_check_duration, &next_tick, &now).await;
        }
    })
}

pub fn make_worker<Fut1, Fut2>(
    token: CancellationToken,
    stop_check_duration: Duration,
    task_function: impl Fn(DateTime<Utc>) -> Fut1 + Send + Sync + 'static,
    stop_function: impl Fn() -> Fut2 + Send + Sync + 'static,
) -> JoinHandle<()>
where
    Fut1: Future<Output = LoopState> + Send,
    Fut2: Future<Output = ()> + Send,
{
    spawn(async move {
        // 動き出した瞬間は実行する
        let mut next_tick: DateTime<Utc> = Utc::now();
        loop {
            // グレースフルストップのチェック
            if token.is_cancelled() {
                stop_function().await;
                break;
            }

            // 現在時間と次実行する処理の時間をチェックする
            let now = Utc::now();
            if now >= next_tick {
                // 定期的に行う処理実行
                if let Some(res) = task_function(now).await.worker(&token, &now) {
                    next_tick = res;
                } else {
                    stop_function().await;
                    break;
                }
            }

            execute_sleep(&stop_check_duration, &next_tick, &now).await;
        }
    })
}