#[non_exhaustive]pub struct JobContext {
pub job_id: Uuid,
pub job_type: String,
pub attempt: u32,
pub max_attempts: u32,
pub auth: AuthContext,
/* private fields */
}Expand description
Context available to job handlers.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.job_id: UuidCurrent attempt (1-based).
job_type: String§attempt: u32§max_attempts: u32§auth: AuthContextImplementations§
Source§impl JobContext
impl JobContext
Sourcepub fn new(
job_id: Uuid,
job_type: String,
attempt: u32,
max_attempts: u32,
db_pool: PgPool,
http_client: CircuitBreakerClient,
) -> Self
pub fn new( job_id: Uuid, job_type: String, attempt: u32, max_attempts: u32, db_pool: PgPool, http_client: CircuitBreakerClient, ) -> Self
Create a new job context.
Sourcepub fn with_kv(self, kv: Arc<dyn KvHandle>) -> Self
pub fn with_kv(self, kv: Arc<dyn KvHandle>) -> Self
Attach a KV store handle. Called by the runtime before handing the context to the handler.
Sourcepub fn with_job_dispatch(self, dispatcher: Arc<dyn JobDispatch>) -> Self
pub fn with_job_dispatch(self, dispatcher: Arc<dyn JobDispatch>) -> Self
Attach a job dispatcher so dispatch_job routes through the
JobDispatch trait (the only path that resolves registered job
metadata).
Sourcepub fn with_workflow_dispatch(
self,
dispatcher: Arc<dyn WorkflowDispatch>,
) -> Self
pub fn with_workflow_dispatch( self, dispatcher: Arc<dyn WorkflowDispatch>, ) -> Self
Attach a workflow dispatcher so start_workflow routes through the
WorkflowDispatch trait, which writes the active version + signature.
Without this, dispatched workflows would resume as
BlockedSignatureMismatch on first attempt.
Sourcepub fn with_saved(self, data: Value) -> Self
pub fn with_saved(self, data: Value) -> Self
Create a new job context with persisted saved data.
Sourcepub fn with_auth(self, auth: AuthContext) -> Self
pub fn with_auth(self, auth: AuthContext) -> Self
Set authentication context.
Sourcepub fn with_tenant_id(self, tenant_id: Uuid) -> Self
pub fn with_tenant_id(self, tenant_id: Uuid) -> Self
Inject a tenant ID into the auth context claims.
Merges the tenant_id claim into the existing auth context so that
ctx.auth.tenant_id() returns the value for the duration of this job.
Used by the executor when the job record carries a tenant ID.
Sourcepub fn with_progress(self, tx: Sender<ProgressUpdate>) -> Self
pub fn with_progress(self, tx: Sender<ProgressUpdate>) -> Self
Set progress channel.
Sourcepub fn with_env_provider(self, provider: Arc<dyn EnvProvider>) -> Self
pub fn with_env_provider(self, provider: Arc<dyn EnvProvider>) -> Self
Set environment provider.
Sourcepub async fn conn(&self) -> Result<ForgeConn<'static>>
pub async fn conn(&self) -> Result<ForgeConn<'static>>
Acquire a connection compatible with sqlx compile-time checked macros.
Sourcepub fn http(&self) -> HttpClient
pub fn http(&self) -> HttpClient
Get the HTTP client for external requests.
Sourcepub fn raw_http(&self) -> &Client
pub fn raw_http(&self) -> &Client
Get the raw reqwest client, bypassing circuit breaker execution.
pub fn set_http_timeout(&mut self, timeout: Option<Duration>)
Sourcepub fn progress(&self, percentage: u8, message: impl Into<String>) -> Result<()>
pub fn progress(&self, percentage: u8, message: impl Into<String>) -> Result<()>
Report job progress.
Sourcepub async fn saved(&self) -> Value
pub async fn saved(&self) -> Value
Get all saved job data.
Returns data that was saved during job execution via save().
This data persists across retries and is accessible in compensation handlers.
Sourcepub async fn save(&self, key: &str, value: Value) -> Result<()>
pub async fn save(&self, key: &str, value: Value) -> Result<()>
Save a key-value pair to persistent job data.
Merges key into the saved data object and persists the result to the
database. Saved data survives retries and is accessible in compensation
handlers. Use this to store information needed for rollback (e.g.,
transaction IDs, resource handles, progress markers).
Read saved data back with saved().
§Example
ctx.save("charge_id", json!(charge.id)).await?;
ctx.save("refund_amount", json!(amount)).await?;Sourcepub async fn dispatch_job<T: Serialize>(
&self,
job_type: &str,
args: &T,
) -> Result<Uuid>
pub async fn dispatch_job<T: Serialize>( &self, job_type: &str, args: &T, ) -> Result<Uuid>
Dispatch a sub-job directly.
Routes through the JobDispatch trait so registered job metadata
(queue/capability, priority, retry policy) is honoured. The dispatch is
non-transactional: once the parent job returns, the child remains
enqueued regardless of success. Use the transactional dispatch on
MutationContext for commit-dependent fan-out.
Sourcepub async fn dispatch<J: ForgeJob>(&self, args: &J::Args) -> Result<Uuid>
pub async fn dispatch<J: ForgeJob>(&self, args: &J::Args) -> Result<Uuid>
Type-safe dispatch: resolves the job name from the type’s ForgeJob
impl and serializes the args at the call site.
Sourcepub async fn start_workflow<T: Serialize>(
&self,
workflow_name: &str,
args: &T,
) -> Result<Uuid>
pub async fn start_workflow<T: Serialize>( &self, workflow_name: &str, args: &T, ) -> Result<Uuid>
Start a workflow directly.
Routes through the WorkflowDispatch trait, which writes the active
version + signature onto the run row and enqueues the
$workflow_resume job. Calling raw SQL here would leave both columns
blank and the executor would immediately mark the run as
BlockedSignatureMismatch.
Sourcepub async fn is_cancel_requested(&self) -> Result<bool>
pub async fn is_cancel_requested(&self) -> Result<bool>
Check if cancellation has been requested for this job.
Sourcepub async fn check_cancelled(&self) -> Result<()>
pub async fn check_cancelled(&self) -> Result<()>
Return an error if cancellation has been requested.
Sourcepub fn is_last_attempt(&self) -> bool
pub fn is_last_attempt(&self) -> bool
Check if this is the last attempt.
Trait Implementations§
Source§impl EnvAccess for JobContext
impl EnvAccess for JobContext
fn env_provider(&self) -> &dyn EnvProvider
fn env(&self, key: &str) -> Option<String>
fn env_or(&self, key: &str, default: &str) -> String
fn env_require(&self, key: &str) -> Result<String>
fn env_parse<T: FromStr>(&self, key: &str) -> Result<T>
Source§fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
fn env_contains(&self, key: &str) -> bool
Auto Trait Implementations§
impl Freeze for JobContext
impl !RefUnwindSafe for JobContext
impl Send for JobContext
impl Sync for JobContext
impl Unpin for JobContext
impl UnsafeUnpin for JobContext
impl !UnwindSafe 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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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