Skip to content

Commit

Permalink
fix issue 4308 (#4375)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Chiavegato authored Sep 9, 2023
1 parent ebcbb33 commit c9b696b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
64 changes: 56 additions & 8 deletions src/activities/Elsa.Activities.Http/Consumers/UpdateRouteTable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Activities.Http.Bookmarks;
using Elsa.Activities.Http.Contracts;
using Elsa.Activities.Http.Extensions;
using Elsa.Events;
using Elsa.Models;
using Elsa.Persistence;
using Elsa.Persistence.Specifications.Bookmarks;
using Elsa.Persistence.Specifications.Triggers;
using Elsa.Services;
using Elsa.Services.Bookmarks;
using Rebus.Extensions;
using Rebus.Handlers;

namespace Elsa.Activities.Http.Consumers
Expand All @@ -13,18 +23,43 @@ public class UpdateRouteTable :
IHandleMessages<BookmarksDeleted>
{
private readonly IRouteTable _routeTable;
public UpdateRouteTable(IRouteTable routeTable) => _routeTable = routeTable;
private readonly ITriggerStore _triggerStore;
private readonly IBookmarkStore _bookmarkStore;
private readonly IBookmarkHasher _bookmarkHasher;
private readonly BookmarkSerializer _bookmarkSerializer;

public UpdateRouteTable(
IRouteTable routeTable,
ITriggerStore triggerStore,
IBookmarkStore bookmarkStore,
IBookmarkHasher bookmarkHasher)
{
_routeTable = routeTable;
_triggerStore = triggerStore;
_bookmarkStore = bookmarkStore;
_bookmarkHasher = bookmarkHasher;
_bookmarkSerializer = new();
}

public Task Handle(TriggerIndexingFinished message)
{
_routeTable.AddRoutes(message.Triggers);
return Task.CompletedTask;
}

public Task Handle(TriggersDeleted message)
public async Task Handle(TriggersDeleted message)
{
_routeTable.RemoveRoutes(message.Triggers);
return Task.CompletedTask;
WorkflowDefinitionIdSpecification specification = new(message.WorkflowDefinitionId);
ICollection<Trigger> currentDefinitionTriggers = (await _triggerStore.FindManyAsync(specification)).ToList();

bool Exists(Trigger trigger) => currentDefinitionTriggers.Any(x => x.ActivityId == trigger.ActivityId
&& x.WorkflowDefinitionId == trigger.WorkflowDefinitionId
&& x.ModelType == typeof(HttpEndpointBookmark).GetSimpleAssemblyQualifiedName()
&& trigger.ModelType == typeof(HttpEndpointBookmark).GetSimpleAssemblyQualifiedName()
&& _bookmarkHasher.Hash(Deserialize(x)) == _bookmarkHasher.Hash(Deserialize(trigger)));

ICollection<Trigger> triggers = message.Triggers.Where(x => !Exists(x)).ToList();
_routeTable.RemoveRoutes(triggers);
}

public Task Handle(BookmarkIndexingFinished message)
Expand All @@ -33,10 +68,23 @@ public Task Handle(BookmarkIndexingFinished message)
return Task.CompletedTask;
}

public Task Handle(BookmarksDeleted message)
public async Task Handle(BookmarksDeleted message)
{
_routeTable.RemoveRoutes (message.Bookmarks);
return Task.CompletedTask;
WorkflowInstanceIdSpecification specification = new(message.WorkflowInstanceId);
ICollection<Bookmark> currentInstanceBookmarks = (await _bookmarkStore.FindManyAsync(specification)).ToList();

bool Exists(Bookmark bookmark) => currentInstanceBookmarks.Any(x => x.ActivityId == bookmark.ActivityId
&& x.WorkflowInstanceId == bookmark.WorkflowInstanceId
&& x.ModelType == typeof(HttpEndpointBookmark).GetSimpleAssemblyQualifiedName()
&& bookmark.ModelType == typeof(HttpEndpointBookmark).GetSimpleAssemblyQualifiedName()
&& _bookmarkHasher.Hash(Deserialize(x)) == _bookmarkHasher.Hash(Deserialize(bookmark)));

ICollection<Bookmark> bookmarks = message.Bookmarks.Where(x => !Exists(x)).ToList();
_routeTable.RemoveRoutes(bookmarks);
}

private HttpEndpointBookmark Deserialize(Trigger trigger) => Deserialize(trigger.Model);
private HttpEndpointBookmark Deserialize(Bookmark bookmark) => Deserialize(bookmark.Model);
private HttpEndpointBookmark Deserialize(string model) => _bookmarkSerializer.Deserialize<HttpEndpointBookmark>(model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ from route in routeTable
routeData.Values[routeValue.Key] = routeValue.Value;
}

// Create a workflow query using the selected route and HTTP method of the request.
const string activityType = nameof(HttpEndpoint);
var bookmark = new HttpEndpointBookmark(routeTemplate, method);
var collectWorkflowsContext = new WorkflowsQuery(activityType, bookmark, correlationId, default, default, tenantId);
var pendingWorkflows = await workflowLaunchpad.FindWorkflowsAsync(collectWorkflowsContext, cancellationToken).ToList();
// Find pending workflows using the selected route and HTTP method of the request.
var pendingWorkflows = await FindPendingWorkflows(workflowLaunchpad, routeTemplate, method, cancellationToken, correlationId, tenantId).ToList();
if (pendingWorkflows?.Count == 0 && string.IsNullOrWhiteSpace(matchingRoute?.route))
{
routeTemplate = path.StartsWith("/") ? path[1..] : $"/{path}";
pendingWorkflows = await FindPendingWorkflows(workflowLaunchpad, routeTemplate, method, cancellationToken, correlationId, tenantId).ToList();
}

if (await HandleNoWorkflowsFoundAsync(httpContext, pendingWorkflows, basePath))
return;
Expand Down Expand Up @@ -274,5 +276,19 @@ private async Task<bool> HandleMultipleWorkflowsFoundAsync(HttpContext httpConte
private string? GetPath(PathString? basePath, HttpContext httpContext) => basePath != null
? httpContext.Request.Path.StartsWithSegments(basePath.Value, out _, out var remainingPath) ? remainingPath.Value : null
: httpContext.Request.Path.Value;

private async Task<IEnumerable<CollectedWorkflow>> FindPendingWorkflows(
IWorkflowLaunchpad workflowLaunchpad,
string routeTemplate,
string method,
CancellationToken cancellationToken,
string? correlationId = null,
string? tenantId = null)
{
const string activityType = nameof(HttpEndpoint);
var bookmark = new HttpEndpointBookmark(routeTemplate, method);
var collectWorkflowsContext = new WorkflowsQuery(activityType, bookmark, correlationId, default, default, tenantId);
return await workflowLaunchpad.FindWorkflowsAsync(collectWorkflowsContext, cancellationToken);
}
}
}

0 comments on commit c9b696b

Please sign in to comment.