Lemline documentation Help

Timeout Handling Examples

This document provides examples of implementing effective timeout strategies in Lemline workflows. Proper timeout handling is essential for building robust workflows that gracefully handle slow or unresponsive services, preventing workflows from hanging indefinitely and ensuring predictable execution times.

Run the Examples

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. Start the tutorial shop from the root of the Lemline source checkout; its delayMs query parameter makes an answer slow:

docker compose -f examples/tutorial-services/compose.yaml up -d

The event examples also need the tutorial publisher. Save it as tutorial-publisher.yaml and register it:

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 }
bin/lemline definition post -f tutorial-publisher.yaml

Basic Timeout Handling

HTTP Failure Fallback

This example returns the error from a failed HTTP request, including a failure from the HTTP client's own timeout.

document: dsl: '1.0.3' namespace: examples name: http-error-fallback version: '1.0.0' do: - callExternalService: try: - fetchData: call: http with: method: get endpoint: http://127.0.0.1:8787/stock query: delayMs: ${ .delayMs // 0 | tostring } catch: as: requestError do: - returnFailure: set: success: false reason: External HTTP request failed errorType: ${ $requestError.type } title: ${ $requestError.title }

Save it as http-request-with-timeout.yaml:

bin/lemline definition post -f http-request-with-timeout.yaml bin/lemline instance start examples http-error-fallback --input '{"delayMs":0}' bin/lemline instance start examples http-error-fallback --input '{"delayMs":40000}'

The first instance returns the stock. In the second, the shop waits 40 seconds; the HTTP client gives up after 30 and the catch returns success: false with the communication error titled Socket timeout has expired ….

Graduated Timeouts

This example shows graduated timeouts for a cache, a database and a computed fallback. Lemline accepts the timeouts but does not apply them; the fallbacks run when a call fails.

document: dsl: '1.0.3' namespace: examples name: graduated-timeouts version: '1.0.0' do: - tryCache: try: - lookupCache: call: http with: method: get endpoint: '${ "http://127.0.0.1:8787/products/\(.itemId)" + (if .cacheMiss then "?fail=404" else "" end) }' timeout: after: milliseconds: 500 output: as: '${ . + { source: "cache" } }' catch: errors: with: type: https://open-workflow-specification.org/spec/1.0.0/errors/communication status: 404 do: - lookupDatabase: try: - queryDatabase: call: http with: method: get endpoint: '${ "http://127.0.0.1:8787/products/\($workflow.input.itemId)?delayMs=\($workflow.input.databaseDelayMs // 0)" }' timeout: after: seconds: 3 output: as: '${ . + { source: "database" } }' catch: do: - computeFallback: set: id: ${ $workflow.input.itemId } source: fallback

Save it as graduated-timeouts.yaml:

bin/lemline definition post -f graduated-timeouts.yaml bin/lemline instance start examples graduated-timeouts --input '{"itemId":"SKU-2"}' bin/lemline instance start examples graduated-timeouts --input '{"itemId":"SKU-2","cacheMiss":true}' bin/lemline instance start examples graduated-timeouts --input '{"itemId":"SKU-2","cacheMiss":true,"databaseDelayMs":4000}'

The first instance answers from the cache (source: cache), the second from the database after a cache miss (source: database). In the third, the database takes 4 seconds: the 3-second timeout is not applied, so the call waits and still answers with source: database. Only the failure of a call, not its duration, reaches the next fallback.

Timeouts with Event Waiting

These examples receive events from the tutorial publisher on the worker's SQLite channel.

Event Timeout

This workflow waits up to 30 seconds for a specific process to complete; a real process would get minutes. The producer stores each status before emitting the event. On timeout, the workflow reads the stored status so it can still recover a completion sent before the listener was registered.

document: dsl: '1.0.3' namespace: examples name: event-timeout version: '1.0.0' input: schema: document: type: object required: [processId] properties: processId: type: string do: - awaitCompletion: try: - waitForCompletion: listen: to: one: with: type: com.example.process.completed correlate: processId: from: ${ .processId } expect: ${ $workflow.input.processId } timeout: after: seconds: 30 - processResult: set: success: true processId: ${ $workflow.input.processId } data: ${ .[0] } catch: errors: with: type: https://open-workflow-specification.org/spec/1.0.0/errors/timeout do: - readDurableStatus: call: http with: method: get endpoint: '${ "http://127.0.0.1:8787/reservations/\($workflow.input.processId | @uri)" }' - summarizeStatus: set: success: ${ .status == "reserved" } status: ${ .status } processId: ${ $workflow.input.processId }

Save it as event-timeout.yaml:

bin/lemline definition post -f event-timeout.yaml bin/lemline instance start examples event-timeout --input '{"processId":"ORD-1401"}'

Publish the completion while it waits:

bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.process.completed","data":{"processId":"ORD-1401","result":"shipped"}}'

The instance completes with success: true and the event data. To see the timeout path, reserve an order in the shop, which stands for the durable status store, and start a listener for it without publishing anything:

curl -X POST http://127.0.0.1:8787/reservations -d '{"orderId":"ORD-1402","items":[{"id":"SKU-1","quantity":1}]}' bin/lemline instance start examples event-timeout --input '{"processId":"ORD-1402"}'

After 30 to 40 seconds the listener times out, the workflow reads the stored reservation, and completes with {"success":true,"status":"reserved","processId":"ORD-1402"}.

Multiple Event Timeout

This example demonstrates waiting for multiple events with a timeout.

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: ${ . }

Save it as listen-for-all-results.yaml:

bin/lemline definition post -f listen-for-all-results.yaml bin/lemline instance start examples listen-for-all-results

Publish both results while it waits:

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}}'

Expect {"checks":[{"stock":2},{"price":15}]}. Without both events, the listener would fault with a timeout error after five minutes.

Workflow-Level Timeouts

Deadline Between Tasks

This example checks a deadline before starting the final task. A task already in progress continues until it returns or reaches its own timeout.

document: dsl: '1.0.3' namespace: examples name: deadline-between-tasks version: '1.0.0' do: - recordDeadline: set: deadline: ${ now + ($workflow.input.deadlineSeconds // 1800) } export: as: '${ $context + { deadline: .deadline } }' - prepareResults: call: http with: method: get endpoint: http://127.0.0.1:8787/stock query: delayMs: '2000' - checkDeadline: switch: - expired: when: ${ now >= $context.deadline } then: returnExpired - timeRemaining: then: finish - returnExpired: set: success: false reason: Deadline reached before the final step then: end - finish: call: http with: method: post endpoint: http://127.0.0.1:8787/audit body: reference: ${ $workflow.input.orderId } message: '${ "Finished with \(.available) units available" }'

Save it as global-workflow-timeout.yaml:

bin/lemline definition post -f global-workflow-timeout.yaml bin/lemline instance start examples deadline-between-tasks --input '{"orderId":"ORD-1403"}' bin/lemline instance start examples deadline-between-tasks --input '{"orderId":"ORD-1404","deadlineSeconds":1}'

The first instance has 30 minutes and finishes: the shop's audit list shows Finished with 99 units available for ORD-1403: the ORD-1402 reservation above holds one unit. The second gives itself one second; the 2-second preparation uses it up, so the workflow returns {"success":false,"reason":"Deadline reached before the final step"} without calling the final step.

Timeout Strategies for Different Services

Database Query Timeouts

This example shows timeout settings for database-style queries, here the shop's catalog. Lemline accepts them but does not apply them.

document: dsl: '1.0.3' namespace: examples name: database-timeouts version: '1.0.0' do: - simpleQuery: try: - executeQuery: call: http with: method: get endpoint: http://127.0.0.1:8787/products query: size: '10' timeout: after: seconds: 3 export: as: '${ $context + { queryResult: .total } }' catch: errors: with: type: https://open-workflow-specification.org/spec/1.0.0/errors/timeout retry: delay: milliseconds: 500 limit: attempt: count: 2 - complexQuery: try: - executeComplexQuery: call: http with: method: get endpoint: http://127.0.0.1:8787/products query: category: garden search: hose delayMs: ${ $workflow.input.slowQueryMs // 0 | tostring } timeout: after: seconds: 1 export: as: '${ $context + { complexResult: [.items[].id] } }' catch: errors: with: type: https://open-workflow-specification.org/spec/1.0.0/errors/timeout do: - simplifyAndRetry: call: http with: method: get endpoint: http://127.0.0.1:8787/products query: category: garden export: as: '${ $context + { usedSimplifiedQuery: true, simplifiedResult: [.items[].id] } }' - summarize: set: results: ${ $context }

Save it as database-query-timeouts.yaml:

bin/lemline definition post -f database-query-timeouts.yaml bin/lemline instance start examples database-timeouts --input '{}' bin/lemline instance start examples database-timeouts --input '{"slowQueryMs":2000}'

Both instances complete with queryResult: 7 and complexResult: ["SKU-1"]. In the second, the complex query takes 2 seconds: its 1-second timeout is not applied, so the fallback query does not run.

External API Timeouts

This example declares different timeouts for different types of external APIs. Lemline accepts them but does not apply them.

document: dsl: '1.0.3' namespace: examples name: api-timeouts version: '1.0.0' do: - callReliableApi: call: http with: method: get endpoint: http://127.0.0.1:8787/stock timeout: after: seconds: 2 export: as: '${ $context + { stock: .available } }' - callThirdPartyApi: try: - getProduct: call: http with: method: get endpoint: http://127.0.0.1:8787/products/SKU-4 query: delayMs: ${ $workflow.input.thirdPartyDelayMs // 0 | tostring } timeout: after: seconds: 5 export: as: '${ $context + { product: .name, source: "live" } }' catch: do: - useCachedProduct: set: cached: true export: as: '${ $context + { product: "Desk lamp", source: "cache" } }' - summarize: set: results: ${ $context }

Save it as external-api-timeouts.yaml:

bin/lemline definition post -f external-api-timeouts.yaml bin/lemline instance start examples api-timeouts --input '{}' bin/lemline instance start examples api-timeouts --input '{"thirdPartyDelayMs":6000}'

Both instances complete with the live product Desk lamp and source: live: the second waits 6 seconds for the shop, beyond its 5-second timeout. The cached fallback only runs if the call fails, for example after the 30-second client limit. Stop the shop when you are done:

docker compose -f examples/tutorial-services/compose.yaml down

Best Practices for Timeout Handling

When implementing timeout strategies in Lemline workflows, consider these best practices:

  1. Appropriate Duration: Set timeout durations appropriate to the operation being performed (Lemline only enforces timeouts on listen tasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)

  2. Graduated Timeouts: Use shorter timeouts for quick operations, longer for complex ones (Lemline only enforces timeouts on listen tasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)

  3. Retry Strategy: Combine timeouts with appropriate retry strategies

  4. Fallback Logic: Implement fallback mechanisms when operations timeout

  5. Monitoring: Log and monitor timeout occurrences to identify patterns

  6. Circuit Breaking: Consider implementing circuit breakers for frequently timing out services

  7. Global Limits: Set workflow-level timeouts to prevent indefinite execution (Lemline only enforces timeouts on listen tasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)

  8. Compensating Actions: Implement cleanup actions when timeouts occur mid-process

  9. Timeout Prediction: Monitor remaining time and adjust behavior as deadlines approach

  10. Documentation: Document expected timeouts for each integration point

Conclusion

These examples demonstrate various approaches to implementing effective timeout handling in Lemline workflows. By combining appropriate timeout durations with retry strategies, fallback mechanisms, and cleanup actions, you can build resilient workflows that handle external service failures gracefully.

Proper timeout handling ensures that workflows remain responsive even when dependent services are slow or unresponsive, improving overall system reliability and user experience.

For more examples of error handling and resilience patterns, see the Error Handling Examples document.

25 November 2026