forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.rs
More file actions
57 lines (48 loc) · 1.68 KB
/
task.rs
File metadata and controls
57 lines (48 loc) · 1.68 KB
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
//! Implements a task-running system which allows asynchronous
//! tasks to run without worry of preemption during server shutdown.
use futures::Future;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::runtime;
use tokio::sync::Notify;
/// Stores tasks which must complete before the server may shut down.
pub struct RunningTasks(Arc<RunningTasksInner>);
struct RunningTasksInner {
count: AtomicUsize,
/// Notify handle which should be notified
/// when a task completes. Allows the server
/// to wait for tasks to complete.
notify: Notify,
/// Runtime to spawn tasks on.
runtime: runtime::Handle,
}
impl RunningTasks {
pub fn new(runtime: runtime::Handle) -> Self {
Self(Arc::new(RunningTasksInner {
count: Default::default(),
notify: Notify::new(),
runtime,
}))
}
/// Schedules an asynchronous task to run.
///
/// The task is guaranteed to finish, even if the server
/// starts shutting down during its execution.
pub fn schedule(&self, task: impl Future + Send + 'static) {
self.0.count.fetch_add(1, Ordering::Relaxed);
let running_tasks = Arc::clone(&self.0);
self.0.runtime.enter(move || {
tokio::spawn(async move {
task.await;
running_tasks.count.fetch_sub(1, Ordering::Relaxed);
running_tasks.notify.notify();
});
});
}
/// Waits for all tasks to complete. Should be called before the program exits.
pub async fn wait(&self) {
while self.0.count.load(Ordering::Relaxed) > 0 {
self.0.notify.notified().await;
}
}
}