Skip to content

Commit c4fb094

Browse files
authored
Update README with middleware documentation (coinbase#288)
* Update README * Add heading
1 parent 65dfdb0 commit c4fb094

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,36 @@ arguments are identical to the `Temporal.start_workflow` API.
400400
set it to allow as many invocations as you need. You can also set it to `nil`, which will use a
401401
default value of 10 years.*
402402

403+
## Middleware
404+
Middleware sits between the execution of your workflows/activities and the Temporal SDK, allowing you to insert custom code before or after the execution.
405+
406+
### Activity Middleware Stack
407+
Middleware added to the activity middleware stack will be executed around each activity method. This is useful when you want to perform a certain task before and/or after each activity execution, such as logging, error handling, or measuring execution time.
408+
409+
### Workflow Middleware Stack
410+
There are actually two types of workflow middleware in Temporal Ruby SDK:
411+
412+
*Workflow Middleware*: This middleware is executed around each entire workflow. This is similar to activity middleware, but for workflows.
413+
414+
*Workflow Task Middleware*: This middleware is executed around each workflow task, of which there will be many for each workflow.
415+
416+
### Example
417+
To add a middleware, you need to define a class that responds to the call method. Within the call method, you should call yield to allow the next middleware in the stack (or the workflow/activity method itself if there are no more middlewares) to execute. Here's an example:
418+
419+
```
420+
class MyMiddleware
421+
def call(metadata)
422+
puts "Before execution"
423+
yield
424+
puts "After execution"
425+
result
426+
end
427+
end
428+
```
429+
430+
You can add this middleware to the stack like so `worker.add_activity_middleware(MyMiddleware)`
431+
432+
Please note that the order of middleware in the stack matters. The middleware that is added last will be the first one to execute. In the example above, MyMiddleware will execute before any other middleware in the stack.
403433

404434
## Breaking Changes
405435

0 commit comments

Comments
 (0)