-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DryRun endpoint and workflow instance finder (#5110)
* Add DryRun endpoint and workflow instance finder Added a new DryRun endpoint for executing alteration plans and implemented the IWorkflowInstanceFinder interface to search for workflow instances. Refactored existing code to use new workflow instance finder service, resulting in cleaner activity implementation. * Fix empty check in WorkflowInstanceFinder Change the conditional logic to use `IsEmpty` method on filters rather than checking if the collection `workflowInstanceIds` is empty. This ensures that matching workflow instances are correctly identified when no specific filters are set. * Refactor WorkflowInstanceFinder with WorkflowStatus default The WorkflowInstanceFinder service has been refactored to include a default workflow status of Running and to utilize a new method WorkflowFilterIsEmpty, which checks if the workflow instance filter is empty. This improves code readability and encapsulates the logic for determining an empty filter.
- Loading branch information
1 parent
e67a6fe
commit d117543
Showing
10 changed files
with
145 additions
and
59 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/modules/Elsa.Alterations.Core/Contracts/IWorkflowInstanceFinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Elsa.Alterations.Core.Models; | ||
|
||
namespace Elsa.Alterations.Core.Contracts; | ||
|
||
/// <summary> | ||
/// Represents a service that can find workflow instances based on specified filters. | ||
/// </summary> | ||
public interface IWorkflowInstanceFinder | ||
{ | ||
/// <summary> | ||
/// Finds workflow instances based on the specified filter. | ||
/// </summary> | ||
Task<IEnumerable<string>> FindAsync(AlterationWorkflowInstanceFilter filter, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
...tracts/DefaultAlteredWorkflowScheduler.cs → ...ore/Services/AlteredWorkflowDispatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/modules/Elsa.Alterations.Core/Services/WorkflowInstanceFinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Elsa.Alterations.Core.Contracts; | ||
using Elsa.Alterations.Core.Models; | ||
using Elsa.Workflows; | ||
using Elsa.Workflows.Management.Contracts; | ||
using Elsa.Workflows.Management.Filters; | ||
using Elsa.Workflows.Runtime.Contracts; | ||
using Elsa.Workflows.Runtime.Filters; | ||
|
||
namespace Elsa.Alterations.Core.Services; | ||
|
||
/// <inheritdoc /> | ||
public class WorkflowInstanceFinder(IWorkflowInstanceStore workflowInstanceStore, IActivityExecutionStore activityExecutionStore) : IWorkflowInstanceFinder | ||
{ | ||
/// <inheritdoc /> | ||
public async Task<IEnumerable<string>> FindAsync(AlterationWorkflowInstanceFilter filter, CancellationToken cancellationToken = default) | ||
{ | ||
var workflowInstanceFilter = new WorkflowInstanceFilter | ||
{ | ||
Ids = filter.WorkflowInstanceIds?.ToList(), | ||
DefinitionVersionIds = filter.DefinitionVersionIds?.ToList(), | ||
CorrelationIds = filter.CorrelationIds?.ToList(), | ||
HasIncidents = filter.HasIncidents, | ||
IsSystem = filter.IsSystem, | ||
TimestampFilters = filter.TimestampFilters?.ToList(), | ||
WorkflowStatus = WorkflowStatus.Running | ||
}; | ||
var activityExecutionFilters = filter.ActivityFilters?.Select(x => new ActivityExecutionRecordFilter | ||
{ | ||
ActivityId = x.ActivityId, | ||
Id = x.ActivityInstanceId, | ||
ActivityNodeId = x.NodeId, | ||
Name = x.Name, | ||
Status = x.Status, | ||
}).ToList(); | ||
|
||
var workflowInstanceFilterIsEmpty = WorkflowFilterIsEmpty(workflowInstanceFilter); | ||
|
||
var workflowInstanceIds = workflowInstanceFilterIsEmpty | ||
? Enumerable.Empty<string>().ToHashSet() | ||
: (await workflowInstanceStore.FindManyIdsAsync(workflowInstanceFilter, cancellationToken)).ToHashSet(); | ||
|
||
if (activityExecutionFilters == null) | ||
return workflowInstanceIds; | ||
|
||
foreach (ActivityExecutionRecordFilter activityExecutionFilter in activityExecutionFilters.Where(x => !x.IsEmpty)) | ||
{ | ||
var activityExecutionRecords = await activityExecutionStore.FindManySummariesAsync(activityExecutionFilter, cancellationToken); | ||
var matchingWorkflowInstanceIds = activityExecutionRecords.Select(x => x.WorkflowInstanceId).ToHashSet(); | ||
|
||
if (workflowInstanceFilterIsEmpty) | ||
workflowInstanceIds = matchingWorkflowInstanceIds; | ||
else | ||
workflowInstanceIds.IntersectWith(matchingWorkflowInstanceIds); | ||
} | ||
|
||
return workflowInstanceIds; | ||
} | ||
|
||
private bool WorkflowFilterIsEmpty(WorkflowInstanceFilter filter) | ||
{ | ||
return filter.Id == null && | ||
filter.Ids == null && | ||
filter.DefinitionId == null && | ||
filter.DefinitionVersionId == null && | ||
filter.DefinitionIds == null && | ||
filter.DefinitionVersionIds == null && | ||
filter.Version == null && | ||
filter.CorrelationId == null && | ||
filter.CorrelationIds == null && | ||
filter.HasIncidents == null && | ||
filter.TimestampFilters == null | ||
&& string.IsNullOrWhiteSpace(filter.SearchTerm); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/modules/Elsa.Alterations/Endpoints/Alterations/DryRun/Endpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Elsa.Abstractions; | ||
using Elsa.Alterations.Core.Contracts; | ||
using Elsa.Alterations.Core.Models; | ||
using JetBrains.Annotations; | ||
|
||
namespace Elsa.Alterations.Endpoints.Alterations.DryRun; | ||
|
||
/// <summary> | ||
/// Executes an alteration plan. | ||
/// </summary> | ||
[PublicAPI] | ||
public class DryRun(IWorkflowInstanceFinder workflowInstanceFinder) : ElsaEndpoint<AlterationWorkflowInstanceFilter, Response> | ||
{ | ||
/// <inheritdoc /> | ||
public override void Configure() | ||
{ | ||
Post("/alterations/dry-run"); | ||
ConfigurePermissions("run:alterations"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override async Task HandleAsync(AlterationWorkflowInstanceFilter filter, CancellationToken cancellationToken) | ||
{ | ||
var workflowInstanceIds = await workflowInstanceFinder.FindAsync(filter, cancellationToken); | ||
|
||
// Write response. | ||
var response = new Response(workflowInstanceIds.ToList()); | ||
await SendOkAsync(response, cancellationToken); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/modules/Elsa.Alterations/Endpoints/Alterations/DryRun/Response.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Elsa.Alterations.Endpoints.Alterations.DryRun; | ||
|
||
/// <summary> | ||
/// The response from the <see cref="DryRun"/> endpoint. | ||
/// </summary> | ||
public record Response(ICollection<string> WorkflowInstanceIds); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters