Interceptors provide cross-cutting functionality for Temporal workflows and activities in the Application SDK. They automatically handle concerns like event tracking, distributed locking, and resource cleanup without requiring changes to your workflow or activity code.
Interceptors are registered with the Temporal worker and automatically applied to all workflows and activities. They follow the Temporal interceptor pattern and can be composed together to provide multiple capabilities.
Interceptor implementationsโ
The Application SDK provides three concrete interceptor implementations, each optimized for different cross-cutting concerns. All interceptors follow the Temporal interceptor pattern and can be composed together to provide comprehensive functionality for workflows and activities.
EventInterceptor
MonitoringTracks workflow and activity execution events for monitoring and observability. Automatically publishes events when workflows and activities start and end.
RedisLockInterceptor
Distributed LockingManages distributed locks for activities using Redis. Limits concurrent execution to a specified number of activities with the same lock name.
CleanupInterceptor
Resource ManagementAutomatically cleans up temporary artifacts and activity state when workflows complete or fail. Supports multiple cleanup base paths and comprehensive error handling.
Combining interceptorsโ
Multiple interceptors can be combined to provide comprehensive functionality:
from application_sdk.interceptors import (
EventInterceptor,
RedisLockInterceptor,
CleanupInterceptor
)
from temporalio.worker import Worker
worker = Worker(
client,
task_queue="my-task-queue",
workflows=[MyWorkflow],
activities=[
my_activity,
acquire_distributed_lock,
release_distributed_lock,
cleanup
],
interceptors=[
EventInterceptor(),
RedisLockInterceptor(activities={
"my_activity": my_activity
}),
CleanupInterceptor()
]
)
Execution order: Interceptors wrap each other in registration order, with each interceptor wrapping the next in the chain. The first interceptor in the list wraps the second, which wraps the third, continuing in this pattern. This means the last interceptor in the list is the innermost wrapper, executing closest to the workflow or activity.
See alsoโ
- Distributed locking: Concepts and usage of distributed locking for activities
- Handle events: Event handling patterns in workflows
- Workflows: Workflow orchestration reference
- StateStore: Persistent state management for workflows and credentials