Lemline Basic Workflow Patterns
This document provides examples of fundamental workflow patterns implemented in the Open Workflow DSL as supported by Lemline. These basic patterns serve as building blocks for creating more complex workflows.
Run the Examples
Each example below is a complete workflow. Complete the SQLite setup and run the commands from 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:
The set example is started by an event, and the for loop example waits for events. Both need the tutorial publisher, saved as tutorial-publisher.yaml:
A workflow started by an event must be registered before the worker starts. Stop the setup worker with Ctrl+C, save the set example below as set-example.yaml, then register both and start the worker again:
Run the other commands from a second terminal in the setup directory. The worker logs Workflow completed with output with each result.
Sequential Task Execution
The simplest workflow pattern is a sequence of tasks executed one after another. This is achieved using the do construct.
Single Task Example
Save it as single-task-example.yaml:
Expect {"sku":"SKU-1","available":100,"reserved":0} on a fresh shop.
A single GET returns the stock of the tutorial shop.
Multiple Sequential Tasks Example
Save it as multiple-sequential-tasks-example.yaml:
Expect {"available":100}.
The first task reads the stock. The second returns the stock level as available.
Nested Task Execution
Tasks can be nested to group related operations and create hierarchical structures.
Nested Tasks Example
Save it as nested-tasks-example.yaml:
Expect {"message":"SKU-1 available: 100"}.
The nested task reads the stock and extracts the level. The final task formats the count as a message.
Nesting is useful for:
Grouping related operations
Localizing variable scope
Creating reusable task patterns
Conditional Logic with Switch
The switch construct allows workflows to implement conditional logic, executing different tasks based on data conditions.
Switch Example
Save it as switch-example.yaml:
Expect {"validate":true,"status":"fulfilled"}, then {"inventory":"clear","items":1,"address":"12 Main St"}, then {"log":"warn","message":"Unknown order type"}.
This example demonstrates:
A switch statement evaluating
.orderTypeThree different paths based on the condition
Each path setting different variables and potentially ending the workflow
A default case for handling unknown types
Key aspects of switch:
Multiple conditions can be evaluated in sequence
The first matching condition is executed
The
defaultcase handles situations where no conditions matchthencan direct flow to other named tasks in the workflow
Loop Patterns
Workflows often need to iterate over collections of data or repeat tasks until certain conditions are met.
For Loop Example
Save it as for-loop-example.yaml:
The instance waits for the first item. Publish two packing events, the second marked as the last item:
Wait about 10 seconds between the two events: the second iteration starts listening only after the first event has been consumed, and an event resumes only the instances already waiting for it. The while condition is checked before each item; after the second event, stop is true, so the loop ends without waiting for SKU-3. Expect {"lastPacked":"SKU-2","position":1,"stop":true}.
This example demonstrates:
Iterating over a collection (
$workflow.input.items)The current item is accessible as
$itemThe index is accessible as
$indexA condition (
while) is checked before each iteration and can end the loop earlyInside the loop, an event listener waits for a specific event
Each iteration's result is the next iteration's input
Key aspects of for:
Iterates over arrays or objects
Can track iteration index with the
atpropertyCan include additional conditions with
whileContains a
doblock with tasks to execute for each iteration
Data Manipulation with Set
The set construct allows workflows to create or modify variables.
Set Example
You registered it before starting the worker. Publish its event:
About 10 seconds later, a started-by-event instance completes with {"startEvent":{"orderId":"ORD-1004"}}: the worker picks up events for event-started workflows every 10 seconds, and the workflow input is the list of events that started it.
This example demonstrates:
Setting a variable
startEventto the first element of the workflow inputThe workflow is triggered by an event
Additional set capabilities:
Set multiple variables in a single step
Use JQ expressions for data transformation
Reference existing variables and workflow context
Advanced Set Example
Save it as advanced-set-example.yaml:
Expect enrichedUser with the user fields, lastAccess set to the timestamp as a number of seconds, isAdmin: true and permissionCount: 2.
This advanced example shows:
Setting nested object structures
Using the JQ expression language for data manipulation
Combining and transforming existing variables
Using built-in functions like
now()andlength()
Combining Patterns
These basic patterns can be combined to create more complex workflows.
Combined Example
Save it as combined-example.yaml:
Each item is routed by type, then logged in the shop's audit list: E-book processed, download ready, Notebook processed, shipping required and Software processed, download ready. Stop the shop when you are done:
This complex example demonstrates:
Initializing a data structure with
setIterating over the data with
forUsing
switchfor conditional processing based on item typeNesting
doblocks within conditionsTransforming data based on conditions
Using the transformed data in subsequent tasks
Best Practices
When implementing these basic patterns:
Descriptive task names: Use clear, meaningful names for tasks to make workflows self-documenting
Appropriate granularity: Break complex tasks into manageable steps, but avoid excessive fragmentation
Consistent data structure: Maintain a predictable data structure throughout the workflow
Error handling: Consider adding try-catch blocks for operations that may fail
Data validation: Validate inputs early in the workflow
Modular design: Group related tasks for better organization and potential reuse
Conclusion
These basic workflow patterns form the foundation for building complex business processes in Lemline. By understanding and combining these patterns, you can create workflows that are:
Easy to understand
Maintainable
Adaptable to changing requirements
Capable of handling complex business logic
For examples integrating with external services, see the Integration Examples document.