10 releases (4 breaking)

0.5.1 Feb 16, 2024
0.5.0 Jan 29, 2024
0.4.2 Jan 22, 2024
0.3.0 Jan 17, 2024
0.1.2 May 30, 2023

#610 in Asynchronous

Download history 21/week @ 2024-01-12 14/week @ 2024-01-19 11/week @ 2024-01-26 2/week @ 2024-02-02 136/week @ 2024-02-16 47/week @ 2024-02-23 11/week @ 2024-03-01 5/week @ 2024-03-08 8/week @ 2024-03-15 6/week @ 2024-03-22 15/week @ 2024-03-29 169/week @ 2024-04-05

198 downloads per month
Used in spaghettinuum

ISC license

13KB
268 lines

This is a tool for managing asynchronous tasks.

For example, maybe you have some periodic background tasks, plus a couple web servers. Using a TaskManager you can group these and shut them down as a group, gracefully, and wait for them all to end.

Handles

  • Long tasks
  • Simple periodic tasks (fixed sleep between invocations)
  • Async streams
  • Ctrl+c shutdown

Use it like

let tm = TaskManager::new();
tm.attach_sigint({
    log2 = logger.clone();
    |e| log2.err("Error handling sigint", e)
});
let tm1 = tm.clone();
let log2 = logger.clone();
tm.task(async move {
    match tm1.if_alive(server).await {
        Some(r) => match r {
            Ok(_) => {} // Server exited normally
            Err(e) => {
                log.err("Server died with error", e);
            }
        },
        None => {} // Received shutdown
    };
});

tm.join().await;

Dependencies

~7–20MB
~253K SLoC