-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Removed duplicate entries * Prevented workflows and activities from starting when the parent workflow is being cancelled * Added cancellation to execution contexts * Added store for workflow execution contexts * Added cancellation to workflowRuntime * Removed calling BookmarkPersistedHandler when persisting bookmarks. * Added endpoint for bulk cancelling tasks * Added tests for cancelling workflows * Prevented cancelling the cancellation process since it could have unwanted effects --------- Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com>
- Loading branch information
1 parent
832fac4
commit c409414
Showing
42 changed files
with
854 additions
and
122 deletions.
There are no files selected for viewing
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
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
21 changes: 13 additions & 8 deletions
21
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/BulkCancel/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 |
---|---|---|
@@ -1,25 +1,30 @@ | ||
using System.Text.Json.Serialization; | ||
using Elsa.Abstractions; | ||
using Elsa.Workflows.Runtime.Contracts; | ||
|
||
namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.BulkCancel; | ||
|
||
public class BulkCancel : ElsaEndpoint<Request, Response> | ||
{ | ||
private readonly IWorkflowRuntime _workflowRuntime; | ||
|
||
public BulkCancel(IWorkflowRuntime workflowRuntime) | ||
{ | ||
_workflowRuntime = workflowRuntime; | ||
} | ||
|
||
public override void Configure() | ||
{ | ||
Post("/bulk-actions/cancel/workflow-instances/by-id"); | ||
ConfigurePermissions("cancel:workflow-instances"); | ||
} | ||
|
||
public override async Task<Response> ExecuteAsync(Request request, CancellationToken cancellationToken) | ||
{ | ||
// TODO: Implement workflow cancellation. | ||
var count = -1; | ||
var tasks = request.Ids.Select(id => _workflowRuntime.CancelWorkflowAsync(id, cancellationToken)).ToList(); | ||
await Task.WhenAll(tasks); | ||
|
||
var count = tasks.Count(t => t.IsCompletedSuccessfully); | ||
|
||
return new(count); | ||
} | ||
|
||
public record BulkCancelWorkflowInstancesRequest(ICollection<string> Ids); | ||
|
||
public record BulkCancelWorkflowInstancesResponse([property: JsonPropertyName("cancelled")] int CancelledCount); | ||
} |
44 changes: 44 additions & 0 deletions
44
src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.Cancel.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,44 @@ | ||
using Elsa.Extensions; | ||
using Elsa.Mediator.Contracts; | ||
using Elsa.Workflows.Notifications; | ||
using Elsa.Workflows.Signals; | ||
|
||
namespace Elsa.Workflows; | ||
|
||
public partial class ActivityExecutionContext | ||
{ | ||
private readonly CancellationTokenRegistration _cancellationRegistration; | ||
private readonly CancellationTokenSource _cancellationTokenSource; | ||
private readonly INotificationSender _publisher; | ||
|
||
private void CancelActivity() | ||
{ | ||
// If the activity is not running, do nothing. | ||
if (Status != ActivityStatus.Running && Status != ActivityStatus.Faulted) | ||
return; | ||
|
||
_ = Task.Run(async () => await CancelActivityAsync()); | ||
} | ||
|
||
private async Task CancelActivityAsync() | ||
{ | ||
// Select all child contexts. | ||
var childContexts = WorkflowExecutionContext.ActivityExecutionContexts.Where(x => x.ParentActivityExecutionContext == this).ToList(); | ||
|
||
foreach (var childContext in childContexts) | ||
childContext._cancellationTokenSource.Cancel(); | ||
|
||
TransitionTo(ActivityStatus.Canceled); | ||
ClearBookmarks(); | ||
ClearCompletionCallbacks(); | ||
WorkflowExecutionContext.Bookmarks.RemoveWhere(x => x.ActivityNodeId == NodeId); | ||
|
||
// Add an execution log entry. | ||
AddExecutionLogEntry("Canceled", payload: JournalData, includeActivityState: true); | ||
|
||
_cancellationRegistration.Dispose(); | ||
|
||
await this.SendSignalAsync(new CancelSignal()); | ||
await _publisher.SendAsync(new ActivityCancelled(this)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.ExecutionLogEntry.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,40 @@ | ||
using Elsa.Extensions; | ||
using Elsa.Workflows.Models; | ||
|
||
namespace Elsa.Workflows; | ||
|
||
public partial class ActivityExecutionContext | ||
{ | ||
/// <summary> | ||
/// Adds a new <see cref="WorkflowExecutionLogEntry"/> to the execution log of the current <see cref="Workflows.WorkflowExecutionContext"/>. | ||
/// </summary> | ||
/// <param name="eventName">The name of the event.</param> | ||
/// <param name="message">The message of the event.</param> | ||
/// <param name="source">The source of the activity. For example, the source file name and line number in case of composite activities.</param> | ||
/// <param name="payload">Any contextual data related to this event.</param> | ||
/// <param name="includeActivityState">True to include activity state with this event, false otherwise.</param> | ||
/// <returns>Returns the created <see cref="WorkflowExecutionLogEntry"/>.</returns> | ||
public WorkflowExecutionLogEntry AddExecutionLogEntry(string eventName, string? message = default, string? source = default, object? payload = default, bool includeActivityState = false) | ||
{ | ||
var activityState = includeActivityState ? ActivityState : default; | ||
|
||
var logEntry = new WorkflowExecutionLogEntry( | ||
Id, | ||
ParentActivityExecutionContext?.Id, | ||
Activity.Id, | ||
Activity.Type, | ||
Activity.Version, | ||
Activity.Name, | ||
NodeId, | ||
activityState, | ||
_systemClock.UtcNow, | ||
WorkflowExecutionContext.ExecutionLogSequence++, | ||
eventName, | ||
message, | ||
source ?? Activity.GetSource(), | ||
payload); | ||
|
||
WorkflowExecutionContext.ExecutionLog.Add(logEntry); | ||
return logEntry; | ||
} | ||
} |
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
Oops, something went wrong.