From 937b86135996e0955cda7a8bd99667d4bbb9fef0 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 15 Apr 2021 21:38:10 +0200 Subject: [PATCH] Optimize workflow registry loading by only considering older versions for which there are non-finished workflow instances pending --- .../Services/ServiceBusQueuesStarter.cs | 11 ----------- .../Elsa.Core/Services/WorkflowRegistry.cs | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/activities/Elsa.Activities.AzureServiceBus/Services/ServiceBusQueuesStarter.cs b/src/activities/Elsa.Activities.AzureServiceBus/Services/ServiceBusQueuesStarter.cs index d8f2779a1..5b6db2caf 100644 --- a/src/activities/Elsa.Activities.AzureServiceBus/Services/ServiceBusQueuesStarter.cs +++ b/src/activities/Elsa.Activities.AzureServiceBus/Services/ServiceBusQueuesStarter.cs @@ -65,7 +65,6 @@ namespace Elsa.Activities.AzureServiceBus.Services using var scope = _scopeFactory.CreateScope(); var workflowRegistry = scope.ServiceProvider.GetRequiredService(); var workflowBlueprintReflector = scope.ServiceProvider.GetRequiredService(); - var workflowInstanceStore = scope.ServiceProvider.GetRequiredService(); var workflows = await workflowRegistry.ListAsync(cancellationToken); var query = @@ -76,10 +75,6 @@ namespace Elsa.Activities.AzureServiceBus.Services foreach (var workflow in query) { - // If a workflow is not published, only consider it for processing if it has at least one non-ended workflow instance. - if (!workflow.IsPublished && !await WorkflowHasNonFinishedWorkflowsAsync(workflow, workflowInstanceStore, cancellationToken)) - continue; - var workflowBlueprintWrapper = await workflowBlueprintReflector.ReflectAsync(scope.ServiceProvider, workflow, cancellationToken); foreach (var activity in workflowBlueprintWrapper.Filter()) @@ -102,11 +97,5 @@ namespace Elsa.Activities.AzureServiceBus.Services } } } - - private static async Task WorkflowHasNonFinishedWorkflowsAsync(IWorkflowBlueprint workflowBlueprint, IWorkflowInstanceStore workflowInstanceStore, CancellationToken cancellationToken) - { - var count = await workflowInstanceStore.CountAsync(new NonFinalizedWorkflowSpecification().WithWorkflowDefinition(workflowBlueprint.Id), cancellationToken); - return count > 0; - } } } \ No newline at end of file diff --git a/src/core/Elsa.Core/Services/WorkflowRegistry.cs b/src/core/Elsa.Core/Services/WorkflowRegistry.cs index 216f86e5f..d5eedb389 100644 --- a/src/core/Elsa.Core/Services/WorkflowRegistry.cs +++ b/src/core/Elsa.Core/Services/WorkflowRegistry.cs @@ -5,6 +5,8 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Elsa.Models; +using Elsa.Persistence; +using Elsa.Persistence.Specifications.WorkflowInstances; using Elsa.Services.Models; using Open.Linq.AsyncExtensions; @@ -13,10 +15,12 @@ namespace Elsa.Services public class WorkflowRegistry : IWorkflowRegistry { private readonly IEnumerable _workflowProviders; + private readonly IWorkflowInstanceStore _workflowInstanceStore; - public WorkflowRegistry(IEnumerable workflowProviders) + public WorkflowRegistry(IEnumerable workflowProviders, IWorkflowInstanceStore workflowInstanceStore) { _workflowProviders = workflowProviders; + _workflowInstanceStore = workflowInstanceStore; } public async Task> ListAsync(CancellationToken cancellationToken) => await GetWorkflowsInternalAsync(cancellationToken).ToListAsync(cancellationToken); @@ -36,7 +40,19 @@ namespace Elsa.Services foreach (var provider in providers) await foreach (var workflow in provider.GetWorkflowsAsync(cancellationToken).WithCancellation(cancellationToken)) + { + // If a workflow is not published, only consider it for processing if it has at least one non-ended workflow instance. + if (!workflow.IsPublished && !await WorkflowHasNonFinishedWorkflowsAsync(workflow, cancellationToken)) + continue; + yield return workflow; + } + } + + private async Task WorkflowHasNonFinishedWorkflowsAsync(IWorkflowBlueprint workflowBlueprint, CancellationToken cancellationToken) + { + var count = await _workflowInstanceStore.CountAsync(new NonFinalizedWorkflowSpecification().WithWorkflowDefinition(workflowBlueprint.Id), cancellationToken); + return count > 0; } } } \ No newline at end of file