[][src]Trait async_jobs::Job

pub trait Job: Sized + PartialEq {
    type Error;
    fn dependencies(&self) -> Vec<Self>;
#[must_use] fn run<'life0, 'async_trait>(
        &'life0 self
    ) -> Pin<Box<dyn Future<Output = Result<Outcome, Self::Error>> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
; }

A unit of work, perhaps with dependencies

Use the Job trait to describe the different types of jobs in your program and how they depend on one another. An implementation has two responsibilities:

  1. Do some job-specific work when the run method is called
  2. Provide a list of dependency jobs via dependencies

Note that the return type of dependencies is Vec<Self>. This restriction means that you cannot use different implementations of Job to represent different types of work in your program; you must provide exactly one type. You may find it useful to use an enum:

use async_jobs::{Job, Outcome};
use async_trait::async_trait;

#[derive(PartialEq)]
enum MyJob {
    DownloadFile(String),
    ConcatFiles(Vec<String>),
}

#[async_trait]
impl Job for MyJob {

    type Error = ();

    async fn run(&self) -> Result<Outcome, Self::Error> {

        match self {

            MyJob::DownloadFile(file) => {
                // download file...
            },

            MyJob::ConcatFiles(files) => {
                // concatenate files...
            },
        }

        Ok(Outcome::Success)
    }

    fn dependencies(&self) -> Vec<Self> {

        match self {

            MyJob::DownloadFile(_) => vec![],

            MyJob::ConcatFiles(files) => {
                files.iter()
                    .map(|f| MyJob::DownloadFile(f.clone()))
                    .collect()
            },
        }
    }
}

Associated Types

type Error

Error type returned by the implementation

Loading content...

Required methods

fn dependencies(&self) -> Vec<Self>

Returns the list of jobs that this job depends on

#[must_use]fn run<'life0, 'async_trait>(
    &'life0 self
) -> Pin<Box<dyn Future<Output = Result<Outcome, Self::Error>> + Send + 'async_trait>> where
    'life0: 'async_trait,
    Self: 'async_trait, 

Performs the work associated with this job

Loading content...

Implementors

Loading content...