Crate async_jobs

Source
Expand description

The async-jobs crate provides a framework for describing and executing a collection of interdependent and asynchronous jobs. It is intended to be used as the scheduling backbone for programs such as build systems which need to orchestrate arbitrary collections of tasks with complex dependency graphs.

The main way to use this crate is by creating implementations of the IntoJob trait to describe the tasks in your program and how they depend on one another. To run your jobs, create a Scheduler and pass a job to its run method.

Jobs are generic over the types C and E, which are the user-defined context and error types, respectively. The context mechanism provides a means for all jobs to access some shared bit of data; see Scheduler for more information. The error type allows jobs to return an arbitrary error value that is propagated up through the scheduler. Note that all jobs within the same job graph must use the same types for C and E.

§Example

#[derive(PartialEq)]
struct Foo(usize);

impl IntoJob<(), ()> for Foo {
    fn into_job(&self) -> Job<(), ()> {
        let num = self.0;
        Box::new(move |_| Box::pin(async move {
            println!("foo: {}", num);
            Ok(Outcome::Success)
        }))
    }
}

#[derive(PartialEq)]
struct Bar(usize);

impl IntoJob<(), ()> for Bar {
    fn plan(&self, plan: &mut PlanBuilder<(), ()>) -> Result<(), Error<()>> {
        plan.add_dependency(Foo(self.0 + 1))?;
        plan.add_dependency(Foo(self.0 + 2))?;
        Ok(())
    }

    fn into_job(&self) -> Job<(), ()> {
        let num = self.0;
        Box::new(move |_| Box::pin(async move {
            println!("bar: {}", num);
            Ok(Outcome::Success)
        }))
    }
}

let mut sched = Scheduler::new();
async_std::task::block_on(sched.run(Bar(7)));

Structs§

PlanBuilder
Representation of an “under construction” execution plan
Scheduler
Schedules execution of jobs and dependencies

Enums§

Error
Errors returned by job scheduler
Outcome
Possible job outcomes

Traits§

IntoJob
Information needed to schedule and execute a job

Type Aliases§

Job
Unit of asynchronous work