Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Job

As already mentioned in the Pouch chapter, Quokka provides you with a run_setup_job method in your Pouch to run some async setup tasks.

To build on top of that and to make it more Quokka like, there is the quokka::job module, coming with the quokka::job::Job trait which allows you to create a job as a separate struct allowing you to keep your code split up and reusable. It does not change the behaviour or time when jobs are being run, but it gives it a cleaner interface. It also allows other modules to expose some initial setup tasks which can be reusable and accept some input data.

To use jobs you have to load the quokka::job::JobPouch.

Example

This example shows the bare minimum of quokka jobs, showing it's reusability and keeping you away of this scary return type of the original run_setup_job() -> Pin<Box<dyn Future<Output = Result<()>> + Send>> function.

use std::convert::Infallible;

use quokka::job::{Job, Jobs, JobStateExt};

// The job can be built with some data, different for each module, allowing them to use the same job for their purpose.
struct GreetModule(String);

impl<S: Clone + Send + Sync + 'static> Job<S> for GreetModule {
    type Error = Infallible;

    async fn handle(&self, _: &S) -> std::result::Result<(), Self::Error> {
        println!("Hello {}", self.0);

        Ok(())
    }
}

fn configure_state<S: Clone + Send + Sync + 'static + ProvideStateRef<Jobs<S>>>(state: S)  -> quokka::pouch::Result<()> {
    let mut jobs: Jobs<S> = state.provide_mut();

    jobs.register_job(GreetModule(String::from("My module")));

    Ok(())
}