Trait background_jobs::Job[][src]

pub trait Job: 'static + DeserializeOwned + Serialize {
    type State: 'static + Clone;
    type Future: Send + Future;

    pub const NAME: &'static str;
    pub const QUEUE: &'static str;
    pub const MAX_RETRIES: MaxRetries;
    pub const BACKOFF: Backoff;
    pub const TIMEOUT: i64;

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

    pub fn queue(&self) -> &str { ... }
pub fn max_retries(&self) -> MaxRetries { ... }
pub fn backoff_strategy(&self) -> Backoff { ... }
pub 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[src]

The application state provided to this job at runtime.

type Future: Send + Future[src]

The future returned by this job

Loading content...

Associated Constants

pub const NAME: &'static str[src]

The name of the job

This name must be unique!!!

pub const QUEUE: &'static str[src]

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.

pub const MAX_RETRIES: MaxRetries[src]

Define the default number of retries for this job

Defaults to Count(5) Jobs can override

pub const BACKOFF: Backoff[src]

Define the default backoff strategy for this job

Defaults to Exponential(2) Jobs can override

pub const TIMEOUT: i64[src]

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

pub fn run(self, state: Self::State) -> Self::Future[src]

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

pub fn queue(&self) -> &str[src]

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

pub fn max_retries(&self) -> MaxRetries[src]

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

pub fn backoff_strategy(&self) -> Backoff[src]

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

pub fn timeout(&self) -> i64[src]

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, Global>>

Loading content...