sacs 0.9.3

Simple Async Cron Scheduler for Tokio
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use crate::{
    event::{Event, EventId},
    ControlChannel, Error, Result,
};
use std::fmt::Debug;
use std::{
    collections::{BTreeMap, BTreeSet, HashMap},
    time::SystemTime,
};
use tokio::{
    select,
    sync::{RwLock, RwLockWriteGuard},
    time::{sleep, Duration},
};
use tracing::{debug, instrument};

/// Default size of Queue control channel
const QUEUE_CONTROL_CHANNEL_SIZE: usize = 1024;
/// Time to sleep control loop if queue is empty, actually value doesn't matter
const EMPTY_QUEUE_SLEEP_DURATION_SECONDS: u64 = 3600;

/// Events queue behavior
pub(crate) trait EventTimeQueue: Debug {
    /// Blocks execution until next event time.
    /// Returns Result with event and remove it from queue.
    async fn next(&self) -> Result<Event>;
    /// Blocks execution until next event time or timeout exceeded.
    /// Returns Option with Result with event and remove it from queue, or None if timeout.
    #[allow(dead_code)]
    async fn try_next(&self, timeout: Duration) -> Option<Result<Event>> {
        select! {
            biased;
            next_job = self.next() => Some(next_job),
            _ = sleep(timeout) => None,
        }
    }

    /// Insert event into queue at event's time position.
    async fn insert(&self, event: Event) -> Result<()>;
    /// Insert event into queue at current time.
    #[allow(dead_code)]
    async fn push(&self, id: EventId) -> Result<()> {
        self.insert(Event::with_id(id)).await
    }

    /// Remove event from the queue at it's time position.
    #[allow(dead_code)]
    async fn remove(&self, event: &Event) -> Result<()>;
    /// Removes all event's instances at all times.
    async fn pop(&self, id: &EventId) -> Result<Vec<SystemTime>>;

    /// Breaks control loop (next/try_next)
    async fn shutdown(&self);
}

/// Simple (but enough) implementation of EventTimeQueue
#[derive(Debug)]
pub(crate) struct Queue {
    // Indexes of events by id and time
    index: RwLock<QueueIndex>,
    // Channel to send update queue events and shutdown
    control_channel: ControlChannel<ChangeStateEvent>,
}

#[derive(Debug)]
struct QueueIndex {
    by_time: BTreeMap<SystemTime, BTreeSet<EventId>>,
    by_id: HashMap<EventId, BTreeSet<SystemTime>>,
}

impl QueueIndex {
    fn new() -> Self {
        Self {
            by_time: BTreeMap::<SystemTime, BTreeSet<EventId>>::new(),
            by_id: HashMap::<EventId, BTreeSet<SystemTime>>::new(),
        }
    }

    fn clear(&mut self) {
        debug!(
            time_index_size = self.by_time.len(),
            id_index_size = self.by_id.len(),
            "clear queue indexes"
        );
        self.by_time.clear();
        self.by_id.clear();
    }
}

/// Queue state change events
#[derive(Debug)]
enum ChangeStateEvent {
    // Send it each time when insert or remove event
    QueueUpdated,
    // Stop control loop and return error
    Shutdown,
}

impl Queue {
    /// Helper method to remove event from the queue.
    /// It helps to avoid deadlocks during multiply remove operations.
    async fn remove_event_from_index(
        event: &Event,
        index: &mut RwLockWriteGuard<'_, QueueIndex>,
    ) -> Result<()> {
        let ids = index.by_time.get(&event.time());
        let times = index.by_id.get(&event.id());

        // Event is present in both indexes - it's OK, remove it
        if ids.is_some() && times.is_some() {
            debug!(?event, "get next event");
            // Remove from time index
            let ids = index.by_time.get_mut(&event.time()).unwrap();
            ids.remove(&event.id());
            if ids.is_empty() {
                index.by_time.remove(&event.time());
            }

            // Remove from id index
            let event_times = index.by_id.get_mut(&event.id()).unwrap();
            event_times.remove(&event.time());

            if event_times.is_empty() {
                index.by_id.remove(&event.id());
            }

            Ok(())
        } else if (ids.is_none() && times.is_some()) || (ids.is_some() && times.is_none()) {
            // There is some inconsistency in indexes - looks like a BUG
            debug!(?event, "inconsistent indexes discovered");
            Err(Error::InconsistentQueueContent)
        } else {
            // Event is absent in both indexes:
            // it's strange (we try to delete non-existent event), but this is not an error of the queue,
            // it's rather an error at the calling side.
            debug!(?event, "event not found");
            Ok(())
        }
    }
}

impl Default for Queue {
    fn default() -> Self {
        Self {
            index: RwLock::new(QueueIndex::new()),
            control_channel: ControlChannel::new(QUEUE_CONTROL_CHANNEL_SIZE),
        }
    }
}

impl EventTimeQueue for Queue {
    #[instrument("waiting for queue event", skip_all, level = "debug")]
    async fn next(&self) -> Result<Event> {
        loop {
            // Calculate sleep time for next loop iteration
            let sleep_for = {
                let mut index = self.index.write().await;
                // Get first time in queue and check if it's in the past, return event if so instead of calculating time to sleep
                if let Some((top_time, top_ids)) = index.by_time.first_key_value() {
                    let now = SystemTime::now();
                    if now >= *top_time {
                        // Construct event from the top of the queue id/time
                        // Get first id from the list if there are many ids
                        let event =
                            Event::new(top_ids.first().unwrap().clone(), top_time.to_owned());
                        // Remove it from queue
                        Queue::remove_event_from_index(&event, &mut index).await?;
                        // And break loop returning event
                        debug!(?event, "event is ready");
                        return Ok(event);
                    } else {
                        // Or calculate sleep time from now to the next event
                        top_time.duration_since(now)?
                    }
                } else {
                    // Queue is empty - sleep for a while
                    Duration::from_secs(EMPTY_QUEUE_SLEEP_DURATION_SECONDS)
                }
            };

            // Sleep but listen to the control channel
            debug!(duration = ?sleep_for, "sleep until next event");
            select! {
                biased;
                control_event = self.control_channel.receive() => {
                    debug!(event = ?control_event, "control event received");
                    if let Some(control_event) = control_event {
                        match control_event {
                            ChangeStateEvent::Shutdown => return Err(Error::ShutdownRequested),
                            // Run loop from the beginning
                            ChangeStateEvent::QueueUpdated => {},
                        }
                    }
                },
                // Run loop from the beginning
                _ = sleep(sleep_for) => {},
            }
        }
    }

    async fn insert(&self, event: Event) -> Result<()> {
        debug!(?event, "insert new event");
        {
            let mut index = self.index.write().await;

            // Insert into index by time
            if let Some(ids) = index.by_time.get_mut(&event.time()) {
                ids.insert(event.id());
            } else {
                let ids = BTreeSet::from([event.id()]);
                index.by_time.insert(event.time(), ids);
            }

            // Insert into index by job id
            if let Some(event_time) = index.by_id.get_mut(&event.id()) {
                event_time.insert(event.time());
            } else {
                let event_time = BTreeSet::from([event.time()]);
                index.by_id.insert(event.id(), event_time);
            }
        }

        self.control_channel
            .send_event(ChangeStateEvent::QueueUpdated)
            .await
    }

    async fn remove(&self, event: &Event) -> Result<()> {
        debug!(?event, "remove event");
        {
            let mut index = self.index.write().await;
            Queue::remove_event_from_index(event, &mut index).await?;
        }

        self.control_channel
            .send_event(ChangeStateEvent::QueueUpdated)
            .await
    }

    async fn pop(&self, id: &EventId) -> Result<Vec<SystemTime>> {
        debug!(event_id = ?id, "purge event instances");
        let mut return_times: Vec<SystemTime> = Vec::new();
        {
            let mut index = self.index.write().await;
            let event_times = index.by_id.get_mut(id);
            if let Some(event_times) = event_times {
                debug!(events_to_purge = event_times.len());
                // Loop over event's times and remove each one
                // But preserve list of times to return it
                return_times = event_times.iter().copied().collect();
                for event_time in &return_times {
                    Queue::remove_event_from_index(
                        &Event::new(id.clone(), *event_time),
                        &mut index,
                    )
                    .await?;
                }
            }
        }

        self.control_channel
            .send_event(ChangeStateEvent::QueueUpdated)
            .await?;
        Ok(return_times)
    }

    async fn shutdown(&self) {
        debug!("shutdown requested");
        self.index.write().await.clear();

        let _result = self
            .control_channel
            .send_event(ChangeStateEvent::Shutdown)
            .await;
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use ntest::timeout;
    use std::collections::HashSet;
    use uuid::Uuid;

    #[tokio::test]
    #[timeout(5000)]
    async fn basic() {
        let queue = Queue::default();
        let id = EventId::default();
        let time = SystemTime::now();
        let event_now = Event::new(id.clone(), time);
        let event_1s = Event::new(
            id.clone(),
            time.checked_add(Duration::from_secs(1)).unwrap(),
        );
        let event_2s = Event::new(
            id.clone(),
            time.checked_add(Duration::from_secs(2)).unwrap(),
        );

        queue.insert(event_now.clone()).await.unwrap();
        queue.insert(event_1s.clone()).await.unwrap();
        queue.insert(event_2s.clone()).await.unwrap();

        let next = queue.next().await.unwrap();
        assert_eq!(next, event_now);

        let next = queue.try_next(Duration::from_millis(100)).await;
        assert!(next.is_none());

        let next = queue.next().await.unwrap();
        assert_eq!(next, event_1s);
        assert!(SystemTime::now() > next.time());

        let next = queue.try_next(Duration::from_millis(100)).await;
        assert!(next.is_none());

        let next = queue
            .try_next(Duration::from_millis(1000))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(next, event_2s);
        assert!(SystemTime::now() > next.time());
    }

    #[tokio::test]
    #[timeout(5000)]
    async fn insert_remove() {
        let queue = Queue::default();
        let id_1 = EventId::default();
        let id_2 = EventId::default();
        let time = SystemTime::now();

        let event_1_0s = Event::new(id_1.clone(), time);
        let event_2_0s = Event::new(id_2.clone(), time);
        let event_1s = Event::new(
            id_1.clone(),
            time.checked_add(Duration::from_secs(1)).unwrap(),
        );
        let event_2s = Event::new(
            id_1.clone(),
            time.checked_add(Duration::from_secs(2)).unwrap(),
        );

        // Test insert of two events at the same time
        queue.insert(event_1_0s.clone()).await.unwrap();
        queue.insert(event_2_0s.clone()).await.unwrap();
        let next_1 = queue.next().await.unwrap();
        let next_2 = queue.next().await.unwrap();
        let mut set_got = HashSet::new();
        let mut set_exp = HashSet::new();
        set_got.insert(next_1);
        set_got.insert(next_2);
        set_exp.insert(event_1_0s);
        set_exp.insert(event_2_0s);
        assert_eq!(set_exp, set_got);

        // Remove the first event when 2nd later is present
        queue.insert(event_1s.clone()).await.unwrap();
        queue.insert(event_2s.clone()).await.unwrap();
        let remove_1s = async {
            sleep(Duration::from_millis(500)).await;
            let _ = queue.remove(&event_1s).await;
            sleep(Duration::from_millis(2000)).await;
        };

        let next = select! {
            next = queue.next() => { Some(next.unwrap()) },
            _ = remove_1s => { None },
        };

        assert!(next.is_some());
        assert_eq!(next.clone().unwrap(), event_2s);
        assert!(SystemTime::now() > next.unwrap().time());

        queue.remove(&event_1s).await.unwrap();
    }

    #[tokio::test]
    #[timeout(1000)]
    async fn push_pop() {
        let queue = Queue::default();
        let id_1 = EventId::default();
        let id_2 = EventId::default();

        queue.push(id_1.clone()).await.unwrap();
        queue.push(id_2.clone()).await.unwrap();

        let next = queue.next().await.unwrap();
        assert_eq!(next.id(), id_1);

        let next = queue.next().await.unwrap();
        assert_eq!(next.id(), id_2);

        queue.push(id_1.clone()).await.unwrap();
        queue.push(id_1.clone()).await.unwrap();
        queue.push(id_1.clone()).await.unwrap();
        queue.push(id_2.clone()).await.unwrap();

        let removed = queue.pop(&id_1).await.unwrap().len();
        assert_eq!(removed, 3);

        let next = queue.next().await.unwrap();
        assert_eq!(next.id(), id_2);
    }

    #[tokio::test]
    async fn shutdown() {
        async fn get_next_event(queue: &Queue) -> Option<Result<Event>> {
            select! {
                e = queue.next() => { Some(e) },
                _ = async {
                    sleep(Duration::from_millis(500)).await;
                    queue.shutdown().await;
                    sleep(Duration::from_millis(1000)).await;
                } => { None },
            }
        }

        let queue = Queue::default();

        // Shutdown on empty queue
        let next = get_next_event(&queue).await;
        assert!(next.is_some());
        let next = next.unwrap();
        assert!(matches!(next, Err(Error::ShutdownRequested)));

        // Shutdown with events in queue
        let id = EventId::default();
        let time = SystemTime::now();
        let event_1s = Event::new(
            id.clone(),
            time.checked_add(Duration::from_secs(1)).unwrap(),
        );
        queue.insert(event_1s.clone()).await.unwrap();
        let next = get_next_event(&queue).await;

        assert!(next.is_some());
        let next = next.unwrap();
        assert!(matches!(next, Err(Error::ShutdownRequested)));
    }

    #[test]
    fn queue_index_clear() {
        let mut index = QueueIndex::new();

        index.by_time.insert(SystemTime::now(), BTreeSet::new());
        index.by_id.insert(Uuid::new_v4().into(), BTreeSet::new());

        assert!(!index.by_id.is_empty());
        assert!(!index.by_time.is_empty());

        index.clear();
        assert!(index.by_id.is_empty());
        assert!(index.by_time.is_empty());
    }

    #[tokio::test]
    async fn remove_event_from_queue() {
        let queue = Queue::default();
        let id = EventId::default();
        let time = SystemTime::now();
        let event = Event::new(id.clone(), time);

        queue.insert(event.clone()).await.unwrap();
        {
            let index = queue.index.read().await;
            assert_eq!(index.by_id.len(), index.by_time.len());
            assert_eq!(index.by_id.len(), 1);
        }

        queue.remove(&event).await.unwrap();
        {
            let index = queue.index.read().await;
            assert_eq!(index.by_id.len(), index.by_time.len());
            assert_eq!(index.by_id.len(), 0);
        }
    }

    #[tokio::test]
    async fn remove_event_from_inconsistent_queue() {
        let queue = Queue::default();
        let id = EventId::default();
        let time = SystemTime::now();
        let event = Event::new(id.clone(), time);

        queue.insert(event.clone()).await.unwrap();
        {
            let index = queue.index.read().await;
            assert_eq!(index.by_id.len(), index.by_time.len());
            assert_eq!(index.by_id.len(), 1);
        }

        {
            let mut index = queue.index.write().await;
            index.by_id.remove(&id);
        }

        let result = queue.remove(&event).await;
        assert!(result.is_err());
        assert!(matches!(
            result.err().unwrap(),
            Error::InconsistentQueueContent
        ));
    }
}