Expand description
§Work Manager
work-manager is a library designed to simplify the management of asynchronous tasks.
It allows you to define Jobs, chain them into Sequences, and schedule them using a WorkManager.
§Key Features
- Job Abstraction: Implement the
Jobtrait to define units of work. - Job Sequencing: Chain jobs together using
Job::then, passing results from one to the next. - Task Management: Use
WorkManagerto enqueue one-off or periodic jobs. - Cancellation: Easily cancel jobs by name.
§Example
use work_manager::{Job, WorkManager};
use std::time::Duration;
#[derive(Clone)]
struct PrintJob {
message: String,
}
impl Job for PrintJob {
type Output = ();
type Error = std::io::Error;
async fn run(self) -> Result<Self::Output, Self::Error> {
println!("{}", self.message);
Ok(())
}
}
let mut manager = WorkManager::new();
// Enqueue a single job
manager.enqueue("print_hello", PrintJob { message: "Hello".into() }, false);
// Enqueue a periodic job
manager.enqueue_periodic(
"print_tick",
PrintJob { message: "Tick".into() },
Duration::from_secs(1),
false,
|_| Ok(())
);Structs§
- Sequence
- A struct representing a sequence where the second job is generated based on the first and the result is passed into its next function generator.
- Work
Manager - Manages the execution of asynchronous jobs.
Traits§
- Job
- A job that can be run asynchronously.