Skip to content

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.

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 name
  • tenant (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 to name.

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

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")

Add a tag to the workflow for metadata and organization.

Parameters:

  • key (str): Tag key
  • value (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 responsible
  • environment: dev, staging, production
  • priority: low, medium, high, critical
  • schedule: daily, weekly, monthly, on-demand
  • category: etl, reporting, monitoring, cleanup

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 reference
builder = cloaca.WorkflowBuilder("etl_workflow")
builder.add_task(extract_data)
builder.add_task(transform_data)

Task Resolution:

  • String: Must match the id parameter of a @cloaca.task decorated function
  • Function: Must be a @cloaca.task decorated function

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 as ValueError)

Example:

builder = cloaca.WorkflowBuilder("my_workflow")
builder.description("Sample workflow")
builder.add_task("task_1")
builder.add_task("task_2")
# Build and validate
workflow = builder.build()

Validation Checks:

  • All referenced tasks exist
  • No circular dependencies
  • All dependencies are resolvable
  • Workflow has at least one task

WorkflowBuilder supports context manager protocol for automatic registration.

Example:

import cloaca
@cloaca.task()
def hello_task(context):
context.set("message", "Hello, World!")
return context
# Automatic registration
with 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 immediately
runner = cloaca.DefaultRunner("sqlite:///app.db")
context = cloaca.Context()
result = runner.execute("hello_workflow", context)

For worked examples, dynamic and conditional construction, validation and debugging, error-handling patterns, and best practices, see Build Workflows with WorkflowBuilder.