[][src]Trait background_jobs::Job

pub trait Job: 'static + DeserializeOwned + Serialize where
    <Self::Future as Future>::Output == Result<(), Error>, 
{ type State: 'static + Clone; type Future: Send + Future; const NAME: &'static str; const QUEUE: &'static str; const MAX_RETRIES: MaxRetries; const BACKOFF: Backoff; const TIMEOUT: i64; fn run(self, state: Self::State) -> Self::Future; fn queue(&self) -> &str { ... }
fn max_retries(&self) -> MaxRetries { ... }
fn backoff_strategy(&self) -> Backoff { ... }
fn timeout(&self) -> i64 { ... } }

The Job trait defines parameters pertaining to an instance of background job

Jobs are defnitions of work to be executed.

The simplest implementation defines the job's State and Future types, NAME contant, and run method.

Example

use anyhow::Error;
use background_jobs_core::{Job, new_job};
use futures::future::{ok, Ready};
use log::info;

#[derive(serde::Deserialize, serde::Serialize)]
struct MyJob {
    count: i64,
}

impl Job for MyJob {
    type State = ();
    type Future = Ready<Result<(), Error>>;

    const NAME: &'static str = "MyJob";

    fn run(self, _: Self::State) -> Self::Future {
        info!("Processing {}", self.count);

        ok(())
    }
}

fn main() -> Result<(), Error> {
    let job = new_job(MyJob { count: 1234 })?;

    Ok(())
}

Associated Types

type State: 'static + Clone

The application state provided to this job at runtime.

type Future: Send + Future

The future returned by this job

Loading content...

Associated Constants

const NAME: &'static str

The name of the job

This name must be unique!!!

const QUEUE: &'static str

The name of the default queue for this job

This can be overridden on an individual-job level, but if a non-existant queue is supplied, the job will never be processed.

const MAX_RETRIES: MaxRetries

Define the default number of retries for this job

Defaults to Count(5) Jobs can override

const BACKOFF: Backoff

Define the default backoff strategy for this job

Defaults to Exponential(2) Jobs can override

const TIMEOUT: i64

Define the maximum number of milliseconds a job should be allowed to run before being considered dead.

This is important for allowing the job server to reap processes that were started but never completed.

Defaults to 15 seconds Jobs can override

Loading content...

Required methods

fn run(self, state: Self::State) -> Self::Future

Users of this library must define what it means to run a job.

This should contain all the logic needed to complete a job. If that means queuing more jobs, sending an email, shelling out (don't shell out), or doing otherwise lengthy processes, that logic should all be called from inside this method.

The state passed into this job is initialized at the start of the application. The state argument could be useful for containing a hook into something like r2d2, or the address of an actor in an actix-based system.

Loading content...

Provided methods

fn queue(&self) -> &str

If this job should not use it's default queue, this can be overridden in user-code.

fn max_retries(&self) -> MaxRetries

If this job should not use it's default maximum retry count, this can be overridden in user-code.

fn backoff_strategy(&self) -> Backoff

If this job should not use it's default backoff strategy, this can be overridden in user-code.

fn timeout(&self) -> i64

Define the maximum number of milliseconds this job should be allowed to run before being considered dead.

This is important for allowing the job server to reap processes that were started but never completed.

Loading content...

Implementors

impl<T> Job for T where
    T: ActixJob
[src]

type State = <T as ActixJob>::State

type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + 'static + Send>>

Loading content...