pub trait Task: Send + Sync {
// Required methods
fn execute<'life0, 'async_trait>(
&'life0 self,
context: Context<Value>,
) -> Pin<Box<dyn Future<Output = Result<Context<Value>, TaskError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn id(&self) -> &str;
fn dependencies(&self) -> &[TaskNamespace];
// Provided methods
fn checkpoint(
&self,
_context: &Context<Value>,
) -> Result<(), CheckpointError> { ... }
fn retry_policy(&self) -> RetryPolicy { ... }
fn trigger_rules(&self) -> Value { ... }
fn code_fingerprint(&self) -> Option<String> { ... }
}Expand description
Core trait that defines an executable task in a pipeline.
Tasks are the fundamental units of work in Cloacina. Most users should use the
#[task] macro instead of implementing this trait directly, as the macro provides
automatic registration, code fingerprinting, and convenient syntax.
§Task Execution Model
Tasks follow a simple but powerful execution model:
- Input: Receive a context containing data from previous tasks
- Processing: Execute the task’s business logic
- Output: Update the context with results
- Completion: Return success or failure
§Using the Macro (Recommended)
use cloacina_workflow::*;
#[task(id = "my_task", dependencies = [])]
async fn my_task(context: &mut Context<serde_json::Value>) -> Result<(), TaskError> {
// Your task logic here
Ok(())
}Required Methods§
Sourcefn execute<'life0, 'async_trait>(
&'life0 self,
context: Context<Value>,
) -> Pin<Box<dyn Future<Output = Result<Context<Value>, TaskError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn execute<'life0, 'async_trait>(
&'life0 self,
context: Context<Value>,
) -> Pin<Box<dyn Future<Output = Result<Context<Value>, TaskError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Executes the task with the provided context.
This is the main entry point for task execution. The method receives a context containing data from previous tasks and should return an updated context with any new or modified data.
§Arguments
context- The execution context containing task data
§Returns
Ok(Context)- Updated context with task resultsErr(TaskError)- If the task execution fails
Sourcefn id(&self) -> &str
fn id(&self) -> &str
Returns the unique identifier for this task.
The task ID must be unique within a Workflow or TaskRegistry. It’s used for dependency resolution and task lookup.
Sourcefn dependencies(&self) -> &[TaskNamespace]
fn dependencies(&self) -> &[TaskNamespace]
Returns the list of task namespaces that this task depends on.
Dependencies define the execution order - this task will only execute after all its dependencies have completed successfully.
Provided Methods§
Sourcefn checkpoint(&self, _context: &Context<Value>) -> Result<(), CheckpointError>
fn checkpoint(&self, _context: &Context<Value>) -> Result<(), CheckpointError>
Saves a checkpoint for this task.
This method is called to save intermediate state during task execution. The default implementation is a no-op, but tasks can override this to implement custom checkpointing logic.
§Arguments
context- The current execution context
§Returns
Ok(())- If checkpointing succeedsErr(CheckpointError)- If checkpointing fails
Sourcefn retry_policy(&self) -> RetryPolicy
fn retry_policy(&self) -> RetryPolicy
Returns the retry policy for this task.
This method defines how the task should behave when it fails, including the number of retry attempts, backoff strategy, and conditions under which retries should be attempted.
The default implementation returns a sensible production-ready policy with exponential backoff and 3 retry attempts.
Sourcefn trigger_rules(&self) -> Value
fn trigger_rules(&self) -> Value
Returns the trigger rules for this task.
Trigger rules define the conditions under which this task should execute beyond simple dependency satisfaction. The default implementation returns an “Always” trigger rule, meaning the task executes whenever its dependencies are satisfied.
§Returns
A JSON value representing the trigger rules for this task.
Sourcefn code_fingerprint(&self) -> Option<String>
fn code_fingerprint(&self) -> Option<String>
Returns a code fingerprint for content-based versioning.
This method should return a hash of the task’s implementation code, enabling automatic detection of changes for Workflow versioning.
The default implementation returns None, indicating that the task
doesn’t support code fingerprinting. Tasks generated by the #[task]
macro automatically provide fingerprints.
§Returns
Some(String)- A hex-encoded hash of the task’s code contentNone- Task doesn’t support code fingerprinting