pub struct JobContext {
pub job_id: Uuid,
pub queue: String,
pub attempt: u32,
pub enqueued_at: DateTime<Utc>,
/* private fields */
}Expand description
Injected into every job execution. Provides job metadata and shared app state.
§Accessing shared state
State is injected via Queue::with_state and
accessed with ctx.state::<T>() — the same mental model as axum’s State<T>:
ⓘ
// Set up — pass your app state once when building the queue
let queue = Queue::new(backend).with_state(Arc::new(db_pool));
// Inside a job — retrieve it by type
impl Perform for MyJob {
async fn perform(self, ctx: JobContext) -> JobResult {
let db = ctx.state::<Arc<PgPool>>().expect("db not in state");
// use db ...
Ok(())
}
}Fields§
§job_id: UuidUnique ID of this job instance.
queue: StringName of the queue this job was claimed from.
attempt: u32Delivery attempt number (1 = first attempt).
enqueued_at: DateTime<Utc>When this job was originally enqueued.
Implementations§
Source§impl JobContext
impl JobContext
Sourcepub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>
Retrieve shared state by type.
Returns None if no state of type T was set on the queue.
ⓘ
let pool = ctx.state::<Arc<PgPool>>().expect("PgPool not in state");Sourcepub fn is_first_attempt(&self) -> bool
pub fn is_first_attempt(&self) -> bool
Whether this is the first attempt (attempt == 1).
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for JobContext
impl !UnwindSafe for JobContext
impl Freeze for JobContext
impl Send for JobContext
impl Sync for JobContext
impl Unpin for JobContext
impl UnsafeUnpin for JobContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more