Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
sfmskywalker committed Dec 14, 2024
2 parents bd9e006 + 83ccbca commit a44f082
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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()
});
}
}
2 changes: 2 additions & 0 deletions src/modules/Elsa.Retention/Feature/RetentionFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;

Expand Down Expand Up @@ -44,6 +45,7 @@ public override void Apply()
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>();
Expand Down
17 changes: 13 additions & 4 deletions src/modules/Elsa.Retention/Jobs/CleanupJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -51,6 +52,10 @@ public async Task ExecuteAsync(CancellationToken cancellationToken = default)
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;

Expand All @@ -71,11 +76,15 @@ public async Task ExecuteAsync(CancellationToken cancellationToken = default)
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)
{
Expand Down

0 comments on commit a44f082

Please sign in to comment.