| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows.Behaviors; |
| | | 5 | | using Elsa.Workflows.Helpers; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | using Elsa.Workflows.Serialization.Converters; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Base class for custom activities. |
| | | 14 | | /// </summary> |
| | | 15 | | [DebuggerDisplay("{Type} - {Id}")] |
| | | 16 | | [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors | ImplicitUseTargetFlags.Members)] |
| | | 17 | | public abstract class Activity : IActivity, ISignalHandler |
| | | 18 | | { |
| | 12972 | 19 | | private readonly ICollection<SignalHandlerRegistration> _signalReceivedHandlers = new List<SignalHandlerRegistration |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Constructor. |
| | | 23 | | /// </summary> |
| | 12972 | 24 | | protected Activity(string? source = null, int? line = null) |
| | | 25 | | { |
| | 12972 | 26 | | this.SetSource(source, line); |
| | 12972 | 27 | | Type = ActivityTypeNameHelper.GenerateTypeName(GetType()); |
| | 12972 | 28 | | Version = 1; |
| | 12972 | 29 | | Behaviors.Add<ScheduledChildCallbackBehavior>(this); |
| | 12972 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 0 | 33 | | protected Activity(string activityType, int version = 1, string? source = null, int? line = null) : this(source, lin |
| | | 34 | | { |
| | 0 | 35 | | Type = activityType; |
| | 0 | 36 | | Version = version; |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 170151 | 40 | | public string Id { get; set; } = null!; |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | 23957 | 43 | | public string NodeId { get; set; } = null!; |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 14529 | 46 | | public string? Name { get; set; } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 72555 | 49 | | public string Type { get; set; } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 56357 | 52 | | public int Version { get; set; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// A flag indicating whether this activity can be used for starting a workflow. |
| | | 56 | | /// Usually used for triggers, but also used to disambiguate between two or more starting activities and no starting |
| | | 57 | | /// </summary> |
| | | 58 | | [JsonIgnore] |
| | | 59 | | public bool CanStartWorkflow |
| | | 60 | | { |
| | 6 | 61 | | get => this.GetCanStartWorkflow(); |
| | 68 | 62 | | set => this.SetCanStartWorkflow(value); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// A flag indicating if this activity should execute synchronously or asynchronously. |
| | | 67 | | /// By default, activities with an <see cref="ActivityKind"/> of <see cref="Action"/>, <see cref="Task"/> or <see cr |
| | | 68 | | /// will execute synchronously, while activities of the <see cref="ActivityKind.Job"/> kind will execute asynchronou |
| | | 69 | | /// </summary> |
| | | 70 | | [JsonIgnore] |
| | | 71 | | public bool? RunAsynchronously |
| | | 72 | | { |
| | 6 | 73 | | get => this.GetRunAsynchronously(); |
| | 1061 | 74 | | set => this.SetRunAsynchronously(value); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | [JsonIgnore] |
| | | 78 | | public string? CommitStrategy |
| | | 79 | | { |
| | 6 | 80 | | get => this.GetCommitStrategy(); |
| | 0 | 81 | | set => this.SetCommitStrategy(value); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | [JsonConverter(typeof(PolymorphicObjectConverterFactory))] |
| | 66308 | 86 | | public IDictionary<string, object> CustomProperties { get; set; } = new Dictionary<string, object>(); |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | | 89 | | [JsonIgnore] |
| | 13086 | 90 | | public IDictionary<string, object> SyntheticProperties { get; set; } = new Dictionary<string, object>(); |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | [JsonConverter(typeof(PolymorphicObjectConverterFactory))] |
| | 17439 | 94 | | public IDictionary<string, object> Metadata { get; set; } = new Dictionary<string, object>(); |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// A collection of reusable behaviors to add to this activity. |
| | | 98 | | /// </summary> |
| | | 99 | | [JsonIgnore] |
| | 44132 | 100 | | public ICollection<IBehavior> Behaviors { get; } = new List<IBehavior>(); |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Override this method to return a value indicating whether the activity can execute. |
| | | 104 | | /// </summary> |
| | | 105 | | protected virtual ValueTask<bool> CanExecuteAsync(ActivityExecutionContext context) |
| | | 106 | | { |
| | 3019 | 107 | | var result = CanExecute(context); |
| | 3019 | 108 | | return new(result); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Override this method to return a value indicating whether the activity can execute. |
| | | 113 | | /// </summary> |
| | | 114 | | protected virtual bool CanExecute(ActivityExecutionContext context) |
| | | 115 | | { |
| | 2997 | 116 | | return true; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Override this method to implement activity-specific logic. |
| | | 121 | | /// </summary> |
| | | 122 | | protected virtual ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 123 | | { |
| | 698 | 124 | | Execute(context); |
| | 671 | 125 | | return ValueTask.CompletedTask; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Override this method to implement activity-specific logic. |
| | | 130 | | /// </summary> |
| | | 131 | | protected virtual void Execute(ActivityExecutionContext context) |
| | | 132 | | { |
| | 18 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Override this method to handle any signals sent from downstream activities. |
| | | 137 | | /// </summary> |
| | | 138 | | protected virtual ValueTask OnSignalReceivedAsync(object signal, SignalContext context) |
| | | 139 | | { |
| | 12182 | 140 | | OnSignalReceived(signal, context); |
| | 12182 | 141 | | return ValueTask.CompletedTask; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Override this method to handle any signals sent from downstream activities. |
| | | 146 | | /// </summary> |
| | | 147 | | protected virtual void OnSignalReceived(object signal, SignalContext context) |
| | | 148 | | { |
| | 12182 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Register a signal handler delegate. |
| | | 153 | | /// </summary> |
| | 10774 | 154 | | protected void OnSignalReceived(Type signalType, Func<object, SignalContext, ValueTask> handler) => _signalReceivedH |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Register a signal handler delegate. |
| | | 158 | | /// </summary> |
| | 10776 | 159 | | protected void OnSignalReceived<T>(Func<T, SignalContext, ValueTask> handler) => OnSignalReceived(typeof(T), (signal |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Register a signal handler delegate. |
| | | 163 | | /// </summary> |
| | | 164 | | protected void OnSignalReceived<T>(Action<T, SignalContext> handler) |
| | | 165 | | { |
| | 4035 | 166 | | OnSignalReceived<T>((signal, context) => |
| | 4035 | 167 | | { |
| | 0 | 168 | | handler(signal, context); |
| | 0 | 169 | | return ValueTask.CompletedTask; |
| | 4035 | 170 | | }); |
| | 4035 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Notify the workflow that this activity completed. |
| | | 175 | | /// </summary> |
| | | 176 | | protected async ValueTask CompleteAsync(ActivityExecutionContext context) |
| | | 177 | | { |
| | 0 | 178 | | await context.CompleteActivityAsync(); |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | async ValueTask<bool> IActivity.CanExecuteAsync(ActivityExecutionContext context) |
| | | 182 | | { |
| | 3019 | 183 | | return await CanExecuteAsync(context); |
| | 3019 | 184 | | } |
| | | 185 | | |
| | | 186 | | async ValueTask IActivity.ExecuteAsync(ActivityExecutionContext context) |
| | | 187 | | { |
| | 3366 | 188 | | await ExecuteAsync(context); |
| | | 189 | | |
| | | 190 | | // Invoke behaviors. |
| | 18849 | 191 | | foreach (var behavior in Behaviors) await behavior.ExecuteAsync(context); |
| | 3333 | 192 | | } |
| | | 193 | | |
| | | 194 | | async ValueTask ISignalHandler.ReceiveSignalAsync(object signal, SignalContext context) |
| | | 195 | | { |
| | | 196 | | // Give derived activity a chance to do something with the signal. |
| | 12182 | 197 | | await OnSignalReceivedAsync(signal, context); |
| | | 198 | | |
| | | 199 | | // Invoke registered signal delegates for this particular type of signal. |
| | 12182 | 200 | | var signalType = signal.GetType(); |
| | 30276 | 201 | | var handlers = _signalReceivedHandlers.Where(x => x.SignalType == signalType); |
| | | 202 | | |
| | 24368 | 203 | | foreach (var registration in handlers) |
| | 2 | 204 | | await registration.Handler(signal, context); |
| | | 205 | | |
| | | 206 | | // Invoke behaviors. |
| | 63187 | 207 | | foreach (var behavior in Behaviors) await behavior.ReceiveSignalAsync(signal, context); |
| | 12182 | 208 | | } |
| | | 209 | | } |