From c9e2b11dfe3718bbf02eac202f59fdce9f17cc74 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sun, 15 Oct 2023 11:35:38 +0200 Subject: [PATCH] Fix RunTask activity This fixes the issue where its bookmark would include the activity instance ID, which causes the task completion reporter to miss the bookmark, since the activity instance ID is not included. Including the activity instance ID is not necessary for locating the correct activity instance because the generated Task ID is unique for each activity run. Fixes #4511 --- .../Activities/RunTask.cs | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/modules/Elsa.Workflows.Runtime/Activities/RunTask.cs b/src/modules/Elsa.Workflows.Runtime/Activities/RunTask.cs index c8e061d8f..70ee2cd36 100644 --- a/src/modules/Elsa.Workflows.Runtime/Activities/RunTask.cs +++ b/src/modules/Elsa.Workflows.Runtime/Activities/RunTask.cs @@ -20,10 +20,10 @@ namespace Elsa.Workflows.Runtime.Activities; /// [Activity("Elsa", "Primitives", "Requests a given task to be run. ", Kind = ActivityKind.Action)] [PublicAPI] -public class RunTask : Activity, IBookmarksPersistedHandler +public class RunTask : Activity { private static readonly object BookmarkPropertyKey = new(); - + /// /// The key that is used for sending and receiving activity input. /// @@ -34,19 +34,19 @@ public class RunTask : Activity, IBookmarksPersistedHandler /// [Input(Description = "The name of the task being requested.")] public Input TaskName { get; set; } = default!; - + /// /// The name of the task being requested. /// [Input(Description = "Any additional parameters to send to the task.")] public Input?> Payload { get; set; } = default!; - + /// [JsonConstructor] private RunTask(string? source = default, int? line = default) : base(source, line) { } - + /// public RunTask(MemoryBlockReference output, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(output, source, line) { @@ -56,20 +56,20 @@ public class RunTask : Activity, IBookmarksPersistedHandler public RunTask(Output? output, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(output, source, line) { } - + /// public RunTask(string taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(new Literal(taskName), source, line) { } /// - public RunTask(Func taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) + public RunTask(Func taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(new DelegateBlockReference(taskName), source, line) { } /// - public RunTask(Func taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) + public RunTask(Func taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(new DelegateBlockReference(taskName), source, line) { } @@ -80,16 +80,23 @@ public class RunTask : Activity, IBookmarksPersistedHandler /// public RunTask(Literal taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line) => TaskName = new Input(taskName); - + /// - protected override void Execute(ActivityExecutionContext context) + protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) { + // Create bookmark. var taskName = TaskName.Get(context); var identityGenerator = context.GetRequiredService(); var taskId = identityGenerator.GenerateId(); var payload = new RunTaskBookmarkPayload(taskId, taskName); - context.CreateBookmark(payload, ResumeAsync); - context.TransientProperties[BookmarkPropertyKey] = payload; + context.CreateBookmark(payload, ResumeAsync, includeActivityInstanceId: false); + + // Dispatch task request. + var taskParams = Payload.GetOrDefault(context); + var runTaskRequest = new RunTaskRequest(context, taskId, taskName, taskParams); + var dispatcher = context.GetRequiredService(); + + await dispatcher.DispatchAsync(runTaskRequest, context.CancellationToken); } private async ValueTask ResumeAsync(ActivityExecutionContext context) @@ -98,15 +105,4 @@ public class RunTask : Activity, IBookmarksPersistedHandler context.Set(Result, input); await context.CompleteActivityAsync(); } - - async ValueTask IBookmarksPersistedHandler.BookmarksPersistedAsync(ActivityExecutionContext context) - { - var bookmark = (RunTaskBookmarkPayload)context.TransientProperties[BookmarkPropertyKey]; - var taskParams = Payload.GetOrDefault(context); - var taskName = TaskName.Get(context); - var notification = new RunTaskRequest(context, bookmark.TaskId, taskName, taskParams); - var dispatcher = context.GetRequiredService(); - - await dispatcher.DispatchAsync(notification, context.CancellationToken); - } -} +} \ No newline at end of file