Skip to main content

TaskContext

Struct TaskContext 

Source
pub struct TaskContext { /* private fields */ }
Expand description

Context for the currently executing task.

This provides convenient access to task metadata and execution state.

§Example

use conductor::worker::{Worker, WorkerOutput, TaskContext};
use conductor::models::Task;

async fn process_long_task(task: &Task) -> WorkerOutput {
    let ctx = TaskContext::from_task(task);
     
    // Check how many times this task has been polled
    let poll_count = ctx.poll_count();
     
    if poll_count > 10 {
        // Too many iterations, fail
        return WorkerOutput::failed("Max iterations exceeded");
    }
     
    // Process work based on poll_count
    let processed = process_chunk(poll_count);
     
    if is_complete() {
        WorkerOutput::completed_with_result(processed)
    } else {
        // More work to do
        WorkerOutput::in_progress(30)
    }
}

Implementations§

Source§

impl TaskContext

Source

pub fn from_task(task: &Task) -> Self

Create a TaskContext from a Task.

Source

pub fn task_id(&self) -> &str

Get the unique task ID.

Source

pub fn workflow_instance_id(&self) -> &str

Get the workflow instance ID.

Source

pub fn task_type(&self) -> &str

Get the task type (definition name).

Source

pub fn reference_task_name(&self) -> &str

Get the reference task name within the workflow.

Source

pub fn poll_count(&self) -> i32

Get the poll count for long-running tasks.

This increments each time the task is polled after returning TaskInProgress. Use this to track progress or implement pagination in long-running tasks.

§Example
let task = Task::default();
let ctx = TaskContext::from_task(&task);

// Process different chunks based on poll count
let offset = ctx.poll_count() * 100;
// process_items(offset, 100);
Source

pub fn retry_count(&self) -> i32

Get the current retry count.

Indicates how many times this task has been retried after failures.

Source

pub fn correlation_id(&self) -> Option<&str>

Get the correlation ID (if set).

Source

pub fn domain(&self) -> Option<&str>

Get the domain (if set).

Source

pub fn scheduled_time(&self) -> i64

Get the scheduled time (epoch milliseconds).

Source

pub fn start_time(&self) -> i64

Get the start time (epoch milliseconds).

Source

pub fn iteration(&self) -> i32

Get the iteration count (for loop tasks).

Source

pub fn is_first_poll(&self) -> bool

Check if this is the first poll (poll_count == 0).

Source

pub fn is_retry(&self) -> bool

Check if this is a retry (retry_count > 0).

Trait Implementations§

Source§

impl Clone for TaskContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TaskContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<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