Implement Dispatch Workflow Definition endpoint

This commit is contained in:
Sipke Schoorstra 2021-05-19 10:09:01 +02:00
parent 62236e107e
commit 9c2b57865e
5 changed files with 68 additions and 13 deletions

View file

@ -73,6 +73,11 @@ namespace Elsa.Services
/// Executes the specified startable workflow.
/// </summary>
Task<RunWorkflowResult> ExecuteStartableWorkflowAsync(StartableWorkflow startableWorkflow, object? input, CancellationToken cancellationToken = default);
/// <summary>
/// Dispatches the specified startable workflow.
/// </summary>
Task<PendingWorkflow> DispatchStartableWorkflowAsync(StartableWorkflow startableWorkflow, object? input, CancellationToken cancellationToken = default);
/// <summary>
/// Collects and executes workflows that are ready for execution. This takes into account both resumable (suspended) workflows as well as startable workflows.

View file

@ -212,9 +212,13 @@ namespace Elsa.Services
public async Task DispatchPendingWorkflowAsync(PendingWorkflow pendingWorkflow, object? input, CancellationToken cancellationToken = default) =>
await _workflowInstanceDispatcher.DispatchAsync(new ExecuteWorkflowInstanceRequest(pendingWorkflow.WorkflowInstanceId, pendingWorkflow.ActivityId, input), cancellationToken);
public async Task<RunWorkflowResult> ExecuteStartableWorkflowAsync(StartableWorkflow startableWorkflow, object? input, CancellationToken cancellationToken = default)
public async Task<RunWorkflowResult> ExecuteStartableWorkflowAsync(StartableWorkflow startableWorkflow, object? input, CancellationToken cancellationToken = default) => await _workflowRunner.RunWorkflowAsync(startableWorkflow.WorkflowBlueprint, startableWorkflow.WorkflowInstance, startableWorkflow.ActivityId, input, cancellationToken);
public async Task<PendingWorkflow> DispatchStartableWorkflowAsync(StartableWorkflow startableWorkflow, object? input, CancellationToken cancellationToken = default)
{
return await _workflowRunner.RunWorkflowAsync(startableWorkflow.WorkflowBlueprint, startableWorkflow.WorkflowInstance, startableWorkflow.ActivityId, input, cancellationToken);
var pendingWorkflow = new PendingWorkflow(startableWorkflow.WorkflowInstance.Id, startableWorkflow.ActivityId);
await ExecutePendingWorkflowAsync(pendingWorkflow, input, cancellationToken);
return pendingWorkflow;
}
public async Task<IEnumerable<PendingWorkflow>> TriggerWorkflowsAsync(CollectWorkflowsContext context, object? input = default, CancellationToken cancellationToken = default)

View file

@ -0,0 +1,45 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Server.Api.ActionFilters;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
{
[ApiController]
[ApiVersion("1")]
[Route("v{apiVersion:apiVersion}/workflow-definitions/{workflowDefinitionId}/dispatch")]
[Produces("application/json")]
public class Dispatch : Controller
{
private readonly IWorkflowLaunchpad _workflowLaunchpad;
public Dispatch(IWorkflowLaunchpad workflowLaunchpad)
{
_workflowLaunchpad = workflowLaunchpad;
}
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(DispatchWorkflowDefinitionResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Executes the specified workflow definition.",
Description = "Executes the specified workflow definition.",
OperationId = "WorkflowDefinitions.Execute",
Tags = new[] { "WorkflowDefinitions" })
]
public async Task<IActionResult> Handle(string workflowDefinitionId, DispatchWorkflowDefinitionRequest request, CancellationToken cancellationToken = default)
{
var startableWorkflow = await _workflowLaunchpad.CollectStartableWorkflowAsync(workflowDefinitionId, request.ActivityId, request.CorrelationId, request.ContextId, default, cancellationToken);
if (startableWorkflow == null)
return NotFound();
var result = await _workflowLaunchpad.DispatchStartableWorkflowAsync(startableWorkflow, request.Input, cancellationToken);
return Ok(new DispatchWorkflowDefinitionResponse(result.WorkflowInstanceId, result.ActivityId));
}
}
}

View file

@ -26,8 +26,7 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WorkflowDefinition))]
[SwaggerResponseExample(StatusCodes.Status200OK, typeof(WorkflowDefinitionExample))]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WorkflowDefinitionExecuteResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Executes the specified workflow definition.",
@ -35,24 +34,23 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
OperationId = "WorkflowDefinitions.Execute",
Tags = new[] { "WorkflowDefinitions" })
]
public async Task<IActionResult> Handle(string workflowDefinitionId, ExecuteWorkflowDefinitionModel model, CancellationToken cancellationToken = default)
public async Task<IActionResult> Handle(string workflowDefinitionId, ExecuteWorkflowDefinitionRequest request, CancellationToken cancellationToken = default)
{
var startableWorkflow = await _workflowLaunchpad.CollectStartableWorkflowAsync(workflowDefinitionId, model.ActivityId, model.CorrelationId, model.ContextId, default, cancellationToken);
var startableWorkflow = await _workflowLaunchpad.CollectStartableWorkflowAsync(workflowDefinitionId, request.ActivityId, request.CorrelationId, request.ContextId, default, cancellationToken);
if (startableWorkflow == null)
return NotFound();
var result = await _workflowLaunchpad.ExecuteStartableWorkflowAsync(startableWorkflow, model.Input, cancellationToken);
var result = await _workflowLaunchpad.ExecuteStartableWorkflowAsync(startableWorkflow, request.Input, cancellationToken);
if (Response.HasStarted)
return new EmptyResult();
return Ok(new
{
return Ok(new WorkflowDefinitionExecuteResponse(
result.Executed,
result.ActivityId,
result.WorkflowInstance
});
));
}
}
}

View file

@ -14,5 +14,8 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
bool IsPublished,
bool IsLatest);
public record ExecuteWorkflowDefinitionModel(string? ActivityId, string? CorrelationId, string? ContextId, object? Input);
public record ExecuteWorkflowDefinitionRequest(string? ActivityId, string? CorrelationId, string? ContextId, object? Input);
public record WorkflowDefinitionExecuteResponse(bool Executed, string? ActivityId, WorkflowInstance? WorkflowInstance);
public record DispatchWorkflowDefinitionRequest(string? ActivityId, string? CorrelationId, string? ContextId, object? Input);
public record DispatchWorkflowDefinitionResponse(string WorkflowInstanceId, string? ActivityId);
}