The Customer Data Platform for Developers
Website
·
Documentation
·
Community Slack
rudder-workflow-engine
Overview
In the rudder-transformer service, we’re transforming customer event data into the format required for different destinations. To make this process clearer and easier to maintain, we needed to divide it into logical steps. So, we developed this workflow engine to meet our needs.
- Validation
- Event validation
- Destination config validation
- Source to destination data mapping
- Enriching data with destination API calls
- Handling different types of events
- Track, Identify, Page, etc.
- Custom categories:
- Product Viewed
- Product Purchased
- Multiplexing
- Batching
- Response building.
Currently, most steps are implemented using Javascript code, which provides the most flexibility. Still, it is getting difficult to maintain, understand, debug, test, and develop in a standardized way. To bring standardization, we are building a workflow engine that is config driven to provide improved readability, testability, reusability, and speed of development.
Since we want to express the transformation of the logic using easy to read and write template based language. We support following template languages:
templateType: Jsonata
steps:
- name: unsupported
condition: $not(op in ["+", "-", "*", "/"])
template: |
$doThrow("unsupported operation")
- name: add
description: Do addition
condition: op = "+"
template: |
( a + b )
- name: subtract
description: Do subtraction
condition: op = "-"
template: |
( a - b )
- name: multiply
description: Do multiplication
condition: op = "*"
template: |
( a * b )
- name: divide
description: Do division
condition: op = "/"
template: |
(
$assert( b != 0, "division by zero is not allowed");
a / b
)
Getting started
npm install @rudderstack/workflow-engine
import { WorkflowEngineFactory } from '@rudderstack/workflow-engine';
const workflowEngine = WorkflowEngineFactory.createFromFilePath('workflow.yaml', options);
workflowEngine.execute(input);
Features
Config Driven
Users should be able to express the destination transformation logic as a series of steps in a YAML file as a workflow. Steps can be written as template base languages.
Bindings
Supports importing of external functions and data using bindings.
Workflow Bindings
- The platform provides these bindings, which can be used directly without defining them in the bindings block.
- We will soon release detailed documentation on these bindings.
Execution bindings
Steps
- Steps are the main execution blocks of the workflow.
- Steps must contain a name to track outputs.
- Steps can contain an optional description field to describe the details.
- The step can contain an optional condition field to execute only if the condition is satisfied.
- The step can contain an optional inputTemplate field to customize the input, which will be passed while executing the step.
- There are two different types of steps supported:
Conditions
ContextTemplate
OnComplete
-
When the step is completed, the next step will be executed, but if we want to exit the workflow with the output of a particular step, then we can use this.
-
This feature should be used only in a conditional step.
-
Example 1: Avoid reprocessing, so return without modifying the input message.
steps:
- name: checkIfProcessed
condition: message.processed = true
template: |
message
**onComplete: return**
- name: processMessage
template: |
(...)
- In the above example, we don’t want to reprocess messages, so we need to return them immediately if they are already processed.
-
Example 2: Return early after processing the input message.
steps:
- name: step1
template: |
(doSomeProcessing)
- name: **step2**
condition: someCondition
template: |
(doSomeProcessing)
onComplete: return
- name: step3
template: |
(doSomeProcessing)
- In this example, we want to return early after successfully processing the message in step2 since this step is conditional, and if the condition is not satisfied, then step3 will be executed.
OnError
Custom Workflow Executor
When you may want finer control on how workflow steps needs to executed by workflow engine then you can use this feature. You need to implement WorkflowExecutor and use it in following ways to override the default workflow executor.
Specify directly in YAML
executor: myCustomWorkflowExecutor
bindings:
- name: myCustomWorkflowExecutor
path: ./custom_executor
steps:
- name: step1
template: |
doSomething
Refer this example for more details.
Specify using workflow options
WorkflowEngineFactory.createFromFilePath('workflow.yaml', {
executor: myCustomWorkflowExecutor,
});
Custom Bindings Provider
When you want to implement custom logic to resolve the bindings then you can use this feature. You need to implement WorkflowBindingProvider.
WorkflowEngineFactory.createFromFilePath('workflow.yaml', {
bindingProvider: myCustomBindingsProvider,
});
Steps
Simple Step
-
Simple step is the basic unit of execution in the workflow.
-
A simple step can be a function that is defined in the Bindings.
bindings:
- name: **processTrackEvent
path: ./transform # actual file name is transform.js**
steps:
- name: processTrackEvent
functionName: processTrackEvent
- We can omit .js extension while defining the bindings.
- processTrackEvent must have the following definition.
(input: any, bindings: Record<string, any>) => {
error?: any,
output?: any
}
-
A simple step can be a JSONata template.
name: processTrackEvent
template: |
(JSONata template to process track events)**
-
We can use an external workflow in a simple step.
steps:
- name: prepareContext
template: $setContext("batchMode", true)
- name: transform
**externalWorkflow:
path: ./pinterest_tag_single_workflow.yaml**
loopOverInput: true
- We are reusing the single event workflow in the batch events transformation workflow.
- The external workflow will be executed as a black box, so we can only access the final output of the workflow but not the individual outputs of steps.
- The external workflow is executed with step input and context of the original workflow.
- The context of the parent workflow is passed to the child workflow (externalWorkflow) but not vice-versa. This is helpful to customize the child workflow execution based on where it is used.
- The external workflow doesn’t have access to the parent workflow outputs.
Workflow Step
- Series of simple steps.
steps:
- name: category
template: |
(compute category)
- name: ecom
condition: $outputs.category = "ecom"
steps:
- name: validateInput
description: Common validation for all ECom pages
template: |
(assert everything is fine)
- name: page
template: |
(compute page using $outputs.category)
- name: processSearchPage
condition: $outputs.ecom.page = "search"
template: |
(search page template)
- name: processDetailPage
condition: $outputs.ecom.page = "detail"
template: |
(detail page template)
- name: processCartPage
condition: $outputs.ecom.page = "cart"
template: |
(cart page template)
- We can access outputs of previous steps normally like $outputs.category.
- To access outputs of the child steps of the workflowStep, we need to use $outputs.workflowStepName.childStepName, for example: $outputs.ecom.page.
- The outputs of the child steps are not available outside the workflow step.
- The last successfully executed child step’s output will become the output of the workflow step, and we can only access that outside the workflow step as $output.workflowStepName, for example, $output.ecom.
- Currently, we don’t support nested workflow steps.
- Workflow Step can be imported from a file.
steps:
- name: processECommerace
workflowStepPath: ./ecomWorkflow.yaml
- Supports additional Bindings
bindings:
- name: commonBinding
path: ./bindings
steps:
- name: processECommerace
bindings:
- name: stepBinding
path: ./workflow_step_bindings
steps:
- name: validateInput
description: Common validation for all ECom pages
template: |
(assert with $commonBinding)
- name: page
template: |
(compute page using $workflowBinding)
- name: processSearchPage
condition: $outputs.ecom.page = "search"
template: |
(search page template)
- In the above example: processECommerace step is the workflow step and importing additional bindings. Both workflow’s (commonBinding) and step’s (stepBinding) bindings are available to the workflow step.
Batch Step
This helps to batch the inputs using filter and by length or size. We can also define our own batching by implementing BatchExecutor interface.
Syntax
steps:
- name: batchStep
type: batch
batches:
- key: heroes
filter: .type === "hero"
length: 5
- key: villains
filter: .type === "villain"
size: 100
Here we are using keys (heroes or villains) to indicate batches and these will be reflected in the output for further processing.
Custom Batch Executor
Refer this example.
Custom Step
When you want to bring your own steps to workflows then you use this feature.
Using Executor
When your custom step doesn’t require initialization specific to workflow then you can directly a provide executor instance in bindings by implementing CustomStepExecutor.
Refer this example.
Using Provider
When your custom step requires initialization with custom params then you can pass a provider in bindings by implementing CustomStepExecutorProvider.
Refer this example.
Testing
Test Scenarios using Jest
npm run jest:scenarios -- --scenarios=<comma separate scenarios>
- Example:
npm run jest:scenarios -- --scenarios=basic_workflow,to_array
Manually Test Scenario
npm run test:scenario -- -s <scenario_folder> -i <test_case_index_from_data.json>
- Example:
npm run test:scenario -- -s outputs -i 1
- Note: It just run the test case and produces results but won’t run any validations of the results.
Contribute
We would love to see you contribute to RudderStack. Get more information on how to contribute here.
License
The RudderStack rudder-workflow-engine is released under the MIT License.
评论