public async Task ExecuteAsync(CancellationToken cancellationToken = default) { var threshold = _clock.GetCurrentInstant().Minus(_options.TimeToLive); var specification = new WorkflowCreatedBeforeSpecification(threshold); var currentPage = 0; var take = _options.BatchSize; var orderBy = new OrderBy <WorkflowInstance>(x => x.CreatedAt, SortDirection.Descending); var collectedWorkflowInstanceIds = new List <string>(); // Collect workflow instances to be deleted. while (true) { var paging = Paging.Page(currentPage++, take); var workflowInstances = await _workflowInstanceStore .FindManyAsync(specification, orderBy, paging, cancellationToken) .ToList(); var filteredWorkflowInstances = await _retentionFilterPipeline.FilterAsync(workflowInstances, cancellationToken).ToList(); collectedWorkflowInstanceIds.AddRange(filteredWorkflowInstances.Select(x => x.Id)); if (workflowInstances.Count < take) { break; } } // Delete collected workflow instances. await DeleteManyAsync(collectedWorkflowInstanceIds, cancellationToken); }
public async Task ExecuteAsync(CancellationToken cancellationToken = default) { var threshold = _clock.GetCurrentInstant().Minus(_options.TimeToLive); var specification = new WorkflowCreatedBeforeSpecification(threshold).And(new WorkflowFinishedStatusSpecification()); var take = _options.PageSize; IList <string> workflowInstanceIds; do { workflowInstanceIds = (await _workflowInstanceStore.FindManyAsync(specification, new OrderBy <WorkflowInstance>(x => x.CreatedAt, SortDirection.Descending), new Paging(0, take), cancellationToken: cancellationToken)) .Select(x => x.Id).ToList(); _logger.LogInformation("Deleting {WorkflowInstanceCount} workflow instances", workflowInstanceIds.Count); if (workflowInstanceIds.Any()) { await DeleteManyAsync(workflowInstanceIds, cancellationToken); } } while (workflowInstanceIds.Any()); }