Update TaskReporter and RunTask activity

Replaced IWorkflowDispatcher in TaskReporter with IWorkflowInbox to streamline task reporting. This fixes race conditions when the workflow is attempted to be resumed before it committed its bookmarks.
This commit is contained in:
Sipke Schoorstra 2024-08-14 18:40:04 +02:00
parent 5f11026ee7
commit 0a76741755
5 changed files with 23 additions and 36 deletions

View file

@ -6,27 +6,18 @@ using Microsoft.EntityFrameworkCore;
namespace Elsa.Samples.AspNet.Onboarding.Web.Controllers;
public class HomeController : Controller
public class HomeController(OnboardingDbContext dbContext, ElsaClient elsaClient) : Controller
{
private readonly OnboardingDbContext _dbContext;
private readonly ElsaClient _elsaClient;
public HomeController(OnboardingDbContext dbContext, ElsaClient elsaClient)
{
_dbContext = dbContext;
_elsaClient = elsaClient;
}
public async Task<IActionResult> Index(CancellationToken cancellationToken)
{
var tasks = await _dbContext.Tasks.Where(x => !x.IsCompleted).ToListAsync(cancellationToken: cancellationToken);
var tasks = await dbContext.Tasks.Where(x => !x.IsCompleted).ToListAsync(cancellationToken: cancellationToken);
var model = new IndexViewModel(tasks);
return View(model);
}
public async Task<IActionResult> CompleteTask(int taskId, CancellationToken cancellationToken)
{
var task = _dbContext.Tasks.FirstOrDefault(x => x.Id == taskId);
var task = dbContext.Tasks.FirstOrDefault(x => x.Id == taskId);
if (task == null)
return NotFound();
@ -35,13 +26,13 @@ public class HomeController : Controller
{
Magic = Random.Shared.NextDouble()
};
await _elsaClient.ReportTaskCompletedAsync(task.ExternalId, result, cancellationToken);
await elsaClient.ReportTaskCompletedAsync(task.ExternalId, result, cancellationToken);
task.IsCompleted = true;
task.CompletedAt = DateTimeOffset.Now;
_dbContext.Tasks.Update(task);
await _dbContext.SaveChangesAsync(cancellationToken);
dbContext.Tasks.Update(task);
await dbContext.SaveChangesAsync(cancellationToken);
return RedirectToAction("Index");
}

View file

@ -4,4 +4,4 @@ public record RunTaskWebhook(
string WorkflowInstanceId,
string TaskId,
string TaskName,
TaskPayload TaskPayload);
TaskPayload? TaskPayload);

View file

@ -13,8 +13,4 @@
<ProjectReference Include="..\..\..\src\modules\Elsa.Workflows.Api\Elsa.Workflows.Api.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
</ItemGroup>
</Project>

View file

@ -91,7 +91,7 @@ public class RunTask : Activity<object>
var taskId = identityGenerator.GenerateId();
var payload = new RunTaskBookmarkPayload(taskId, taskName);
context.CreateBookmark(payload, ResumeAsync, includeActivityInstanceId: false);
// Dispatch task request.
var taskParams = Payload.GetOrDefault(context);
var runTaskRequest = new RunTaskRequest(context, taskId, taskName, taskParams);

View file

@ -2,23 +2,14 @@ using Elsa.Workflows.Helpers;
using Elsa.Workflows.Runtime.Activities;
using Elsa.Workflows.Runtime.Bookmarks;
using Elsa.Workflows.Runtime.Contracts;
using Elsa.Workflows.Runtime.Requests;
using Elsa.Workflows.Runtime.Models;
using Elsa.Workflows.Runtime.Parameters;
namespace Elsa.Workflows.Runtime.Services;
namespace Elsa.Workflows.Runtime;
/// <inheritdoc />
public class TaskReporter : ITaskReporter
public class TaskReporter(IWorkflowInbox workflowInbox) : ITaskReporter
{
private readonly IWorkflowDispatcher _workflowDispatcher;
/// <summary>
/// Constructor.
/// </summary>
public TaskReporter(IWorkflowDispatcher workflowDispatcher)
{
_workflowDispatcher = workflowDispatcher;
}
/// <inheritdoc />
public async Task ReportCompletionAsync(string taskId, object? result = default, CancellationToken cancellationToken = default)
{
@ -30,7 +21,16 @@ public class TaskReporter : ITaskReporter
};
var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<RunTask>();
var request = new DispatchResumeWorkflowsRequest(activityTypeName, bookmarkPayload, Input: input);
await _workflowDispatcher.DispatchAsync(request, cancellationToken: cancellationToken);
var message = new NewWorkflowInboxMessage
{
ActivityTypeName = activityTypeName,
BookmarkPayload = bookmarkPayload,
Input = input,
};
var options = new WorkflowInboxMessageDeliveryParams
{
DispatchAsynchronously = true
};
await workflowInbox.SubmitAsync(message, options, cancellationToken);
}
}