-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.rs
57 lines (46 loc) · 1.4 KB
/
worker.rs
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
use std::time::Duration;
use tokio::time::sleep;
async fn do_work(num: u64) -> usize {
sleep(Duration::from_millis(num * 300)).await;
1
}
async fn worker(name: u64) {
let guard = elegant_departure::get_shutdown_guard();
let mut counter = 0;
loop {
let result = tokio::select! {
r = do_work(name) => r,
_ = guard.wait() => break,
};
counter += result;
println!("[worker {}] Did some hard work", name);
}
println!(
"[worker {}] Created {} work units, cleaning up",
name, counter
);
sleep(Duration::from_secs(1)).await;
println!("[worker {}] Done", name);
}
async fn important_worker() {
let guard = elegant_departure::get_shutdown_guard().shutdown_on_drop();
for i in 0.. {
// Do some important work and wait for the shutdown.
tokio::select! {
_ = sleep(Duration::from_secs(1)) => {},
_ = guard.wait() => break,
};
if i == 5 {
panic!("Oh no an unexpected crash in the important worker!");
}
println!("[important_worker] Did some important work");
}
println!("[important_worker] Important worker is shutting down");
}
#[tokio::main]
async fn main() {
tokio::spawn(worker(1));
tokio::spawn(worker(2));
tokio::spawn(important_worker());
elegant_departure::tokio::depart().on_termination().await
}