Merge pull request #6211 from Sverre-W/retention-sweep-interval
Retention: Allow to define cleanup strategy for workflow instances
This commit is contained in:
commit
83ccbca0e9
|
|
@ -0,0 +1,20 @@
|
|||
using Elsa.Retention.Contracts;
|
||||
using Elsa.Workflows.Management;
|
||||
using Elsa.Workflows.Management.Entities;
|
||||
using Elsa.Workflows.Management.Filters;
|
||||
|
||||
namespace Elsa.Retention.CleanupStrategies;
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the workflow instance.
|
||||
/// </summary>
|
||||
public class DeleteWorkflowInstanceStrategy(IWorkflowInstanceStore store) : IDeletionCleanupStrategy<WorkflowInstance>
|
||||
{
|
||||
public async Task Cleanup(ICollection<WorkflowInstance> collection)
|
||||
{
|
||||
await store.DeleteAsync(new WorkflowInstanceFilter
|
||||
{
|
||||
Ids = collection.Select(x => x.Id).ToArray()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ using Elsa.Retention.Contracts;
|
|||
using Elsa.Retention.Extensions;
|
||||
using Elsa.Retention.Jobs;
|
||||
using Elsa.Retention.Options;
|
||||
using Elsa.Workflows.Management.Entities;
|
||||
using Elsa.Workflows.Runtime.Entities;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
|
|
@ -44,6 +45,7 @@ public class RetentionFeature : FeatureBase
|
|||
Services.AddScoped<IDeletionCleanupStrategy<StoredBookmark>, DeleteBookmarkStrategy>();
|
||||
Services.AddScoped<IDeletionCleanupStrategy<ActivityExecutionRecord>, DeleteActivityExecutionRecordStrategy>();
|
||||
Services.AddScoped<IDeletionCleanupStrategy<WorkflowExecutionLogRecord>, DeleteWorkflowExecutionRecordStrategy>();
|
||||
Services.AddScoped<IDeletionCleanupStrategy<WorkflowInstance>, DeleteWorkflowInstanceStrategy>();
|
||||
|
||||
Services.AddScoped<IRelatedEntityCollector, BookmarkCollector>();
|
||||
Services.AddScoped<IRelatedEntityCollector, ActivityExecutionRecordCollector>();
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ public class CleanupJob(
|
|||
/// <param name="cancellationToken"></param>
|
||||
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
Console.WriteLine(DateTime.Now.ToLongTimeString());
|
||||
var collectors = GetServices(typeof(IRelatedEntityCollector), typeof(IRelatedEntityCollector<>));
|
||||
var deletedWorkflowInstances = 0L;
|
||||
|
||||
|
|
@ -51,6 +52,10 @@ public class CleanupJob(
|
|||
foreach (var collectorService in collectors)
|
||||
{
|
||||
var cleanupStrategyConcreteType = policy.CleanupStrategy.MakeGenericType(collectorService.Key);
|
||||
|
||||
if(cleanupStrategyConcreteType == typeof(WorkflowInstance))
|
||||
continue;
|
||||
|
||||
var collector = collectorService.Value as IRelatedEntityCollector;
|
||||
var cleanupService = serviceProvider.GetService(cleanupStrategyConcreteType) as ICleanupStrategy;
|
||||
|
||||
|
|
@ -71,11 +76,15 @@ public class CleanupJob(
|
|||
await cleanupService.Cleanup(entities);
|
||||
}
|
||||
}
|
||||
|
||||
var cleanupWorkflowInstances = policy.CleanupStrategy.MakeGenericType(typeof(WorkflowInstance));
|
||||
var workflowInstanceCleaner = serviceProvider.GetService(cleanupWorkflowInstances) as ICleanupStrategy<WorkflowInstance>;
|
||||
|
||||
deletedWorkflowInstances += await workflowInstanceStore.DeleteAsync(new WorkflowInstanceFilter
|
||||
{
|
||||
Ids = page.Items.Select(x => x.Id).ToArray()
|
||||
}, cancellationToken);
|
||||
if (workflowInstanceCleaner == null)
|
||||
throw new Exception($"{policy.CleanupStrategy} has no strategy to clean WorkflowInstances");
|
||||
|
||||
await workflowInstanceCleaner.Cleanup(page.Items);
|
||||
deletedWorkflowInstances += page.Items.Count;
|
||||
|
||||
if (page.TotalCount <= page.Items.Count + pageArgs.Offset)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue