Skip to content

Commit

Permalink
example: simplify example with tokio select
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Nov 18, 2024
1 parent b5a549b commit c7554c4
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions examples/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ async fn worker(name: u64) {
let mut counter = 0;
loop {
let result = tokio::select! {
_ = guard.wait() => None,
r = do_work(name) => Some(r),
};

let result = match result {
None => break,
Some(x) => x,
r = do_work(name) => r,
_ = guard.wait() => break,
};

counter += result;
Expand All @@ -34,15 +29,19 @@ async fn worker(name: u64) {
}

async fn important_worker() {
let _guard = elegant_departure::get_shutdown_guard().shutdown_on_drop();
let guard = elegant_departure::get_shutdown_guard().shutdown_on_drop();

// Do some important work.
for i in 0.. {
sleep(Duration::from_secs(1)).await;
// 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] Did some important work and is shutting down");
}
}

Expand Down

0 comments on commit c7554c4

Please sign in to comment.