< Summary

Information
Class: Elsa.Workflows.Runtime.Activities.RunTask
Assembly: Elsa.Workflows.Runtime
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Activities/RunTask.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 117
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
get_TaskName()100%210%
get_Payload()100%210%
get_TaskId()100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
ExecuteAsync()100%210%
ResumeAsync()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Activities/RunTask.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using System.Text.Json.Serialization;
 3using Elsa.Expressions.Models;
 4using Elsa.Extensions;
 5using Elsa.Workflows.Attributes;
 6using Elsa.Workflows.Memory;
 7using Elsa.Workflows.Models;
 8using Elsa.Workflows.Runtime.Notifications;
 9using Elsa.Workflows.Runtime.Stimuli;
 10using JetBrains.Annotations;
 11
 12namespace Elsa.Workflows.Runtime.Activities;
 13
 14/// <summary>
 15/// Notifies the application that a task with a given name is requested to start.
 16/// When the application fulfilled the task, it is expected to report back to the workflow engine in order to resume the
 17/// </summary>
 18[Activity("Elsa", "Primitives", "Requests a given task to be run. ", Kind = ActivityKind.Action)]
 19[UsedImplicitly]
 20public class RunTask : Activity<object>
 21{
 022    private static readonly object BookmarkPropertyKey = new();
 23
 24    /// <summary>
 25    /// The key that is used for sending and receiving activity input.
 26    /// </summary>
 27    public const string InputKey = "RunTaskInput";
 28
 29    /// <summary>
 30    /// The name of the task being requested.
 31    /// </summary>
 32    [Input(Description = "The name of the task being requested.")]
 033    public Input<string> TaskName { get; set; } = null!;
 34
 35    /// <summary>
 36    /// The name of the task being requested.
 37    /// </summary>
 38    [Input(Description = "Any additional parameters to send to the task.")]
 039    public Input<IDictionary<string, object>?> Payload { get; set; } = null!;
 40
 41    /// <summary>
 42    /// The ID of the task that was requested to run.
 43    /// </summary>
 44    [Output(Description = "The ID of the task that was requested to run.")]
 045    public Output<string> TaskId { get; set; } = null!;
 46
 47    /// <inheritdoc />
 48    [JsonConstructor]
 049    private RunTask(string? source = null, int? line = null) : base(source, line)
 50    {
 051    }
 52
 53    /// <inheritdoc />
 054    public RunTask(MemoryBlockReference output, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = n
 55    {
 056    }
 57
 58    /// <inheritdoc />
 059    public RunTask(Output<object>? output, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) 
 60    {
 061    }
 62
 63    /// <inheritdoc />
 064    public RunTask(string taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this(
 65    {
 066    }
 67
 68    /// <inheritdoc />
 69    public RunTask(Func<string> taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null)
 070        : this(new Input<string>(Expression.DelegateExpression(taskName), new MemoryBlockReference()), source, line)
 71    {
 072    }
 73
 74    /// <inheritdoc />
 75    public RunTask(Func<ExpressionExecutionContext, string?> taskName, [CallerFilePath] string? source = null, [CallerLi
 076        : this(new Input<string>(Expression.DelegateExpression(taskName), new MemoryBlockReference()), source, line)
 77    {
 078    }
 79
 80    /// <inheritdoc />
 081    public RunTask(Variable<string> taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = nul
 82
 83    /// <inheritdoc />
 084    public RunTask(Literal<string> taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null
 85
 86    /// <inheritdoc />
 087    public RunTask(Input<string> taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) 
 88
 89    /// <inheritdoc />
 90    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
 91    {
 92        // Create bookmark.
 093        var taskName = TaskName.Get(context);
 094        var identityGenerator = context.GetRequiredService<IIdentityGenerator>();
 095        var taskId = identityGenerator.GenerateId();
 096        var stimulus = new RunTaskStimulus(taskId, taskName);
 097        context.CreateBookmark(stimulus, ResumeAsync, includeActivityInstanceId: false);
 98
 99        // Dispatch task request.
 0100        var taskParams = Payload.GetOrDefault(context);
 0101        var runTaskRequest = new RunTaskRequest(context, taskId, taskName, taskParams);
 0102        var dispatcher = context.GetRequiredService<ITaskDispatcher>();
 103
 104        // Set the task ID output.
 0105        TaskId.Set(context, taskId);
 106
 107        // Dispatch the task request.
 0108        await dispatcher.DispatchAsync(runTaskRequest, context.CancellationToken);
 0109    }
 110
 111    private async ValueTask ResumeAsync(ActivityExecutionContext context)
 112    {
 0113        var input = context.GetWorkflowInput<object>(InputKey);
 0114        context.Set(Result, input);
 0115        await context.CompleteActivityAsync();
 0116    }
 117}