Skip to main content

JobContext

Struct JobContext 

Source
pub struct JobContext {
    pub job: JobRow,
    /* private fields */
}
Expand description

Context passed to worker handlers during job execution.

Provides access to the job metadata, shared state (e.g., service dependencies), callback registration, and structured progress reporting.

Fields§

§job: JobRow

The raw job row from the database.

Implementations§

Source§

impl JobContext

Source

pub fn is_cancelled(&self) -> bool

Check if this job’s execution has been cancelled (shutdown or deadline).

Source

pub fn cancellation_flag(&self) -> Arc<AtomicBool>

Clone the shared cancellation flag for language bridges.

Source

pub fn cancel(&self)

Signal cancellation for this job.

Source

pub fn extract<T: Any + Send + Sync + Clone>(&self) -> Option<T>

Extract a shared state value by type.

State values are registered via Client::builder().state(value). Register the concrete type you want to extract:

// Register with the type you'll extract:
let deps = Arc::new(MyDeps::new());
Client::builder(pool)
    .state(deps.clone())  // stores Arc<MyDeps>
    .build()?;

// In handler — extract the same type:
let deps = ctx.extract::<Arc<MyDeps>>().unwrap();
Source

pub fn pool(&self) -> &PgPool

Get a reference to the database pool.

Source

pub fn queue_storage_store(&self) -> Option<Arc<QueueStorage>>

Get the active queue-storage backend for this job, if any.

Source

pub async fn register_callback( &self, timeout: Duration, ) -> Result<CallbackGuard, AwaError>

Register a callback for this job, writing the callback_id to the database immediately.

Call this BEFORE sending the callback_id to the external system to avoid the race condition where the external system fires before the DB knows about the callback.

Returns a CallbackGuard whose id should be included in the URL or payload sent to the external system.

Source

pub async fn register_callback_with_config( &self, timeout: Duration, config: &CallbackConfig, ) -> Result<CallbackGuard, AwaError>

Register a callback with CEL expressions for automatic resolution.

See CallbackConfig for expression semantics.

Source

pub async fn wait_for_callback( &self, guard: CallbackGuard, ) -> Result<Value, AwaError>

Wait for an external callback to resolve, then resume with the payload.

This enables sequential callbacks: the handler can register a callback, wait for it, process the result, register another callback, wait again, and so on — all within a single handler invocation.

The handler’s async task suspends while waiting. The job transitions to waiting_external. When resume_external(callback_id, payload) is called, the job transitions back to running and this method returns the payload.

The handler holds its permit (worker slot) during the wait. For very long waits, consider whether the single-shot WaitForCallback pattern (which releases the permit) is more appropriate.

let token = ctx.register_callback(Duration::from_secs(3600)).await?;
send_to_external_system(token.id());
let payload = ctx.wait_for_callback(token).await?;
// Handler resumes here with the external system's response
Source

pub fn set_progress(&self, percent: u8, message: &str)

Set structured progress (0-100 with message). Sync — writes to in-memory buffer.

percent is clamped to 0-100. For progress without a message, pass "".

Source

pub fn update_metadata(&self, updates: Value) -> Result<(), AwaError>

Shallow-merge keys into progress.metadata for checkpointing. Sync.

updates must be a JSON object. Top-level keys overwrite; nested objects are replaced, not deep-merged.

Source

pub async fn flush_progress(&self) -> Result<(), AwaError>

Force immediate flush of pending progress to DB. For critical checkpoints.

Does not return success until the progress has been durably written or the job is no longer in running state (rescued/cancelled).

Source

pub fn progress_buffer(&self) -> Arc<Mutex<ProgressState>>

Get a clone of the shared progress state Arc (for Python bridge).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more