Task Types¶
YAML Workflow provides several types of tasks that can be used in your workflows. Each task type serves a specific purpose and has its own set of parameters and capabilities.
Common Task Properties¶
Every task in YAML Workflow supports these properties:
steps:
- name: task_name # Required: Unique name for the task
task: task_type # Required: Type of task to execute
description: string # Optional: Description of what the task does
params: {} # Required: Task-specific parameters
retry: # Optional: Retry configuration
max_attempts: int # Number of retry attempts
delay: int # Delay between retries in seconds
backoff: float # Exponential backoff multiplier
on_error: # Optional: Error handling configuration
action: string # One of: fail, continue, retry
message: string # Custom error message
next: string # Next task to execute on error
timeout: int # Optional: Task timeout in seconds
depends_on: [] # Optional: List of task dependencies
Basic Tasks¶
Simple utility tasks for common operations:
-
echo
: Print a message to the console -
fail
: Deliberately fail a workflow (useful for testing) -
add_numbers
: Add two numbers together -
join_strings
: Concatenate strings
File Operations¶
Tasks for working with files and directories:
-
write_file
: Write content to a file -
read_file
: Read content from a file -
append_file
: Append content to a file copy_file
: Copy a file to another locationmove_file
: Move/rename a filedelete_file
: Delete a file
Example:
steps:
- name: write_config
task: write_file
inputs:
file_path: config.json
content: |
{
"setting": "value"
}
- name: read_data
task: read_file
inputs:
file_path: data.txt
outputs: file_content
Shell Commands¶
Execute shell commands and scripts:
shell
: Run shell commands with variable substitutiontask: shell inputs: command: string # Required: Command to execute cwd: string # Optional: Working directory env: object # Optional: Additional environment variables shell: string # Optional: Shell to use (default: /bin/sh) outputs: stdout: string # Command standard output stderr: string # Command standard error exit_code: int # Command exit code
Template Processing¶
Tasks for template rendering and text processing:
template
: Render a template with variable substitution
Example:
steps:
- name: generate_report
task: template
template: |
# Report for {{ date }}
Total records: {{ count }}
Status: {{ status }}
output: report.md
Python Integration¶
Execute Python code within workflows:
python_code
: Run Python code with access to workflow context
Batch Processing¶
Process data in batches:
batch
: Process items in batches with configurable size and parallelismtask: batch inputs: items: [any] # Required: List of items to process batch_size: int # Optional: Items per batch (default: 10) parallel: bool # Optional: Process in parallel (default: false) max_workers: int # Optional: Max parallel workers (default: 4) task: # Required: Task configuration to run for each item type: string # Task type to execute inputs: object # Task inputs (item available as {{ item }}) outputs: results: [any] # List of task results failed: [any] # List of failed items
Task Features¶
All tasks support these common features:
- Variable Substitution
- Use
{{ variable }}
syntax to reference variables -
Access step outputs, environment variables, and parameters
-
Output Capture
- Store task results in variables
-
Use outputs in subsequent steps
-
Error Handling
- Configure retry behavior
- Define error handling steps
-
Set custom error messages
-
Conditional Execution
- Run tasks based on conditions
- Skip tasks when conditions aren't met
See the specific task documentation for detailed parameter lists and usage examples.
Data Processing¶
write_json
¶
Writes data as JSON to a file.
- name: save_json
task: write_json
params:
file_path: "output/data.json"
data: "{{ process_result }}"
indent: 2
write_yaml
¶
Writes data as YAML to a file.
- name: save_yaml
task: write_yaml
params:
file_path: "output/config.yaml"
data: "{{ config_data }}"
Creating Custom Tasks¶
You can create custom tasks by:
- Creating a Python class that inherits from
BaseTask
- Implementing the required methods
- Registering the task with the engine
Example:
from yaml_workflow.tasks import BaseTask
class CustomTask(BaseTask):
def run(self, params):
# Task implementation
pass
# Register the task
register_task("custom_task", CustomTask)
Error Handling¶
Tasks can be configured to handle errors in different ways:
-
Retry Configuration
-
Error Actions
-
Conditional Execution
Task Dependencies¶
Tasks can specify dependencies using the depends_on
property:
steps:
- name: first_task
task: echo
inputs:
message: "First"
- name: second_task
task: echo
inputs:
message: "Second"
depends_on: [first_task]
- name: parallel_task
task: echo
inputs:
message: "Can run in parallel with second_task"
depends_on: [first_task]
Dependencies can be:
- Single task: depends_on: task_name
- Multiple tasks: depends_on: [task1, task2]
- Conditional: depends_on: "{{ success_of_task }}"
Examples¶
Basic File Processing¶
steps:
- name: read_input
task: read_file
inputs:
file: "input.txt"
- name: process_content
task: python_code # Updated
inputs:
code: |
content = steps.read_input.result.content
# ... process content ...
result = processed_content
- Shell Tasks: Execute shell commands.
- Python Tasks: Execute Python code, functions, scripts, or modules.
- Batch Tasks: Process items in batches.