Retention
This section explains the Retention feature and how it can be used to remove completed workflow instances automatically.
Configuration
To get started with the retention module, you must enable the retention feature.
The SweepInterval
determines how often the retention feature will check for workflows that match any of the configured policies. By default, the retention module provides an AddDeletePolicy
method, which deletes workflow instances that match the given RetentionWorkflowInstanceFilter
.
Note that the policy takes a function that returns a RetentionWorkflowInstanceFilter
.
This function is called during every SweepInterval
.
Example
Delete a workflow if it has been Finished
for over an hour:
In this example, we use Elsa's provided ISystemClock
to access the current time and filter workflow instances that finished more than an hour ago. Since the filter is re-created during each SweepInterval
, we can use clock.UtcNow
to calculate the threshold dynamically.
Extending
The retention module allows for easy extension to add new types of policies or to include additional entities in the existing policies.
Extra Entities
Let's say you have custom WorkflowInstanceData
created with every workflow instance that also needs to be removed.
Entity Collector
First, define an IRelatedEntityCollector<TEntity>
that, given a set of workflow instances, returns the related WorkflowInstanceData
records.
Cleanup Strategy
Next, define a cleanup strategy for each policy you wish to support. In this example, we’ll implement a strategy to delete WorkflowInstanceData
records.
Register Dependencies
Finally, register the DeleteWorkflowInstanceDataRecordStrategy
and WorkflowInstanceDataRecordCollector
in the dependency container:
Different Cleanup Strategies
Another way to extend the retention feature is by using different cleanup strategies. For example, you could archive workflow instances to a different storage provider.
Defining a Marker Interface
First, create a marker interface for the archiving cleanup strategy:
Defining the Policy
Next, define a policy
Note: In the CleanupStrategy
property, we specify our marker interface (IArchivingStrategy
). This allows the retention module to scan for all implementations of IArchivingStrategy
when executing the ArchivingRetentionPolicy
.
Implementing the Strategy
Next, implement the IArchivingStrategy
for each entity that needs to be archived. The implementation is similar to the CleanupStrategy
in the extra entities example, but instead of deleting the entities, you can move them to a different storage provider.
When implementing a new policy, ensure that you provide an ICleanupStrategy<TEntity>
for the following entities:
ActivityExecutionRecord
StoredBookmark
WorkflowExecutionLogRecord
WorkflowInstance
Last updated