WorkflowBuilder
The WorkflowBuilder class provides a builder pattern for constructing workflows. It allows you to add tasks, set descriptions, configure dependencies, and build executable workflow objects.
This page documents patterns 1 and 2 (in-process). For pattern 3, see the packaging guides.
Constructor
Section titled “Constructor”WorkflowBuilder(name, *, tenant=None, package=None, workflow=None)
Section titled “WorkflowBuilder(name, *, tenant=None, package=None, workflow=None)”Create a new workflow builder.
Parameters:
name(str): Unique workflow nametenant(str, optional, keyword-only): Tenant id for the task namespace. Defaults to"public".package(str, optional, keyword-only): Package name for the task namespace. Defaults to"embedded".workflow(str, optional, keyword-only): Workflow id for the task namespace. Defaults toname.
The keyword arguments set the namespace context in which @cloaca.task
functions register while the builder is active; the defaults are right for
in-process use.
Example:
import cloaca
builder = cloaca.WorkflowBuilder("data_processing_workflow")Naming Rules:
- Must be unique within your application
- Recommended: Use snake_case or kebab-case
- Avoid spaces and special characters
- Should be descriptive of the workflow’s purpose
Configuration Methods
Section titled “Configuration Methods”description(description)
Section titled “description(description)”Set the workflow description.
Parameters:
description(str): Human-readable description of the workflow
Example:
builder = cloaca.WorkflowBuilder("etl_pipeline")builder.description("Extract data from API, transform format, and load to database")tag(key, value)
Section titled “tag(key, value)”Add a tag to the workflow for metadata and organization.
Parameters:
key(str): Tag keyvalue(str): Tag value
Example:
builder = cloaca.WorkflowBuilder("daily_report")builder.description("Generate daily sales report")builder.tag("department", "sales")builder.tag("frequency", "daily")builder.tag("priority", "high")Common Tag Patterns:
department: Team or department responsibleenvironment: dev, staging, productionpriority: low, medium, high, criticalschedule: daily, weekly, monthly, on-demandcategory: etl, reporting, monitoring, cleanup
Task Management
Section titled “Task Management”add_task(task)
Section titled “add_task(task)”Add a task to the workflow.
Parameters:
task(str or callable): Task ID string or task function reference
Example:
# Method 1: Add by task ID (string)@cloaca.task()def extract_data(context): return context
@cloaca.task(dependencies=["extract_data"])def transform_data(context): return context
builder = cloaca.WorkflowBuilder("etl_workflow")builder.add_task("extract_data")builder.add_task("transform_data")
# Method 2: Add by function referencebuilder = cloaca.WorkflowBuilder("etl_workflow")builder.add_task(extract_data)builder.add_task(transform_data)Task Resolution:
- String: Must match the
idparameter of a@cloaca.taskdecorated function - Function: Must be a
@cloaca.taskdecorated function
Building Workflows
Section titled “Building Workflows”build()
Section titled “build()”Build the workflow and validate its structure.
Returns: Workflow object ready for execution
Raises:
ValueError: if the workflow has structural problems or references a task that doesn’t exist (all builder failures surface asValueError)
Example:
builder = cloaca.WorkflowBuilder("my_workflow")builder.description("Sample workflow")builder.add_task("task_1")builder.add_task("task_2")
# Build and validateworkflow = builder.build()Validation Checks:
- All referenced tasks exist
- No circular dependencies
- All dependencies are resolvable
- Workflow has at least one task
Context Manager Support
Section titled “Context Manager Support”WorkflowBuilder supports context manager protocol for automatic registration.
with WorkflowBuilder(...) as builder:
Section titled “with WorkflowBuilder(...) as builder:”Example:
import cloaca
@cloaca.task()def hello_task(context): context.set("message", "Hello, World!") return context
# Automatic registrationwith cloaca.WorkflowBuilder("hello_workflow") as builder: builder.description("Simple hello world workflow") builder.add_task("hello_task") # Workflow automatically registered when exiting context
# Can execute immediatelyrunner = cloaca.DefaultRunner("sqlite:///app.db")context = cloaca.Context()result = runner.execute("hello_workflow", context)Task-Oriented Guidance
Section titled “Task-Oriented Guidance”For worked examples, dynamic and conditional construction, validation and debugging, error-handling patterns, and best practices, see Build Workflows with WorkflowBuilder.
Related Classes
Section titled “Related Classes”- Context - Data passed through workflows
- DefaultRunner - Executes built workflows
- Task Decorator - Defines tasks added to workflows
- Workflow - Built workflow objects