| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Expressions.Contracts; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows.Activities; |
| | | 7 | | using Elsa.Workflows.LogPersistence; |
| | | 8 | | using Elsa.Workflows.LogPersistence.Strategies; |
| | | 9 | | using Elsa.Workflows.Management.Options; |
| | | 10 | | using Elsa.Workflows.Models; |
| | | 11 | | using Elsa.Workflows.Serialization.Converters; |
| | | 12 | | using Humanizer; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | using Microsoft.Extensions.Options; |
| | | 15 | | |
| | | 16 | | namespace Elsa.Workflows.Runtime; |
| | | 17 | | |
| | | 18 | | /* The following legacy JSON structure is expected to be found in the custom properties of the workflow and activity: |
| | | 19 | | * { |
| | | 20 | | * "logPersistenceMode": { |
| | | 21 | | * "default": "default", |
| | | 22 | | * "inputs": { k : v }, |
| | | 23 | | * "outputs": { k: v } |
| | | 24 | | * } |
| | | 25 | | * } |
| | | 26 | | */ |
| | | 27 | | |
| | | 28 | | /* The following JSON structure is expected to be found in the custom properties of the workflow and activity: |
| | | 29 | | * { |
| | | 30 | | * "logPersistenceConfig": { |
| | | 31 | | * "default": { "evaluationMode": "Strategy", "strategyType": "Elsa.Workflows.LogPersistence.Strategies.Inherit |
| | | 32 | | * "inputs": { "input1" : { "evaluationMode": "Strategy", "strategyType": "Elsa.Workflows.LogPersistence.Strate |
| | | 33 | | * "outputs": { "output1" : { "evaluationMode": "Strategy", "strategyType": "Elsa.Workflows.LogPersistence.Stra |
| | | 34 | | * "internalState": { "evaluationMode": "Strategy", "strategyType": "Elsa.Workflows.LogPersistence.Strategies.I |
| | | 35 | | * } |
| | | 36 | | * } |
| | | 37 | | */ |
| | | 38 | | public class ActivityPropertyLogPersistenceEvaluator : IActivityPropertyLogPersistenceEvaluator |
| | | 39 | | { |
| | | 40 | | private readonly IExpressionEvaluator _expressionEvaluator; |
| | | 41 | | private readonly JsonSerializerOptions _jsonOptions; |
| | | 42 | | private readonly IDictionary<string, ILogPersistenceStrategy> _strategies; |
| | | 43 | | private readonly IOptions<ManagementOptions> _options; |
| | | 44 | | private readonly ILogger _logger; |
| | | 45 | | |
| | | 46 | | private const string LegacyKey = "logPersistenceMode"; |
| | | 47 | | const string ConfigKey = "logPersistenceConfig"; |
| | | 48 | | |
| | 470 | 49 | | public ActivityPropertyLogPersistenceEvaluator( |
| | 470 | 50 | | ILogPersistenceStrategyService strategyService, |
| | 470 | 51 | | IExpressionDescriptorRegistry expressionDescriptorRegistry, |
| | 470 | 52 | | IExpressionEvaluator expressionEvaluator, |
| | 470 | 53 | | IOptions<ManagementOptions> options, |
| | 470 | 54 | | ILogger<ActivityPropertyLogPersistenceEvaluator> logger) |
| | | 55 | | { |
| | 470 | 56 | | _expressionEvaluator = expressionEvaluator; |
| | 470 | 57 | | _options = options; |
| | 470 | 58 | | _logger = logger; |
| | 4230 | 59 | | _strategies = strategyService.ListStrategies().ToDictionary(x => x.GetType().GetSimpleAssemblyQualifiedName(), x |
| | 470 | 60 | | _jsonOptions = new JsonSerializerOptions |
| | 470 | 61 | | { |
| | 470 | 62 | | PropertyNameCaseInsensitive = true |
| | 470 | 63 | | }.WithConverters( |
| | 470 | 64 | | new ExpressionJsonConverterFactory(expressionDescriptorRegistry), |
| | 470 | 65 | | new JsonStringEnumConverter(), |
| | 470 | 66 | | new ExpandoObjectConverterFactory()); |
| | 470 | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task<ActivityLogPersistenceModeMap> EvaluateLogPersistenceModesAsync(ActivityExecutionContext context) |
| | | 70 | | { |
| | 3751 | 71 | | var cancellationToken = context.CancellationToken; |
| | 3751 | 72 | | var (legacyProps, configProps, defaultMode) = await GetPersistenceDefaultsAsync(context, cancellationToken); |
| | 3751 | 73 | | var map = new ActivityLogPersistenceModeMap(); |
| | | 74 | | |
| | 3751 | 75 | | await EvaluatePropertiesAsync(context, "inputs", context.ActivityDescriptor.Inputs, legacyProps, configProps, de |
| | 3751 | 76 | | await EvaluatePropertiesAsync(context, "outputs", context.ActivityDescriptor.Outputs, legacyProps, configProps, |
| | 3751 | 77 | | map.InternalState = await EvaluateInternalStateModeAsync(context.ExpressionExecutionContext, context.Activity.Cu |
| | | 78 | | |
| | 3751 | 79 | | return map; |
| | 3751 | 80 | | } |
| | | 81 | | |
| | | 82 | | public async Task<Dictionary<string, object>> GetPersistableOutputAsync(ActivityExecutionContext context) |
| | | 83 | | { |
| | 3 | 84 | | var cancellationToken = context.WorkflowExecutionContext.CancellationToken; |
| | 3 | 85 | | var (legacyProps, configProps, defaultMode) = await GetPersistenceDefaultsAsync(context, cancellationToken); |
| | 3 | 86 | | var outputs = context.GetOutputs(); |
| | 3 | 87 | | return await GetPersistablePropertiesAsync(context, outputs, "outputs", legacyProps, configProps, defaultMode, c |
| | 3 | 88 | | } |
| | | 89 | | |
| | | 90 | | private async Task<(IDictionary<string, object> legacyProps, IDictionary<string, object> configProps, LogPersistence |
| | | 91 | | { |
| | 7498 | 92 | | var legacyProps = context.Activity.CustomProperties.GetValueOrDefault<IDictionary<string, object>>(LegacyKey, () |
| | 8106 | 93 | | var rootContext = context.WorkflowExecutionContext.ActivityExecutionContexts.First(x => x.ParentActivityExecutio |
| | 10807 | 94 | | var workflow = (Workflow?)context.GetAncestors().FirstOrDefault(x => x.Activity is Workflow)?.Activity ?? contex |
| | 9941 | 95 | | var workflowDefault = await GetDefaultPersistenceModeAsync(rootContext.ExpressionExecutionContext, workflow.Cust |
| | 9940 | 96 | | var activityDefault = await GetDefaultPersistenceModeAsync(context.ExpressionExecutionContext, context.Activity. |
| | 7506 | 97 | | var configProps = context.Activity.CustomProperties.GetValueOrDefault<IDictionary<string, object>>(ConfigKey, () |
| | 3754 | 98 | | return (legacyProps, configProps, activityDefault); |
| | 3754 | 99 | | } |
| | | 100 | | |
| | | 101 | | private async Task EvaluatePropertiesAsync( |
| | | 102 | | ActivityExecutionContext context, |
| | | 103 | | string key, |
| | | 104 | | IEnumerable<PropertyDescriptor> descriptors, |
| | | 105 | | IDictionary<string, object> legacyConfig, |
| | | 106 | | IDictionary<string, object> currentConfig, |
| | | 107 | | LogPersistenceMode defaultMode, |
| | | 108 | | IDictionary<string, LogPersistenceMode> resultMap, |
| | | 109 | | CancellationToken cancellationToken) |
| | | 110 | | { |
| | 14988 | 111 | | var legacySection = legacyConfig.GetValueOrDefault(key, () => new Dictionary<string, object>())!; |
| | 15004 | 112 | | var currentSection = currentConfig.GetValueOrDefault(key, () => new Dictionary<string, object>())!; |
| | | 113 | | |
| | 29538 | 114 | | foreach (var descriptor in descriptors) |
| | | 115 | | { |
| | 7267 | 116 | | resultMap[descriptor.Name] = await EvaluatePropertyModeAsync(context.ExpressionExecutionContext, descriptor, |
| | | 117 | | } |
| | 7502 | 118 | | } |
| | | 119 | | |
| | | 120 | | private async Task<LogPersistenceMode> EvaluatePropertyModeAsync( |
| | | 121 | | ExpressionExecutionContext executionContext, |
| | | 122 | | PropertyDescriptor descriptor, |
| | | 123 | | IDictionary<string, object> legacySection, |
| | | 124 | | IDictionary<string, object> currentSection, |
| | | 125 | | LogPersistenceMode defaultMode, |
| | | 126 | | CancellationToken cancellationToken) |
| | | 127 | | { |
| | 7267 | 128 | | var key = descriptor.Name.Camelize(); |
| | 14534 | 129 | | var configObject = currentSection.GetValueOrDefault(key, () => null); |
| | 7267 | 130 | | var config = ConvertToConfig(configObject); |
| | 7267 | 131 | | if (config != null) |
| | 0 | 132 | | return await EvaluateConfigAsync(config, executionContext, () => defaultMode, cancellationToken); |
| | | 133 | | |
| | 14524 | 134 | | var mode = legacySection.GetValueOrDefault(key, () => defaultMode); |
| | 13201 | 135 | | return ResolveMode(mode, () => defaultMode); |
| | 7267 | 136 | | } |
| | | 137 | | |
| | | 138 | | private async Task<LogPersistenceMode> EvaluateInternalStateModeAsync( |
| | | 139 | | ExpressionExecutionContext executionContext, |
| | | 140 | | IDictionary<string, object> currentConfig, |
| | | 141 | | LogPersistenceMode defaultMode, |
| | | 142 | | CancellationToken cancellationToken) |
| | | 143 | | { |
| | 7502 | 144 | | var configObject = currentConfig.GetValueOrDefault("internalState", () => new Dictionary<string, object>())!; |
| | 3751 | 145 | | var config = ConvertToConfig(configObject); |
| | 11253 | 146 | | if (config != null) return await EvaluateConfigAsync(config, executionContext, () => defaultMode, cancellationTo |
| | 0 | 147 | | return LogPersistenceMode.Inherit; |
| | 3751 | 148 | | } |
| | | 149 | | |
| | | 150 | | private async Task<Dictionary<string, object>> GetPersistablePropertiesAsync( |
| | | 151 | | ActivityExecutionContext context, |
| | | 152 | | IDictionary<string, object> state, |
| | | 153 | | string key, |
| | | 154 | | IDictionary<string, object> legacyConfig, |
| | | 155 | | IDictionary<string, object> currentConfig, |
| | | 156 | | LogPersistenceMode defaultMode, |
| | | 157 | | CancellationToken cancellationToken) |
| | | 158 | | { |
| | 3 | 159 | | var result = new Dictionary<string, object>(); |
| | 6 | 160 | | var legacySection = legacyConfig.GetValueOrDefault(key, () => new Dictionary<string, object>()); |
| | 6 | 161 | | var currentSection = currentConfig.GetValueOrDefault(key, () => new Dictionary<string, object>()); |
| | | 162 | | |
| | 18 | 163 | | foreach (var item in state) |
| | | 164 | | { |
| | 6 | 165 | | var propKey = item.Key.Camelize(); |
| | 12 | 166 | | var configObject = currentSection!.GetValueOrDefault(propKey, () => null); |
| | 6 | 167 | | var config = ConvertToConfig(configObject); |
| | 6 | 168 | | var mode = config != null |
| | 0 | 169 | | ? await EvaluateConfigAsync(config, context.ExpressionExecutionContext, () => defaultMode, cancellationT |
| | 12 | 170 | | : legacySection!.GetValueOrDefault(propKey, () => defaultMode); |
| | | 171 | | |
| | 6 | 172 | | if (mode == LogPersistenceMode.Include || (mode == LogPersistenceMode.Inherit && (defaultMode == LogPersiste |
| | 6 | 173 | | result.Add(item.Key, item.Value); |
| | 6 | 174 | | } |
| | | 175 | | |
| | 3 | 176 | | return result; |
| | 3 | 177 | | } |
| | | 178 | | |
| | | 179 | | private async Task<LogPersistenceMode> GetDefaultPersistenceModeAsync( |
| | | 180 | | ExpressionExecutionContext executionContext, |
| | | 181 | | IDictionary<string, object> properties, |
| | | 182 | | Func<LogPersistenceMode> defaultFactory, |
| | | 183 | | CancellationToken cancellationToken) |
| | | 184 | | { |
| | 14997 | 185 | | var legacyProps = properties.GetValueOrDefault<IDictionary<string, object>>(LegacyKey, () => new Dictionary<stri |
| | 15008 | 186 | | var configProps = properties.GetValueOrDefault<IDictionary<string, object>>(ConfigKey, () => new Dictionary<stri |
| | 7508 | 187 | | var defaultObj = configProps!.TryGetValue("default", out var val) ? val : null; |
| | 7508 | 188 | | if (defaultObj == null) |
| | | 189 | | { |
| | 7500 | 190 | | var legacyDefault = legacyProps!.GetValueOrDefault("default", defaultFactory); |
| | 7500 | 191 | | return legacyDefault == LogPersistenceMode.Inherit ? defaultFactory() : legacyDefault; |
| | | 192 | | } |
| | | 193 | | |
| | 8 | 194 | | var config = ConvertToConfig(defaultObj); |
| | 8 | 195 | | return await EvaluateConfigAsync(config, executionContext, defaultFactory, cancellationToken); |
| | 7508 | 196 | | } |
| | | 197 | | |
| | | 198 | | private async Task<LogPersistenceMode> EvaluateConfigAsync( |
| | | 199 | | LogPersistenceConfiguration? config, |
| | | 200 | | ExpressionExecutionContext executionContext, |
| | | 201 | | Func<LogPersistenceMode> defaultFactory, |
| | | 202 | | CancellationToken cancellationToken) |
| | | 203 | | { |
| | 3759 | 204 | | if (config?.EvaluationMode == LogPersistenceEvaluationMode.Strategy) |
| | | 205 | | { |
| | 3759 | 206 | | var strategyType = config.StrategyType ?? typeof(Inherit).GetSimpleAssemblyQualifiedName(); |
| | 3759 | 207 | | if (!_strategies.TryGetValue(strategyType, out var strategy)) |
| | 0 | 208 | | return defaultFactory(); |
| | | 209 | | |
| | 3759 | 210 | | var strategyContext = new LogPersistenceStrategyContext(cancellationToken); |
| | 3759 | 211 | | var mode = await strategy.GetPersistenceModeAsync(strategyContext); |
| | 3759 | 212 | | return ResolveMode(mode, defaultFactory); |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | if (config?.Expression == null) |
| | 0 | 216 | | return defaultFactory(); |
| | | 217 | | |
| | | 218 | | try |
| | | 219 | | { |
| | 0 | 220 | | var mode = await _expressionEvaluator.EvaluateAsync<LogPersistenceMode>(config.Expression, executionContext) |
| | 0 | 221 | | return ResolveMode(mode, defaultFactory); |
| | | 222 | | } |
| | 0 | 223 | | catch (Exception ex) |
| | | 224 | | { |
| | 0 | 225 | | _logger.LogWarning(ex, "Error evaluating log persistence expression"); |
| | 0 | 226 | | return defaultFactory(); |
| | | 227 | | } |
| | 3759 | 228 | | } |
| | | 229 | | |
| | | 230 | | private LogPersistenceMode ResolveMode(LogPersistenceMode mode, Func<LogPersistenceMode> defaultFactory) |
| | | 231 | | { |
| | 11026 | 232 | | var m = mode == LogPersistenceMode.Inherit ? defaultFactory() : mode; |
| | 11026 | 233 | | return m == LogPersistenceMode.Inherit ? LogPersistenceMode.Include : m; |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | private LogPersistenceConfiguration? ConvertToConfig(object? value) |
| | | 237 | | { |
| | 18305 | 238 | | if (value == null) return null; |
| | 3759 | 239 | | if (value is LogPersistenceConfiguration config) return config; |
| | 3759 | 240 | | var json = JsonSerializer.Serialize(value); |
| | 3759 | 241 | | return JsonSerializer.Deserialize<LogPersistenceConfiguration>(json, _jsonOptions); |
| | | 242 | | } |
| | | 243 | | } |