Skip to content

Commit

Permalink
Update TaskReporter and RunTask activity
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sfmskywalker committed Aug 14, 2024
1 parent 5f11026 commit 0a76741
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,18 @@

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();
Expand All @@ -35,13 +26,13 @@ public async Task<IActionResult> CompleteTask(int taskId, CancellationToken canc
{
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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public record RunTaskWebhook(
string WorkflowInstanceId,
string TaskId,
string TaskName,
TaskPayload TaskPayload);
TaskPayload? TaskPayload);
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@
<ProjectReference Include="..\..\..\src\modules\Elsa.Workflows.Api\Elsa.Workflows.Api.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="App_Data\" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/modules/Elsa.Workflows.Runtime/Activities/RunTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected override async ValueTask ExecuteAsync(ActivityExecutionContext context
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);
Expand Down
30 changes: 15 additions & 15 deletions src/modules/Elsa.Workflows.Runtime/Services/TaskReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@
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)
{
Expand All @@ -30,7 +21,16 @@ public async Task ReportCompletionAsync(string taskId, object? result = default,
};

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);
}
}

0 comments on commit 0a76741

Please sign in to comment.