V3 update routetable on startup (#3780)

* add triggering all indexes to route table

* create hostedservice to run population of routetable
This commit is contained in:
herblinernexxbizz 2023-03-08 21:43:02 +01:00 committed by GitHub
parent 6289d86899
commit 7d7e893a62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 1 deletions

View file

@ -20,4 +20,9 @@ public interface ITriggerIndexer
/// Indexes triggers of the specified workflow.
/// </summary>
Task<IndexedWorkflowTriggers> IndexTriggersAsync(Workflow workflow, CancellationToken cancellationToken = default);
/// <summary>
/// Send message of all triggers in the trigger store to populate the routetable on startup
/// </summary>
Task<IndexedWorkflowTriggers> IndexAllTriggersAsync(CancellationToken cancellationToken = default);
}

View file

@ -109,7 +109,8 @@ public class WorkflowRuntimeFeature : FeatureBase
Module
.ConfigureHostedService<RegisterDescriptors>()
.ConfigureHostedService<RegisterExpressionSyntaxDescriptors>()
.ConfigureHostedService<PopulateWorkflowDefinitionStore>();
.ConfigureHostedService<PopulateWorkflowDefinitionStore>()
.ConfigureHostedService<PopulateRouteTable>();
/// <inheritdoc />
public override void Apply()

View file

@ -0,0 +1,29 @@
using Elsa.Workflows.Runtime.Contracts;
using Microsoft.Extensions.Hosting;
namespace Elsa.Workflows.Runtime.HostedServices;
/// <summary>
/// Synchronously updates the routetable from the triggers.
/// </summary>
public class PopulateRouteTable : IHostedService
{
private readonly ITriggerIndexer _triggerIndexer;
/// <summary>
/// Constructor.
/// </summary>
public PopulateRouteTable(ITriggerIndexer triggerIndexer)
{
_triggerIndexer = triggerIndexer;
}
/// <inheritdoc />
public async Task StartAsync(CancellationToken cancellationToken)
{
await _triggerIndexer.IndexAllTriggersAsync(cancellationToken);
}
/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

View file

@ -67,6 +67,27 @@ public class TriggerIndexer : ITriggerIndexer
return await IndexTriggersAsync(workflow, cancellationToken);
}
/// <inheritdoc />
public async Task<IndexedWorkflowTriggers> IndexAllTriggersAsync(CancellationToken cancellationToken = default)
{
var emptyTriggerList = new List<StoredTrigger>(0);
// Get current triggers
var filter = new TriggerFilter();
var allTriggersTriggers = await _triggerStore.FindManyAsync(filter, cancellationToken);
//workflow definition already deleted so you do not have one
var workflow = new Workflow();
var indexedWorkflow = new IndexedWorkflowTriggers(workflow, allTriggersTriggers.ToList(), emptyTriggerList, emptyTriggerList);
// Publish event.
await _eventPublisher.PublishAsync(new WorkflowTriggersIndexed(indexedWorkflow), cancellationToken);
return indexedWorkflow;
}
/// <inheritdoc />
public async Task<IndexedWorkflowTriggers> IndexTriggersAsync(Workflow workflow, CancellationToken cancellationToken = default)
{