How to execute tasks in parallel (fork)
Use fork.branches to run independent branches concurrently. Each branch has its own task input. By default, a fork waits for all branches.
Complete the SQLite setup and keep its worker running. Run the commands from a second terminal in the setup directory. With the JAR, substitute java -jar lemline.jar for bin/lemline. The examples call the tutorial shop; start it from the root of the Lemline source checkout:
docker compose -f examples/tutorial-services/compose.yaml up -d
Save this definition as parallel-order-checks.yaml:
document:
dsl: '1.0.3'
namespace: examples
name: parallel-order-checks
version: '1.0.0'
do:
- checkOrder:
fork:
branches:
- checkStock:
call: http
with:
method: get
endpoint: http://127.0.0.1:8787/stock
output:
as: '${ { available: .available } }'
- checkShipping:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/quotes/express
body: ${ . }
output:
as: '${ { checks: . } }'
bin/lemline definition post -f parallel-order-checks.yaml
bin/lemline instance start examples parallel-order-checks --input '{"orderId":"QUOTE-1"}'
The fork returns the results of its branches in the order they are listed: {"checks":[{"available":100},{"orderId":"QUOTE-1","carrier":"express","attempts":1,"price":9}]}. For a complete checkout, compare the two carriers. Each branch returns its own price and min_by(.price) selects the carrier after both branches finish.
Handle errors around a fork
Use a surrounding try task when the workflow needs a fallback for the whole group. Place a separate try inside a branch when that branch can recover independently.
document:
dsl: '1.0.3'
namespace: examples
name: parallel-checks-with-fallback
version: '1.0.0'
do:
- attemptChecks:
try:
- checkOrder:
fork:
branches:
- inventory:
call: http
with:
method: get
endpoint: http://127.0.0.1:8787/stock
query:
fail: '503'
failTimes: ${ .inventoryFailures // 0 | tostring }
key: ${ .orderId }
- shipping:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/quotes/express
body:
orderId: ${ .orderId }
catch:
do:
- requestManualReview:
set:
status: needs-review
The inventoryFailures input makes the shop's stock endpoint answer HTTP 503 that many times. Save the definition as parallel-checks-with-fallback.yaml and run it with a working and a failing inventory:
bin/lemline definition post -f parallel-checks-with-fallback.yaml
bin/lemline instance start examples parallel-checks-with-fallback --input '{"orderId":"QUOTE-2"}'
bin/lemline instance start examples parallel-checks-with-fallback --input '{"orderId":"QUOTE-3","inventoryFailures":1}'
The first instance returns both branch results. In the second, the inventory branch fails, the fork fails with it, and the catch returns {"status":"needs-review"}. The shipping branch still ran: the shop lists an express quote for QUOTE-3.
Wait for outcomes published as events
A listen task can collect events produced by independent services. In this example, all requires one match for each filter and timeout.after bounds the wait.
document:
dsl: '1.0.3'
namespace: examples
name: listen-for-all-results
version: '1.0.0'
do:
- collectResults:
listen:
to:
all:
- with:
type: com.example.inventory.checked
- with:
type: com.example.shipping.quoted
timeout:
after: PT5M
- recordResults:
set:
checks: ${ . }
This example needs the publisher from the tutorial. Save it as tutorial-publisher.yaml:
document:
dsl: '1.0.3'
namespace: examples
name: tutorial-publisher
version: '1.0.0'
do:
- publish:
emit:
event:
with:
type: ${ .type }
source: https://events.example.com/tutorial
data: ${ .data }
Save the listener as listen-for-all-results.yaml, start it, then publish both events:
bin/lemline definition post -f tutorial-publisher.yaml
bin/lemline definition post -f listen-for-all-results.yaml
bin/lemline instance start examples listen-for-all-results
bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.inventory.checked","data":{"stock":2}}'
bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.shipping.quoted","data":{"price":15}}'
After the second event the listener completes with {"checks":[{"stock":2},{"price":15}]}. Stop the shop when you are done:
docker compose -f examples/tutorial-services/compose.yaml down
Use a For Task to iterate over a collection at runtime. See Fan-Out and Fan-In for the difference between iteration, forking tasks and waiting for events.
25 November 2026