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
use futures::{
    prelude::*,
    task::{Context, Poll},
};
use std::{collections::VecDeque, pin::Pin, time::Duration};
use thiserror::Error;
use tokio::sync::mpsc::{self, error::TrySendError, Receiver, Sender};
use tokio_util::time::delay_queue::{DelayQueue, Key};

#[derive(Error, Debug)]
pub enum Error {
    #[error("failed to send a command: {0}")]
    CommandSend(&'static str),
    #[error("command queue closed")]
    CommandQueueClosed,
    #[error("timer error")]
    Timer(#[from] tokio::time::error::Error),
}

impl<T> From<TrySendError<T>> for Error {
    fn from(error: TrySendError<T>) -> Self {
        let cause = match error {
            TrySendError::Closed(_) => "channel closed",
            TrySendError::Full(_) => "no available capacity",
        };
        Error::CommandSend(cause)
    }
}

/// Schedule for fire-forget tasks
///
/// Each task has an ID (TID) and can be launched onto a worker identified with an ID (WID).
/// Launching is defined by a closure (Launcher), which also accepts additional data (Data).
/// Multiple instances of task with same TID, but different WID may be running in parallel.
/// Launcher must be quick and non-blocking.
///
/// When task is scheduled, it's queued. When it's finally launched, a timeout is started.
/// Until it runs out or task is declared complete it's considered running and it consumes parallel
/// task execution limits. Finishing with timeout doesn't make task completed and other queued
/// instances with same TID, but different WID may be run. Timed out instances are considered
/// failed and aren't rescheduled. Finishing by declaration task completion cancels all task
/// instances with the same TID.
///
/// Scheduling tasks and declaring them complete is possible with `FireForgetScheduler`.
///
/// The scheduler is a future that never resolves, it's used only to drive itself on executor.
/// It requires a valid Tokio context.
pub struct FireForgetSchedulerFuture<TID, WID, Data, Launcher>
where
    TID: Clone + PartialEq,
    WID: Clone + PartialEq,
    Launcher: Fn(TID, WID, Data),
{
    command_sender: FireForgetScheduler<TID, WID, Data>,
    command_receiver: Receiver<Command<TID, WID, Data>>,
    scheduled: VecDeque<ScheduledTask<TID, WID, Data>>,
    running: Vec<RunningTask<TID, WID>>,
    timeouts: DelayQueue<TimedOutTask<TID, WID>>,
    launcher: Launcher,
    max_running_same_task: usize,
    timeout: Duration,
}

pub struct FireForgetSchedulerConfig {
    /// How many tasks can be run in parallel
    pub max_running: usize,
    /// How many tasks with the same TID can be run in prallel
    pub max_running_same_task: usize,
    /// Size of command channel between `FireForgetScheduler` and `FireForgetSchedulerFuture`
    pub command_channel_size: usize,
    /// Launched task timeout after which it's considered failed if not declared complete
    pub timeout: Duration,
}

impl<TID, WID, Data, Launcher> FireForgetSchedulerFuture<TID, WID, Data, Launcher>
where
    TID: Clone + PartialEq,
    WID: Clone + PartialEq,
    Launcher: Fn(TID, WID, Data),
{
    /// Launcher controls how tasks will be started. It must be quick and non-blocking.
    pub fn new(config: &FireForgetSchedulerConfig, launcher: Launcher) -> Self {
        let (sender, command_receiver) = mpsc::channel(config.command_channel_size);
        let command_sender = FireForgetScheduler { sender };
        FireForgetSchedulerFuture {
            command_sender,
            command_receiver,
            scheduled: VecDeque::new(),
            running: Vec::with_capacity(config.max_running),
            timeouts: DelayQueue::with_capacity(config.max_running),
            launcher,
            max_running_same_task: config.max_running_same_task,
            timeout: config.timeout,
        }
    }

    pub fn scheduler(&self) -> FireForgetScheduler<TID, WID, Data> {
        self.command_sender.clone()
    }

    fn schedule(&mut self, task: ScheduledTask<TID, WID, Data>) {
        let scheduled_opt = self
            .scheduled
            .iter_mut()
            .find(|scheduled| scheduled.is_scheduled(&task));
        match scheduled_opt {
            Some(scheduled) => scheduled.update_data(task),
            None => {
                self.scheduled.push_back(task);
                self.try_run_scheduled();
            }
        }
    }

    fn declare_completed(&mut self, task: TID) {
        self.scheduled
            .retain(|scheduled| !scheduled.is_completed(&task));
        let timeouts = &mut self.timeouts;
        self.running.retain(|running| {
            if running.is_completed(&task) {
                timeouts.remove(&running.timeout_key);
                false
            } else {
                true
            }
        });
        self.try_run_scheduled();
    }

    fn declare_timed_out(&mut self, timed_out: TimedOutTask<TID, WID>) {
        self.running
            .retain(|running| !running.is_timed_out(&timed_out));
        self.try_run_scheduled();
    }

    fn try_run_scheduled(&mut self) {
        while self.running.len() < self.running.capacity() {
            let scheduled = match self.pop_next_runnable_task() {
                Some(scheduled) => scheduled,
                None => break,
            };
            let timeout_key = self.timeouts.insert(scheduled.to_timed_out(), self.timeout);
            self.running.push(scheduled.to_running(timeout_key));
            scheduled.launch(&self.launcher);
        }
    }

    fn pop_next_runnable_task(&mut self) -> Option<ScheduledTask<TID, WID, Data>> {
        self.scheduled
            .iter()
            .position(|scheduled| self.task_run_count(scheduled) < self.max_running_same_task)
            .and_then(|run_idx| self.scheduled.remove(run_idx))
    }

    fn task_run_count(&self, scheduled: &ScheduledTask<TID, WID, Data>) -> usize {
        self.running
            .iter()
            .filter(|running| running.is_running_same_task(scheduled))
            .count()
    }
}

impl<TID, WID, Data, Launcher> Future for FireForgetSchedulerFuture<TID, WID, Data, Launcher>
where
    TID: Clone + PartialEq + Unpin,
    WID: Clone + PartialEq + Unpin,
    Data: Unpin,
    Launcher: Fn(TID, WID, Data) + Unpin,
{
    type Output = Result<(), Error>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let inner = Pin::into_inner(self);
        while let Poll::Ready(command_opt) = Pin::new(&mut inner.command_receiver).poll_recv(cx) {
            match command_opt {
                None => return Poll::Ready(Err(Error::CommandQueueClosed)),
                Some(Command::Schedule { task }) => inner.schedule(task),
                Some(Command::DeclareCompleted { task }) => inner.declare_completed(task),
            }
        }
        while let Poll::Ready(Some(expired)) = Pin::new(&mut inner.timeouts).poll_expired(cx) {
            match expired {
                Ok(expired) => inner.declare_timed_out(expired.into_inner()),
                Err(err) => return Poll::Ready(Err(Error::Timer(err))),
            }
        }
        Poll::Pending
    }
}

pub struct FireForgetScheduler<TID, WID, Data> {
    sender: Sender<Command<TID, WID, Data>>,
}

impl<TID, WID, Data> Clone for FireForgetScheduler<TID, WID, Data> {
    fn clone(&self) -> Self {
        FireForgetScheduler {
            sender: self.sender.clone(),
        }
    }
}

impl<TID, WID, Data> FireForgetScheduler<TID, WID, Data> {
    /// Schedules a task to be launched.
    /// If task with same TID and WID is already queued, it has no effect.
    pub fn schedule(&mut self, tid: TID, wid: WID, data: Data) -> Result<(), Error> {
        self.try_send(Command::Schedule {
            task: ScheduledTask { tid, wid, data },
        })
    }

    /// Declares all tasks with given TID completed.
    /// Queued instances will be canceled and running ones will be considered finished.
    pub fn declare_completed(&mut self, task: TID) -> Result<(), Error> {
        self.try_send(Command::DeclareCompleted { task })
    }

    fn try_send(&mut self, command: Command<TID, WID, Data>) -> Result<(), Error> {
        self.sender.try_send(command).map_err(Into::into)
    }
}

enum Command<TID, WID, Data> {
    Schedule { task: ScheduledTask<TID, WID, Data> },
    DeclareCompleted { task: TID },
}

struct ScheduledTask<TID, WID, Data> {
    tid: TID,
    wid: WID,
    data: Data,
}

impl<TID, WID, Data> ScheduledTask<TID, WID, Data>
where
    TID: Clone + PartialEq,
    WID: Clone + PartialEq,
{
    fn to_running(&self, timeout_key: Key) -> RunningTask<TID, WID> {
        RunningTask {
            tid: self.tid.clone(),
            wid: self.wid.clone(),
            timeout_key,
        }
    }

    fn to_timed_out(&self) -> TimedOutTask<TID, WID> {
        TimedOutTask {
            tid: self.tid.clone(),
            wid: self.wid.clone(),
        }
    }

    fn is_completed(&self, task: &TID) -> bool {
        self.tid == *task
    }

    fn is_scheduled(&self, scheduled: &Self) -> bool {
        self.tid == scheduled.tid && self.wid == scheduled.wid
    }

    fn update_data(&mut self, other: Self) {
        self.data = other.data
    }

    fn launch(self, launcher: impl Fn(TID, WID, Data)) {
        launcher(self.tid, self.wid, self.data);
    }
}

struct RunningTask<TID, WID> {
    tid: TID,
    wid: WID,
    timeout_key: Key,
}

impl<TID, WID> RunningTask<TID, WID>
where
    TID: PartialEq,
    WID: PartialEq,
{
    fn is_timed_out(&self, timed_out: &TimedOutTask<TID, WID>) -> bool {
        self.tid == timed_out.tid && self.wid == timed_out.wid
    }

    fn is_completed(&self, task: &TID) -> bool {
        self.tid == *task
    }

    fn is_running_same_task<Data>(&self, scheduled: &ScheduledTask<TID, WID, Data>) -> bool {
        self.tid == scheduled.tid
    }
}

struct TimedOutTask<TID, WID> {
    tid: TID,
    wid: WID,
}