laravel-rust-queue 0.1.0

Internal crate of Laravel for Rust: jobs, the queue drivers, the worker, batches, and chains. Depend on `laravel-rust` instead
Documentation
//! Events that the queue worker dispatches as it processes jobs.
//!
//! The `sync` connection and `dispatch_sync` don't dispatch these events. Listeners run in the
//! job's context, so they see the context values that the job was dispatched with and the values
//! it added while it ran:
//!
//! ```
//! use laravel::Application;
//! use laravel::event::Event;
//! use laravel::queue::events::JobFailed;
//!
//! Application::configure().with_events(|events, _| {
//!     events.listen(|event: Event<JobFailed>| async move {
//!         tracing::error!(
//!             job = %event.job.name,
//!             error = %event.error,
//!             "A job failed for good",
//!         );
//!     });
//! });
//! ```

use std::fmt;

use super::JobId;
use crate::time::ExactDuration;

/// The job that an event is about, and where the job ran.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct JobInfo {
    /// The ID that the job's dispatch returned.
    pub id: JobId,
    /// The job's registered name.
    pub name: String,
    /// The connection the job came from.
    pub connection: String,
    /// The queue the job came from.
    pub queue: String,
    /// The number of this run, starting at 1.
    pub attempt: u32,
}

/// The event dispatched after each attempt of a job, however the attempt ended.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct JobProcessed {
    /// The job that ran.
    pub job: JobInfo,
    /// How the attempt ended.
    pub outcome: JobOutcome,
    /// How long the attempt took, including its `failed` hook.
    pub duration: ExactDuration,
}

/// The event dispatched when a job fails for good, after the job's `failed` hook runs.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct JobFailed {
    /// The job that failed.
    pub job: JobInfo,
    /// The error the job failed with.
    ///
    /// Check what failed with [`Error::is`](crate::Error::is), such as
    /// `error.is::<JobTimedOut>()` for [`JobTimedOut`](crate::queue::JobTimedOut).
    pub error: crate::Error,
}

/// How an attempt of a job ended.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum JobOutcome {
    /// The attempt finished, and the job was removed from its queue.
    Succeeded,
    /// The attempt released the job to run again after a delay.
    Released,
    /// The attempt deleted the job.
    Deleted,
    /// A later dispatch of the debounced job replaced this one, so the job was removed without
    /// running.
    Debounced,
    /// The row of one of the job's models was gone, so the job was removed without running, and
    /// its chain ended.
    MissingModels,
    /// The attempt failed, and the job will be retried after its backoff.
    Retrying,
    /// The attempt failed, and the job failed for good.
    Failed,
}

impl fmt::Display for JobOutcome {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Succeeded => "succeeded",
            Self::Released => "released",
            Self::Deleted => "deleted",
            Self::Debounced => "debounced",
            Self::MissingModels => "missing models",
            Self::Retrying => "retrying",
            Self::Failed => "failed",
        })
    }
}