pub struct WorkerContext {
pub config: WorkerConfig,
pub started_at: DateTime<Utc>,
pub execution_metadata: HashMap<String, String>,
pub attempt: u32,
pub previous_exception: Option<String>,
pub cancel: CancellationToken,
}Expand description
Context information provided to workers during job execution
Fields§
§config: WorkerConfigWorker configuration
started_at: DateTime<Utc>When the job execution started
execution_metadata: HashMap<String, String>Metadata for the current execution
attempt: u32Attempt number (for retries)
previous_exception: Option<String>Previous exception if this is a retry
cancel: CancellationTokenCancellation token for cooperative shutdown. A long-running worker impl can race its work against this token to drop out cleanly when the server is asked to stop:
tokio::select! {
_ = ctx.cancel.cancelled() => Ok(WorkerResult::retry(
"shutting down".into(),
None,
)),
res = do_expensive_work() => res,
}The token is a child of the BackgroundJobServer shutdown token, so
calling server.stop() flips every context in flight.
Implementations§
Source§impl WorkerContext
impl WorkerContext
Sourcepub fn new(config: WorkerConfig) -> Self
pub fn new(config: WorkerConfig) -> Self
Create a new worker context with a detached cancellation token. The
server installs a real, shutdown-linked token via
WorkerContext::with_cancel.
Sourcepub fn retry_from(
config: WorkerConfig,
attempt: u32,
previous_exception: Option<String>,
) -> Self
pub fn retry_from( config: WorkerConfig, attempt: u32, previous_exception: Option<String>, ) -> Self
Create a retry context from a previous attempt
Sourcepub fn with_cancel(self, cancel: CancellationToken) -> Self
pub fn with_cancel(self, cancel: CancellationToken) -> Self
Override the cancellation token. Builder-style so the server can install the shutdown-linked child token.
Sourcepub fn add_metadata(&mut self, key: impl Into<String>, value: impl Into<String>)
pub fn add_metadata(&mut self, key: impl Into<String>, value: impl Into<String>)
Add execution metadata
Sourcepub fn is_timed_out(&self) -> bool
pub fn is_timed_out(&self) -> bool
Check if the job has timed out
Trait Implementations§
Source§impl Clone for WorkerContext
impl Clone for WorkerContext
Source§fn clone(&self) -> WorkerContext
fn clone(&self) -> WorkerContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more