The Task Object
Every service method execution has access to contextual information through theTask object. This provides metadata about the current task, the workflow that triggered it (if any), and access to the Infinitic client.
Location in source code: io.infinitic.tasks.Task
Task Properties
TheTask object provides the following properties:
Basic Task Information
Task.taskId
The unique identifier for the current task.Task.taskName
The name of the task method being executed.Task.serviceName
The name of the service this task belongs to.Task.workerName
The name of the worker executing this task.Workflow Information
These properties are available when the task is called from a workflow:Task.workflowId
The unique identifier of the workflow that triggered this task (null if not called from a workflow).Task.workflowName
The name of the workflow that triggered this task.Task.workflowVersion
The version of the workflow that triggered this task.Retry Information
Task.retrySequence
The sequence number of the current retry attempt (0 for first execution).Task.retryIndex
The index of the current retry attempt.Task.lastError
Information about the error from the previous attempt (null on first attempt).Tags and Metadata
Task.tags
A set of tags associated with this task.Task.meta
A mutable map of metadata associated with the task. You can read and write to this metadata.Batch Information
Task.batchKey
The batch key for this task when using batch processing (null if not batched).Configuration Information
Task.withRetry
The retry configuration for this task.Task.withTimeout
The timeout configuration for this task.The Task Context
The fullTaskContext interface provides all contextual information:
Accessing the Infinitic Client
TheTask.client property provides access to the Infinitic client, allowing services to:
- Dispatch other tasks
- Start workflows
- Interact with running workflows
Timeout Handling
Task.hasTimedOut
Check if the current task execution has timed out.Task.onTimeOut
Register a callback to be executed when the task times out.Batch Context Access
In batch methods, useTask.getContext(taskId) to access the context for individual tasks:
Practical Examples
Using Task Context for Logging
Conditional Logic Based on Retry Attempt
Using Metadata for Tracking
Starting Child Workflows
Testing with Task Context
When writing tests, you can mock the task context:Next Steps
Delegated Tasks
Integrate with external systems for long-running operations
Batching
Process multiple tasks efficiently with batch operations