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
impl TaskContext
Sourcepub fn workflow_instance_id(&self) -> &str
pub fn workflow_instance_id(&self) -> &str
Get the workflow instance ID.
Sourcepub fn reference_task_name(&self) -> &str
pub fn reference_task_name(&self) -> &str
Get the reference task name within the workflow.
Sourcepub fn poll_count(&self) -> i32
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);Sourcepub fn retry_count(&self) -> i32
pub fn retry_count(&self) -> i32
Get the current retry count.
Indicates how many times this task has been retried after failures.
Sourcepub fn correlation_id(&self) -> Option<&str>
pub fn correlation_id(&self) -> Option<&str>
Get the correlation ID (if set).
Sourcepub fn scheduled_time(&self) -> i64
pub fn scheduled_time(&self) -> i64
Get the scheduled time (epoch milliseconds).
Sourcepub fn start_time(&self) -> i64
pub fn start_time(&self) -> i64
Get the start time (epoch milliseconds).
Sourcepub fn is_first_poll(&self) -> bool
pub fn is_first_poll(&self) -> bool
Check if this is the first poll (poll_count == 0).
Trait Implementations§
Source§impl Clone for TaskContext
impl Clone for TaskContext
Auto Trait Implementations§
impl Freeze for TaskContext
impl RefUnwindSafe for TaskContext
impl Send for TaskContext
impl Sync for TaskContext
impl Unpin for TaskContext
impl UnsafeUnpin for TaskContext
impl UnwindSafe for TaskContext
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