Skip to main content

JobProcessor

Trait JobProcessor 

Source
pub trait JobProcessor: Sync + Send {
    type Job: Send + 'static;
    type JobId: Send + Sync + Debug + 'static;
    type JobArtifacts: Send + 'static;

    const SERVICE_NAME: &'static str;
    const POLLING_INTERVAL_MS: u64 = 1000;
    const MAX_BACKOFF_MS: u64 = 60_000;
    const BACKOFF_MULTIPLIER: u64 = 2;

    // Required methods
    fn get_next_job<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<Option<(Self::JobId, Self::Job)>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn save_failure<'life0, 'async_trait>(
        &'life0 self,
        job_id: Self::JobId,
        started_at: Instant,
        error: String,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn process_job<'life0, 'life1, 'async_trait>(
        &'life0 self,
        job_id: &'life1 Self::JobId,
        job: Self::Job,
        started_at: Instant,
    ) -> Pin<Box<dyn Future<Output = JoinHandle<Result<Self::JobArtifacts>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn save_result<'life0, 'async_trait>(
        &'life0 self,
        job_id: Self::JobId,
        started_at: Instant,
        artifacts: Self::JobArtifacts,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn max_attempts(&self) -> u32;
    fn get_job_attempts<'life0, 'life1, 'async_trait>(
        &'life0 self,
        job_id: &'life1 Self::JobId,
    ) -> Pin<Box<dyn Future<Output = Result<u32>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn run<'async_trait>(
        self,
        stop_receiver: Receiver<bool>,
        iterations_left: Option<usize>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: Sized + 'async_trait { ... }
    fn wait_for_task<'life0, 'life1, 'async_trait>(
        &'life0 self,
        job_id: Self::JobId,
        started_at: Instant,
        task: JoinHandle<Result<Self::JobArtifacts>>,
        stop_receiver: &'life1 mut Receiver<bool>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}

Required Associated Constants§

Source

const SERVICE_NAME: &'static str

Provided Associated Constants§

Required Associated Types§

Source

type Job: Send + 'static

Source

type JobId: Send + Sync + Debug + 'static

Source

type JobArtifacts: Send + 'static

Required Methods§

Source

fn get_next_job<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Option<(Self::JobId, Self::Job)>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns None when there is no pending job Otherwise, returns Some(job_id, job) Note: must be concurrency-safe - that is, one job must not be returned in two parallel processes

Source

fn save_failure<'life0, 'async_trait>( &'life0 self, job_id: Self::JobId, started_at: Instant, error: String, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Invoked when process_job panics Should mark the job as failed

Source

fn process_job<'life0, 'life1, 'async_trait>( &'life0 self, job_id: &'life1 Self::JobId, job: Self::Job, started_at: Instant, ) -> Pin<Box<dyn Future<Output = JoinHandle<Result<Self::JobArtifacts>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Function that processes a job

Source

fn save_result<'life0, 'async_trait>( &'life0 self, job_id: Self::JobId, started_at: Instant, artifacts: Self::JobArtifacts, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Invoked when process_job doesn’t panic

Source

fn max_attempts(&self) -> u32

Source

fn get_job_attempts<'life0, 'life1, 'async_trait>( &'life0 self, job_id: &'life1 Self::JobId, ) -> Pin<Box<dyn Future<Output = Result<u32>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Invoked in wait_for_task for in-progress job.

Provided Methods§

Source

fn run<'async_trait>( self, stop_receiver: Receiver<bool>, iterations_left: Option<usize>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: Sized + 'async_trait,

iterations_left: To run indefinitely, pass None, To process one job, pass Some(1), To process a batch, pass Some(batch_size).

Source

fn wait_for_task<'life0, 'life1, 'async_trait>( &'life0 self, job_id: Self::JobId, started_at: Instant, task: JoinHandle<Result<Self::JobArtifacts>>, stop_receiver: &'life1 mut Receiver<bool>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Polls task handle, saving its outcome.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§