using Elsa.Common.Models;
using Elsa.Extensions;
using Elsa.Workflows.Runtime.Contracts;
using Elsa.Workflows.Runtime.Requests;
using Quartz;
namespace Elsa.Quartz.Jobs;
///
/// A job that runs a workflow.
///
public class RunWorkflowJob : IJob
{
private readonly IWorkflowDispatcher _workflowDispatcher;
///
/// The job key.
///
public static readonly JobKey JobKey = new(nameof(RunWorkflowJob));
///
/// Initializes a new instance of the class.
///
public RunWorkflowJob(IWorkflowDispatcher workflowDispatcher)
{
_workflowDispatcher = workflowDispatcher;
}
///
/// The workflow request.
///
public DispatchWorkflowDefinitionRequest Request { get; set; } = default!;
///
public async Task Execute(IJobExecutionContext context)
{
var map = context.MergedJobDataMap;
var request = new DispatchWorkflowDefinitionRequest
{
DefinitionId = (string)map.Get(nameof(DispatchWorkflowDefinitionRequest.DefinitionId)),
VersionOptions = VersionOptions.FromString((string)map.Get(nameof(DispatchWorkflowDefinitionRequest.VersionOptions))),
TriggerActivityId = (string?)map.Get(nameof(DispatchWorkflowDefinitionRequest.TriggerActivityId)),
InstanceId = (string?)map.Get(nameof(DispatchWorkflowDefinitionRequest.InstanceId)),
CorrelationId = (string?)map.Get(nameof(DispatchWorkflowDefinitionRequest.CorrelationId)),
Input = map.GetDictionary(nameof(DispatchWorkflowDefinitionRequest.Input))
};
await _workflowDispatcher.DispatchAsync(request, cancellationToken: context.CancellationToken);
}
}