Overview
Extensions in Serverless Workflow offer a flexible way to extend the functionality of tasks within a workflow. They allow developers to inject custom logic, perform pre- or post-processing tasks, and modify task behavior dynamically based on runtime conditions. With extensions, developers can enhance workflow capabilities, promote code reuse, and maintain consistency across workflows.Use Cases
Extensions can be used to:- Perform logging before and after task execution
- Intercept HTTP calls to mock service responses
- Implement custom error handling or retries
- Apply security checks or data transformations
- Add monitoring and observability hooks
- Implement cross-cutting concerns like authentication or caching
Extension Properties
Extensions are defined with properties that provide precise control over their application:extend
Specifies the type of task to extend. This can be:
all- applies to all tasks- A specific task type (e.g.,
call,set,emit) - A combination of task types
when
Conditionally applies the extension based on runtime expressions. This allows you to selectively enable extensions based on:
- Task properties
- Runtime context
- Input data
- Environment conditions
before
Executes tasks before the extended task runs. These tasks have access to:
- The task’s input
- The workflow context
- Runtime information
after
Executes tasks after the extended task completes. These tasks have access to:
- The task’s output
- The task’s input
- The workflow context
- Runtime information
Basic Example: Logging Extension
Here’s a simple logging extension that logs before and after every task:Advanced Examples
Conditional Extension
Apply an extension only to specific tasks based on conditions:Error Handling Extension
Add custom error handling to all tasks:Authentication Extension
Inject authentication tokens into HTTP calls:Mock Service Extension
Intercept service calls for testing:Best Practices
Keep Extensions Focused
Each extension should have a single, well-defined purpose. This makes them easier to understand, maintain, and reuse across workflows.
Use Conditional Application
Use the
when property to apply extensions only where needed. This improves performance and reduces unnecessary processing.Document Your Extensions
Clearly document what each extension does, when it applies, and any side effects it may have.
Test Extensions Independently
Test extensions in isolation before combining them with complex workflows.
Execution Order
When multiple extensions apply to the same task, they are executed in the order they are defined:- All applicable
beforetasks from each extension (in definition order) - The actual task being extended
- All applicable
aftertasks from each extension (in definition order)
Frequently Asked Questions
Can extensions modify task input or output?
Can extensions modify task input or output?
Yes! Extensions can modify data through the workflow context. Tasks in the
before section can update the context before the main task executes, and tasks in the after section can transform the output or update the context based on the results.Can I disable an extension for specific tasks?
Can I disable an extension for specific tasks?
While you can’t directly disable an extension for a specific task, you can use the
when property with a conditional expression that excludes certain tasks based on their properties or names.What happens if an extension task fails?
What happens if an extension task fails?
If a task in the
before section fails, the main task will not execute. If a task in the after section fails, the workflow will fault. Use error handling mechanisms like try/catch within your extensions to handle failures gracefully.Can extensions call other workflows?
Can extensions call other workflows?
Yes! Extension tasks can use the
run task to execute other workflows, scripts, or containers, giving you complete flexibility in what extensions can do.How do extensions differ from reusable tasks?
How do extensions differ from reusable tasks?
Extensions automatically apply to multiple tasks based on conditions, while reusable tasks must be explicitly called. Extensions are ideal for cross-cutting concerns (logging, monitoring, security), while reusable tasks are better for specific business logic that needs to be repeated.