Module task

Module task 

Source
Expand description

§Task API - Simplified Workflow Interface

This module provides the Task trait, which offers a simplified interface for workflow processing. A Task only requires implementing a single run method, giving you direct control over the execution flow.

§Key Differences

  • Task: Simple interface with a single run method. It’s great for straightforward operations and quick prototyping.
  • crate::node::Node: A more structured interface with a three-phase lifecycle (prep, exec, post). It’s ideal for complex operations where separating concerns is beneficial.

Both Task and Node support retry strategies.

§Relationship & Compatibility

Every crate::node::Node automatically implements Task through a blanket implementation. This means:

  • You can use any existing Node wherever a Task is expected.
  • Workflows can register both Tasks and Nodes using the same register() method.
  • This provides a seamless migration path from a simple Task to a more structured Node if complexity increases.

§When to Use Which

  • Use Task for simplicity and direct control.
  • Use crate::node::Node for complex logic that benefits from a structured, multi-phase approach.

§Example

use cano::prelude::*;

// Simple Task implementation
struct SimpleTask;

#[async_trait]
impl Task<String> for SimpleTask {
    async fn run(&self, store: &MemoryStore) -> Result<TaskResult<String>, CanoError> {
        // Do all your work here - load, process, store
        let input: String = store.get("input")?;
        let result = format!("processed: {input}");
        store.put("result", result)?;
        Ok(TaskResult::Single("next_state".to_string()))
    }
}

§Interoperability

Every crate::node::Node automatically implements Task, so you can use existing nodes wherever tasks are expected. This provides a smooth upgrade path and backward compatibility.

Structs§

TaskConfig
Task configuration for retry behavior and parameters

Enums§

RetryMode
Retry modes for task execution
TaskResult
Result type for task execution that supports both single and split transitions

Traits§

DynTask
Concrete task trait object with default types
Task
Task trait for simplified workflow processing

Functions§

run_with_retries
Default implementation for retry logic that can be used by any task

Type Aliases§

DefaultTaskParams
Simple key-value parameters for task configuration
TaskObject
Type alias for trait objects