using Elsa.Models;
using Elsa.Runtime.Contracts;
using Elsa.Runtime.Models;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Runtime.Services;
///
/// A workflow server represents a logical unit of services to invoke workflows.
/// It's essentially a thin wrapper around a service provider built from a given service collection using .
/// May be useful for scenarios where you have multiple tenants in a system or if you simply want to use different workflow engine configurations within the same application.
///
public class WorkflowServer : IWorkflowServer
{
public WorkflowServer(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public IServiceProvider ServiceProvider { get; }
public async Task ExecuteWorkflowAsync(string definitionId, int version, CancellationToken cancellationToken = default)
{
var workflowInvoker = ServiceProvider.GetRequiredService();
var executeRequest = new ExecuteWorkflowDefinitionRequest(definitionId, version);
return await workflowInvoker.ExecuteAsync(executeRequest, cancellationToken);
}
public async Task ExecuteWorkflowAsync(string instanceId, Bookmark bookmark, CancellationToken cancellationToken = default)
{
var workflowInvoker = ServiceProvider.GetRequiredService();
var request = new ExecuteWorkflowInstanceRequest(instanceId, bookmark);
return await workflowInvoker.ExecuteAsync(request, cancellationToken);
}
public async Task DispatchWorkflowAsync(string definitionId, int version, CancellationToken cancellationToken = default)
{
var workflowInvoker = ServiceProvider.GetRequiredService();
var executeRequest = new DispatchWorkflowDefinitionRequest(definitionId, version);
return await workflowInvoker.DispatchAsync(executeRequest, cancellationToken);
}
public async Task DispatchWorkflowAsync(string instanceId, Bookmark bookmark, CancellationToken cancellationToken = default)
{
var workflowInvoker = ServiceProvider.GetRequiredService();
var request = new DispatchWorkflowInstanceRequest(instanceId, bookmark);
return await workflowInvoker.DispatchAsync(request, cancellationToken);
}
public async Task> ExecuteStimulusAsync(IStimulus stimulus, CancellationToken cancellationToken = default)
{
// Collect instructions for the specified stimulus.
var instructions = await GetWorkflowInstructionsAsync(stimulus, cancellationToken);
// Execute instructions.
var instructionExecutor = ServiceProvider.GetRequiredService();
return await instructionExecutor.ExecuteInstructionsAsync(instructions, cancellationToken);
}
public async Task> DispatchStimulusAsync(IStimulus stimulus, CancellationToken cancellationToken)
{
// Collect instructions for the specified stimulus.
var instructions = await GetWorkflowInstructionsAsync(stimulus, cancellationToken);
// Execute instructions.
var instructionExecutor = ServiceProvider.GetRequiredService();
return await instructionExecutor.DispatchInstructionsAsync(instructions, cancellationToken);
}
private async Task> GetWorkflowInstructionsAsync(IStimulus stimulus, CancellationToken cancellationToken = default)
{
var stimulusInterpreter = ServiceProvider.GetRequiredService();
return await stimulusInterpreter.GetExecutionInstructionsAsync(stimulus, cancellationToken);
}
}