Merge remote-tracking branch 'origin/main'

This commit is contained in:
Sipke Schoorstra 2024-12-14 19:41:05 +01:00
commit a44f082dff
3 changed files with 35 additions and 4 deletions

View file

@ -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()
});
}
}

View file

@ -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>();

View file

@ -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)
{