Skip to main content

Task

Trait Task 

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

  1. Input: Receive a context containing data from previous tasks
  2. Processing: Execute the task’s business logic
  3. Output: Update the context with results
  4. Completion: Return success or failure
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§

Source

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 results
  • Err(TaskError) - If the task execution fails
Source

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.

Source

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§

Source

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 succeeds
  • Err(CheckpointError) - If checkpointing fails
Source

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.

Source

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.

Source

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 content
  • None - Task doesn’t support code fingerprinting

Implementors§