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