| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Dynamic; |
| | | 4 | | using System.Reflection; |
| | | 5 | | using System.Text.Json; |
| | | 6 | | using System.Text.Json.Nodes; |
| | | 7 | | using Elsa.Caching.Features; |
| | | 8 | | using Elsa.Common.Codecs; |
| | | 9 | | using Elsa.Common.Features; |
| | | 10 | | using Elsa.Expressions.Contracts; |
| | | 11 | | using Elsa.Extensions; |
| | | 12 | | using Elsa.Features.Abstractions; |
| | | 13 | | using Elsa.Features.Attributes; |
| | | 14 | | using Elsa.Features.Services; |
| | | 15 | | using Elsa.Workflows.Features; |
| | | 16 | | using Elsa.Workflows.LogPersistence; |
| | | 17 | | using Elsa.Workflows.Management.Activities.HostMethod; |
| | | 18 | | using Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity; |
| | | 19 | | using Elsa.Workflows.Management.Contracts; |
| | | 20 | | using Elsa.Workflows.Management.Entities; |
| | | 21 | | using Elsa.Workflows.Management.Handlers.Notifications; |
| | | 22 | | using Elsa.Workflows.Management.Mappers; |
| | | 23 | | using Elsa.Workflows.Management.Materializers; |
| | | 24 | | using Elsa.Workflows.Management.Models; |
| | | 25 | | using Elsa.Workflows.Management.Options; |
| | | 26 | | using Elsa.Workflows.Management.Providers; |
| | | 27 | | using Elsa.Workflows.Management.Services; |
| | | 28 | | using Elsa.Workflows.Management.Stores; |
| | | 29 | | using Elsa.Workflows.Serialization.Serializers; |
| | | 30 | | using JetBrains.Annotations; |
| | | 31 | | using Microsoft.Extensions.DependencyInjection; |
| | | 32 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 33 | | |
| | | 34 | | namespace Elsa.Workflows.Management.Features; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Installs and configures the workflow management feature. |
| | | 38 | | /// </summary> |
| | | 39 | | [DependsOn(typeof(StringCompressionFeature))] |
| | | 40 | | [DependsOn(typeof(MediatorFeature))] |
| | | 41 | | [DependsOn(typeof(MemoryCacheFeature))] |
| | | 42 | | [DependsOn(typeof(SystemClockFeature))] |
| | | 43 | | [DependsOn(typeof(WorkflowsFeature))] |
| | | 44 | | [DependsOn(typeof(WorkflowDefinitionsFeature))] |
| | | 45 | | [DependsOn(typeof(WorkflowInstancesFeature))] |
| | | 46 | | [UsedImplicitly] |
| | 7 | 47 | | public class WorkflowManagementFeature(IModule module) : FeatureBase(module) |
| | | 48 | | { |
| | | 49 | | private const string PrimitivesCategory = "Primitives"; |
| | | 50 | | private const string LookupsCategory = "Lookups"; |
| | | 51 | | private const string DynamicCategory = "Dynamic"; |
| | | 52 | | private const string DataCategory = "Data"; |
| | | 53 | | private const string SystemCategory = "System"; |
| | | 54 | | |
| | 437 | 55 | | private Func<IServiceProvider, IWorkflowDefinitionPublisher> _workflowDefinitionPublisher = sp => ActivatorUtilities |
| | 437 | 56 | | private Func<IServiceProvider, IWorkflowReferenceQuery> _workflowReferenceQuery = sp => ActivatorUtilities.CreateIns |
| | | 57 | | |
| | 14 | 58 | | private string CompressionAlgorithm { get; set; } = nameof(None); |
| | 21 | 59 | | private LogPersistenceMode LogPersistenceMode { get; set; } = LogPersistenceMode.Include; |
| | 14 | 60 | | private bool IsReadOnlyMode { get; set; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// A set of activity types to make available to the system. |
| | | 64 | | /// </summary> |
| | 175 | 65 | | public HashSet<Type> ActivityTypes { get; } = []; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// A set of variable types to make available to the system. |
| | | 69 | | /// </summary> |
| | 14 | 70 | | public HashSet<VariableDescriptor> VariableDescriptors { get; } = |
| | 7 | 71 | | [ |
| | 7 | 72 | | new(typeof(object), PrimitivesCategory, "The root class for all object in the CLR System."), |
| | 7 | 73 | | new(typeof(string), PrimitivesCategory, "Represents a static string of characters."), |
| | 7 | 74 | | new(typeof(bool), PrimitivesCategory, "Represents a true or false value."), |
| | 7 | 75 | | new(typeof(int), PrimitivesCategory, "A 32 bit integer."), |
| | 7 | 76 | | new(typeof(long), PrimitivesCategory, "A 64 bit integer."), |
| | 7 | 77 | | new(typeof(float), PrimitivesCategory, "A 32 bit floating point number."), |
| | 7 | 78 | | new(typeof(double), PrimitivesCategory, "A 64 bit floating point number."), |
| | 7 | 79 | | new(typeof(decimal), PrimitivesCategory, "A decimal number."), |
| | 7 | 80 | | new(typeof(Guid), PrimitivesCategory, "Represents a Globally Unique Identifier."), |
| | 7 | 81 | | new(typeof(DateTime), PrimitivesCategory, "A value type that represents a date and time."), |
| | 7 | 82 | | new(typeof(DateTimeOffset), PrimitivesCategory, "A value type that consists of a DateTime and a time zone offset |
| | 7 | 83 | | new(typeof(TimeSpan), PrimitivesCategory, "Represents a duration of time."), |
| | 7 | 84 | | new(typeof(IDictionary<string, string>), LookupsCategory, "A dictionary with string key and values."), |
| | 7 | 85 | | new(typeof(IDictionary<string, object>), LookupsCategory, "A dictionary with string key and object values."), |
| | 7 | 86 | | new(typeof(ExpandoObject), DynamicCategory, "A dictionary that can be typed as dynamic to access members using d |
| | 7 | 87 | | new(typeof(JsonElement), DynamicCategory, "A JSON element for reading a JSON structure."), |
| | 7 | 88 | | new(typeof(JsonNode), DynamicCategory, "A JSON node for reading and writing a JSON structure."), |
| | 7 | 89 | | new(typeof(JsonObject), DynamicCategory, "A JSON object for reading and writing a JSON structure."), |
| | 7 | 90 | | new(typeof(byte[]), DataCategory, "A byte array."), |
| | 7 | 91 | | new(typeof(Stream), DataCategory, "A stream."), |
| | 7 | 92 | | new(typeof(LogPersistenceMode), SystemCategory, "A LogPersistenceMode enum value.") |
| | 7 | 93 | | ]; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Adds the specified activity type to the system. |
| | | 97 | | /// </summary> |
| | 91 | 98 | | public WorkflowManagementFeature AddActivity<T>() where T : IActivity => AddActivity(typeof(T)); |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Adds the specified activity type to the system. |
| | | 102 | | /// </summary> |
| | | 103 | | public WorkflowManagementFeature AddActivity(Type activityType) |
| | | 104 | | { |
| | 91 | 105 | | ActivityTypes.Add(activityType); |
| | 91 | 106 | | return this; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Adds all types implementing <see cref="IActivity"/> to the system. |
| | | 111 | | /// </summary> |
| | | 112 | | public WorkflowManagementFeature AddActivitiesFrom<TMarker>() |
| | | 113 | | { |
| | 63 | 114 | | var activityTypes = typeof(TMarker).Assembly.GetExportedTypes() |
| | 9380 | 115 | | .Where(x => typeof(IActivity).IsAssignableFrom(x) && x is { IsAbstract: false, IsInterface: false, IsGeneric |
| | 63 | 116 | | .ToList(); |
| | 63 | 117 | | return AddActivities(activityTypes); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Adds the specified activity types to the system. |
| | | 122 | | /// </summary> |
| | | 123 | | public WorkflowManagementFeature AddActivities(IEnumerable<Type> activityTypes) |
| | | 124 | | { |
| | 63 | 125 | | ActivityTypes.AddRange(activityTypes); |
| | 63 | 126 | | return this; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Removes the specified activity type from the system. |
| | | 131 | | /// </summary> |
| | 7 | 132 | | public WorkflowManagementFeature RemoveActivity<T>() where T : IActivity => RemoveActivity(typeof(T)); |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Adds the specified activity type to the system. |
| | | 136 | | /// </summary> |
| | | 137 | | public WorkflowManagementFeature RemoveActivity(Type activityType) |
| | | 138 | | { |
| | 7 | 139 | | ActivityTypes.Remove(activityType); |
| | 7 | 140 | | return this; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Configures the system to add a specific activity host type to the workflow management feature. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <typeparam name="T">The type of the activity host to be added.</typeparam> |
| | | 147 | | /// <param name="key">An optional unique key to associate with the activity host type.</param> |
| | | 148 | | public WorkflowManagementFeature AddActivityHost<T>(string? key = null) where T : class |
| | | 149 | | { |
| | 28 | 150 | | Module.Services.Configure<HostMethodActivitiesOptions>(options => options.AddType<T>(key)); |
| | 14 | 151 | | return this; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Configures the system to add a specific activity host type to the workflow management feature. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="hostType">The type of the activity host to be added.</param> |
| | | 158 | | /// <param name="key">An optional unique key to associate with the activity host type.</param> |
| | | 159 | | public WorkflowManagementFeature AddActivityHost(Type hostType, string? key = null) |
| | | 160 | | { |
| | 0 | 161 | | Module.Services.Configure<HostMethodActivitiesOptions>(options => options.AddType(hostType, key)); |
| | 0 | 162 | | return this; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Adds the specified variable type to the system. |
| | | 167 | | /// </summary> |
| | 0 | 168 | | public WorkflowManagementFeature AddVariableType<T>(string category) => AddVariableType(typeof(T), category); |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Adds the specified variable type to the system. |
| | | 172 | | /// </summary> |
| | 0 | 173 | | public WorkflowManagementFeature AddVariableType(Type type, string category) => AddVariableTypes([type], category); |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Adds the specified variable types to the system. |
| | | 177 | | /// </summary> |
| | | 178 | | public WorkflowManagementFeature AddVariableTypes(IEnumerable<Type> types, string category) => |
| | 63 | 179 | | AddVariableTypes(types.Select(x => new VariableDescriptor(x, category, x.GetCustomAttribute<DescriptionAttribute |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Adds the specified variable types to the system. |
| | | 183 | | /// </summary> |
| | | 184 | | public WorkflowManagementFeature AddVariableTypes(IEnumerable<VariableDescriptor> descriptors) |
| | | 185 | | { |
| | 7 | 186 | | VariableDescriptors.AddRange(descriptors); |
| | 7 | 187 | | return this; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Sets the compression algorithm to use for compressing workflow state. |
| | | 192 | | /// </summary> |
| | | 193 | | public WorkflowManagementFeature SetCompressionAlgorithm(string algorithm) |
| | | 194 | | { |
| | 0 | 195 | | CompressionAlgorithm = algorithm; |
| | 0 | 196 | | return this; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Set the default Log Persistence mode to use for worflow state (default is Include) |
| | | 201 | | /// </summary> |
| | | 202 | | /// <param name="logPersistenceMode">The mode persistence value</param> |
| | | 203 | | public WorkflowManagementFeature SetDefaultLogPersistenceMode(LogPersistenceMode logPersistenceMode) |
| | | 204 | | { |
| | 7 | 205 | | LogPersistenceMode = logPersistenceMode; |
| | 7 | 206 | | return this; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Enables or disables read-only mode for resources such as workflow definitions. |
| | | 211 | | /// </summary> |
| | | 212 | | /// <returns></returns> |
| | | 213 | | public WorkflowManagementFeature UseReadOnlyMode(bool enabled) |
| | | 214 | | { |
| | 7 | 215 | | IsReadOnlyMode = enabled; |
| | 7 | 216 | | return this; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | public WorkflowManagementFeature UseWorkflowDefinitionPublisher(Func<IServiceProvider, IWorkflowDefinitionPublisher> |
| | | 220 | | { |
| | 0 | 221 | | _workflowDefinitionPublisher = workflowDefinitionPublisher; |
| | 0 | 222 | | return this; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | public WorkflowManagementFeature UseWorkflowReferenceFinder<T>() where T : class, IWorkflowReferenceQuery |
| | | 226 | | { |
| | 0 | 227 | | Services.TryAddScoped<T>(); |
| | 0 | 228 | | return UseWorkflowReferenceFinder(sp => sp.GetRequiredService<T>()); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | public WorkflowManagementFeature UseWorkflowReferenceFinder(Func<IServiceProvider, IWorkflowReferenceQuery> workflow |
| | | 232 | | { |
| | 0 | 233 | | _workflowReferenceQuery = workflowReferenceFinder; |
| | 0 | 234 | | return this; |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <inheritdoc /> |
| | | 238 | | [RequiresUnreferencedCode("The assembly containing the specified marker type will be scanned for activity types.")] |
| | | 239 | | public override void Configure() |
| | | 240 | | { |
| | 7 | 241 | | AddActivitiesFrom<WorkflowManagementFeature>(); |
| | 7 | 242 | | } |
| | | 243 | | |
| | | 244 | | /// <inheritdoc /> |
| | | 245 | | public override void Apply() |
| | | 246 | | { |
| | 7 | 247 | | Services |
| | 7 | 248 | | .AddMemoryStore<WorkflowDefinition, MemoryWorkflowDefinitionStore>() |
| | 7 | 249 | | .AddMemoryStore<WorkflowInstance, MemoryWorkflowInstanceStore>() |
| | 7 | 250 | | .AddActivityProvider<TypedActivityProvider>() |
| | 7 | 251 | | .AddActivityProvider<WorkflowDefinitionActivityProvider>() |
| | 7 | 252 | | .AddActivityProvider<HostMethodActivityProvider>() |
| | 7 | 253 | | .AddScoped<IHostMethodActivityDescriber, HostMethodActivityDescriber>() |
| | 7 | 254 | | .AddScoped<IHostMethodParameterValueProvider, DefaultHostMethodParameterValueProvider>() |
| | 7 | 255 | | .AddScoped<WorkflowDefinitionActivityDescriptorFactory>() |
| | 7 | 256 | | .AddScoped<WorkflowDefinitionActivityProvider>() |
| | 7 | 257 | | .AddScoped<IWorkflowDefinitionActivityRegistryUpdater, WorkflowDefinitionActivityRegistryUpdater>() |
| | 7 | 258 | | .AddScoped<IWorkflowDefinitionService, WorkflowDefinitionService>() |
| | 7 | 259 | | .AddScoped<IWorkflowSerializer, WorkflowSerializer>() |
| | 7 | 260 | | .AddScoped<IWorkflowValidator, WorkflowValidator>() |
| | 7 | 261 | | .AddScoped(_workflowReferenceQuery) |
| | 7 | 262 | | .AddScoped(_workflowDefinitionPublisher) |
| | 7 | 263 | | .AddScoped<IWorkflowDefinitionImporter, WorkflowDefinitionImporter>() |
| | 7 | 264 | | .AddScoped<IWorkflowDefinitionManager, WorkflowDefinitionManager>() |
| | 7 | 265 | | .AddScoped<IWorkflowInstanceManager, WorkflowInstanceManager>() |
| | 7 | 266 | | .AddScoped<IWorkflowReferenceUpdater, WorkflowReferenceUpdater>() |
| | 7 | 267 | | .AddScoped<IActivityRegistryPopulator, ActivityRegistryPopulator>() |
| | 7 | 268 | | .AddSingleton<IExpressionDescriptorRegistry, ExpressionDescriptorRegistry>() |
| | 7 | 269 | | .AddSingleton<IExpressionDescriptorProvider, DefaultExpressionDescriptorProvider>() |
| | 7 | 270 | | .AddSerializationOptionsConfigurator<SerializationOptionsConfigurator>() |
| | 7 | 271 | | .AddScoped<IWorkflowMaterializer, TypedWorkflowMaterializer>() |
| | 7 | 272 | | .AddScoped<IWorkflowMaterializer, ClrWorkflowMaterializer>() |
| | 7 | 273 | | .AddScoped<IWorkflowMaterializer, JsonWorkflowMaterializer>() |
| | 7 | 274 | | .AddScoped<IActivityResolver, WorkflowDefinitionActivityResolver>() |
| | 7 | 275 | | .AddScoped<IWorkflowInstanceVariableManager, WorkflowInstanceVariableManager>() |
| | 7 | 276 | | .AddScoped<WorkflowDefinitionMapper>() |
| | 7 | 277 | | .AddSingleton<VariableDefinitionMapper>() |
| | 7 | 278 | | .AddSingleton<WorkflowStateMapper>() |
| | 7 | 279 | | ; |
| | | 280 | | |
| | 7 | 281 | | Services |
| | 7 | 282 | | .AddNotificationHandler<DeleteWorkflowInstances>() |
| | 7 | 283 | | .AddNotificationHandler<RefreshActivityRegistry>() |
| | 7 | 284 | | .AddNotificationHandler<UpdateConsumingWorkflows>() |
| | 7 | 285 | | .AddNotificationHandler<ValidateWorkflow>() |
| | 7 | 286 | | ; |
| | | 287 | | |
| | 7 | 288 | | Services.Configure<ManagementOptions>(options => |
| | 7 | 289 | | { |
| | 798 | 290 | | foreach (var activityType in ActivityTypes.Distinct()) |
| | 392 | 291 | | options.ActivityTypes.Add(activityType); |
| | 7 | 292 | | |
| | 623 | 293 | | foreach (var descriptor in VariableDescriptors.DistinctBy(x => x.Type)) |
| | 203 | 294 | | options.VariableDescriptors.Add(descriptor); |
| | 7 | 295 | | |
| | 7 | 296 | | options.CompressionAlgorithm = CompressionAlgorithm; |
| | 7 | 297 | | options.LogPersistenceMode = LogPersistenceMode; |
| | 7 | 298 | | options.IsReadOnlyMode = IsReadOnlyMode; |
| | 14 | 299 | | }); |
| | | 300 | | |
| | 14 | 301 | | Services.Configure<HostMethodActivitiesOptions>(_ => { }); |
| | 7 | 302 | | } |
| | | 303 | | } |