Implement Execute and Dispatch Workflow Instance endpoint

This commit is contained in:
Sipke Schoorstra 2021-05-19 10:31:52 +02:00
parent 9c2b57865e
commit 911b933cd5
6 changed files with 112 additions and 11 deletions

View file

@ -57,7 +57,7 @@ namespace Elsa.Services
/// <summary>
/// Executes a pending workflow.
/// </summary>
Task ExecutePendingWorkflowAsync(string workflowInstanceId, string? activityId, object? input = default, CancellationToken cancellationToken = default);
Task<RunWorkflowResult> ExecutePendingWorkflowAsync(string workflowInstanceId, string? activityId, object? input = default, CancellationToken cancellationToken = default);
/// <summary>
/// Dispatches a list of pending workflows for execution.
@ -68,6 +68,11 @@ namespace Elsa.Services
/// Dispatches a pending workflow for execution.
/// </summary>
Task DispatchPendingWorkflowAsync(PendingWorkflow pendingWorkflow, object? input, CancellationToken cancellationToken = default);
/// <summary>
/// Dispatches a pending workflow for execution.
/// </summary>
Task DispatchPendingWorkflowAsync(string workflowInstanceId, string? activityId, object? input, CancellationToken cancellationToken = default);
/// <summary>
/// Executes the specified startable workflow.

View file

@ -193,15 +193,11 @@ namespace Elsa.Services
await _workflowInstanceExecutor.ExecuteAsync(pendingWorkflow.WorkflowInstanceId, pendingWorkflow.ActivityId, input, cancellationToken);
}
public async Task ExecutePendingWorkflowAsync(PendingWorkflow pendingWorkflow, object? input = default, CancellationToken cancellationToken = default)
{
public async Task ExecutePendingWorkflowAsync(PendingWorkflow pendingWorkflow, object? input = default, CancellationToken cancellationToken = default) =>
await _workflowInstanceExecutor.ExecuteAsync(pendingWorkflow.WorkflowInstanceId, pendingWorkflow.ActivityId, input, cancellationToken);
}
public async Task ExecutePendingWorkflowAsync(string workflowInstanceId, string? activityId, object? input = default, CancellationToken cancellationToken = default)
{
public async Task<RunWorkflowResult> ExecutePendingWorkflowAsync(string workflowInstanceId, string? activityId, object? input = default, CancellationToken cancellationToken = default) =>
await _workflowInstanceExecutor.ExecuteAsync(workflowInstanceId, activityId, input, cancellationToken);
}
public async Task DispatchPendingWorkflowsAsync(IEnumerable<PendingWorkflow> pendingWorkflows, object? input, CancellationToken cancellationToken = default)
{
@ -212,6 +208,8 @@ 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 Task DispatchPendingWorkflowAsync(string workflowInstanceId, string? activityId, object? input, CancellationToken cancellationToken = default) => DispatchPendingWorkflowAsync(new PendingWorkflow(workflowInstanceId, activityId), input, cancellationToken);
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)

View file

@ -26,7 +26,7 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WorkflowDefinitionExecuteResponse))]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ExecuteWorkflowDefinitionResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Executes the specified workflow definition.",
@ -46,7 +46,7 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
if (Response.HasStarted)
return new EmptyResult();
return Ok(new WorkflowDefinitionExecuteResponse(
return Ok(new ExecuteWorkflowDefinitionResponse(
result.Executed,
result.ActivityId,
result.WorkflowInstance

View file

@ -13,9 +13,20 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
WorkflowPersistenceBehavior PersistenceBehavior,
bool IsPublished,
bool IsLatest);
public record ExecuteWorkflowDefinitionRequest(string? ActivityId, string? CorrelationId, string? ContextId, object? Input);
public record WorkflowDefinitionExecuteResponse(bool Executed, string? ActivityId, WorkflowInstance? WorkflowInstance);
public record ExecuteWorkflowDefinitionResponse(bool Executed, string? ActivityId, WorkflowInstance? WorkflowInstance);
public record DispatchWorkflowDefinitionRequest(string? ActivityId, string? CorrelationId, string? ContextId, object? Input);
public record DispatchWorkflowDefinitionResponse(string WorkflowInstanceId, string? ActivityId);
public record ExecuteWorkflowInstanceRequest(string? ActivityId, object? Input);
public record ExecuteWorkflowInstanceResponse(bool Executed, string? ActivityId, WorkflowInstance? WorkflowInstance);
public record DispatchWorkflowInstanceRequest(string? ActivityId, object? Input);
public record DispatchWorkflowInstanceResponse();
}

View file

@ -0,0 +1,42 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Server.Api.ActionFilters;
using Elsa.Server.Api.Endpoints.WorkflowDefinitions;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
namespace Elsa.Server.Api.Endpoints.WorkflowInstances
{
[ApiController]
[ApiVersion("1")]
[Route("v{apiVersion:apiVersion}/workflow-instances/{workflowInstanceId}/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(DispatchWorkflowInstanceResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Dispatches the specified workflow instance.",
Description = "Dispatches the specified workflow instance.",
OperationId = "WorkflowInstances.Dispatch",
Tags = new[] { "WorkflowInstances" })
]
public async Task<IActionResult> Handle(string workflowInstanceId, DispatchWorkflowInstanceRequest request, CancellationToken cancellationToken = default)
{
await _workflowLaunchpad.DispatchPendingWorkflowAsync(workflowInstanceId, request.ActivityId, request.Input, cancellationToken);
return Ok(new DispatchWorkflowInstanceResponse());
}
}
}

View file

@ -0,0 +1,45 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Server.Api.ActionFilters;
using Elsa.Server.Api.Endpoints.WorkflowDefinitions;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
namespace Elsa.Server.Api.Endpoints.WorkflowInstances
{
[ApiController]
[ApiVersion("1")]
[Route("v{apiVersion:apiVersion}/workflow-instances/{workflowInstanceId}/execute")]
[Produces("application/json")]
public class Execute : Controller
{
private readonly IWorkflowLaunchpad _workflowLaunchpad;
public Execute(IWorkflowLaunchpad workflowLaunchpad)
{
_workflowLaunchpad = workflowLaunchpad;
}
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ExecuteWorkflowInstanceResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Executes the specified workflow instance.",
Description = "Executes the specified workflow instance.",
OperationId = "WorkflowInstances.Execute",
Tags = new[] { "WorkflowInstances" })
]
public async Task<IActionResult> Handle(string workflowInstanceId, ExecuteWorkflowInstanceRequest request, CancellationToken cancellationToken = default)
{
var result = await _workflowLaunchpad.ExecutePendingWorkflowAsync(workflowInstanceId, request.ActivityId, request.Input, cancellationToken);
if (Response.HasStarted)
return new EmptyResult();
return Ok(new ExecuteWorkflowInstanceResponse(result.Executed, result.ActivityId, result.WorkflowInstance));
}
}
}