Skip to main content

Loop

Type: loop • Category: flow • Tags: loop, iteration, control, flow, cycle

Description

Execute iterations with continue/exit branches

Parameters

NameTypeDescriptionRequiredDefault
max_iterationsnumberMaximum number of iterationsno10
exit_conditionstringExpression for early exit (optional, e.g., 'vars.counter > 5')no
check_afterbooleanIf true, evaluate exit_condition after running the loop body (continue branch)nofalse
loop_varstringVariable name to store iteration counter in workflow varsno"loop_iteration"

Help

Loop Worker

This worker implements loop control flow with two output branches for workflow routing.

How it works:

  1. Initialize: Set maximum iterations and optional exit condition
  2. Check conditions: Evaluate exit condition (pre- or post-body based on check_after) and iteration count
  3. Route execution:
    • continue branch: When loop should proceed to next iteration (loops back)
    • exit branch: When loop should terminate (exits loop)

Setting up the Loop:

  1. Connect previous workers → to Loop worker (entry point)
  2. Connect Loop worker "continue" → back to the beginning of loop tasks
  3. Connect Loop worker "exit" → to post-loop processing

Workflow Structure:

[Entry Tasks] → [Loop Worker] → continue → [Loop Tasks] → [Loop Worker]
↓ exit
[Exit Tasks]

Parameters:

  • max_iterations: Maximum number of loop iterations (default: 10)
  • exit_condition: Expression for early exit (optional, e.g., 'vars.counter > 5')
  • check_after: If true, evaluate exit_condition after running the loop body (default: false)
  • loop_var: Variable name to store iteration counter (default: "loop_iteration")

Branch Logic:

  • continue: Returns {"action": "continue", "next_iteration": N+1} - loops back
  • exit: Returns {"action": "exit", "exit_reason": "..."} - exits loop

Example: Create a loop that processes items until condition met:

  1. Set max_iterations: 100
  2. Set exit_condition: 'vars.items_processed >= vars.total_items'
  3. Connect continue → back to data processing workers
  4. Connect exit → to final reporting worker

The workflow will cycle through the loop tasks until exit condition is met or max iterations reached.

Notes:

  • exit_condition supports template braces: "{{ expr }}" (the braces are stripped before evaluation).
  • The expression must evaluate to a boolean; prefer comparisons (>, >=, ==) rather than returning strings.
  • Use safe accessors to avoid KeyError, e.g., vars.get('redis_n_ijnr', {}).get('result', 0) > 100.
  • Use check_after=true to evaluate the condition after executing the continue branch (useful when the loop body computes the condition).

Examples:

  • Pre-check (default): exit when counter reached total exit_condition: vars.get('items_processed', 0) >= vars.get('total_items', 0)
  • Post-check: exit when Redis counter exceeds 100 after body exit_condition: vars.get('redis_n_ijnr', {}).get('result', 0) > 100 check_after: true