| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Attributes; |
| | | 6 | | using Elsa.Workflows.Memory; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 9 | | using Elsa.Workflows.Runtime.Stimuli; |
| | | 10 | | using JetBrains.Annotations; |
| | | 11 | | |
| | | 12 | | namespace 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] |
| | | 20 | | public class RunTask : Activity<object> |
| | | 21 | | { |
| | 0 | 22 | | 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.")] |
| | 0 | 33 | | 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.")] |
| | 0 | 39 | | 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.")] |
| | 0 | 45 | | public Output<string> TaskId { get; set; } = null!; |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | [JsonConstructor] |
| | 0 | 49 | | private RunTask(string? source = null, int? line = null) : base(source, line) |
| | | 50 | | { |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | 0 | 54 | | public RunTask(MemoryBlockReference output, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = n |
| | | 55 | | { |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | 0 | 59 | | public RunTask(Output<object>? output, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | | 60 | | { |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | 0 | 64 | | public RunTask(string taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this( |
| | | 65 | | { |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public RunTask(Func<string> taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | 0 | 70 | | : this(new Input<string>(Expression.DelegateExpression(taskName), new MemoryBlockReference()), source, line) |
| | | 71 | | { |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | | 75 | | public RunTask(Func<ExpressionExecutionContext, string?> taskName, [CallerFilePath] string? source = null, [CallerLi |
| | 0 | 76 | | : this(new Input<string>(Expression.DelegateExpression(taskName), new MemoryBlockReference()), source, line) |
| | | 77 | | { |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <inheritdoc /> |
| | 0 | 81 | | public RunTask(Variable<string> taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = nul |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | 0 | 84 | | public RunTask(Literal<string> taskName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | 0 | 87 | | 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. |
| | 0 | 93 | | var taskName = TaskName.Get(context); |
| | 0 | 94 | | var identityGenerator = context.GetRequiredService<IIdentityGenerator>(); |
| | 0 | 95 | | var taskId = identityGenerator.GenerateId(); |
| | 0 | 96 | | var stimulus = new RunTaskStimulus(taskId, taskName); |
| | 0 | 97 | | context.CreateBookmark(stimulus, ResumeAsync, includeActivityInstanceId: false); |
| | | 98 | | |
| | | 99 | | // Dispatch task request. |
| | 0 | 100 | | var taskParams = Payload.GetOrDefault(context); |
| | 0 | 101 | | var runTaskRequest = new RunTaskRequest(context, taskId, taskName, taskParams); |
| | 0 | 102 | | var dispatcher = context.GetRequiredService<ITaskDispatcher>(); |
| | | 103 | | |
| | | 104 | | // Set the task ID output. |
| | 0 | 105 | | TaskId.Set(context, taskId); |
| | | 106 | | |
| | | 107 | | // Dispatch the task request. |
| | 0 | 108 | | await dispatcher.DispatchAsync(runTaskRequest, context.CancellationToken); |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | private async ValueTask ResumeAsync(ActivityExecutionContext context) |
| | | 112 | | { |
| | 0 | 113 | | var input = context.GetWorkflowInput<object>(InputKey); |
| | 0 | 114 | | context.Set(Result, input); |
| | 0 | 115 | | await context.CompleteActivityAsync(); |
| | 0 | 116 | | } |
| | | 117 | | } |