| | | 1 | | using System.ComponentModel.DataAnnotations; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Nodes; |
| | | 4 | | using Elsa.Expressions.Helpers; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A storage driver that stores objects in the workflow state itself. |
| | | 13 | | /// </summary> |
| | | 14 | | [Display(Name = "Workflow Instance")] |
| | | 15 | | [UsedImplicitly] |
| | 507 | 16 | | public class WorkflowInstanceStorageDriver(IPayloadSerializer payloadSerializer, ILogger<WorkflowInstanceStorageDriver> |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// The key used to store the variables in the workflow state. |
| | | 20 | | /// </summary> |
| | | 21 | | public const string VariablesDictionaryStateKey = "Variables"; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | 0 | 24 | | public double Priority => 5; |
| | | 25 | | /// <inheritdoc /> |
| | 0 | 26 | | public IEnumerable<string> Tags => []; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public ValueTask WriteAsync(string id, object value, StorageDriverContext context) |
| | | 30 | | { |
| | 201 | 31 | | UpdateVariablesDictionary(context, dictionary => |
| | 201 | 32 | | { |
| | 201 | 33 | | try |
| | 201 | 34 | | { |
| | 201 | 35 | | var node = JsonSerializer.SerializeToNode(value, GetSerializationType(value, context), GetSerializerOpti |
| | 199 | 36 | | dictionary[id] = node!; |
| | 199 | 37 | | } |
| | 2 | 38 | | catch (Exception ex) when (ex is JsonException or NotSupportedException or ObjectDisposedException) |
| | 201 | 39 | | { |
| | 2 | 40 | | logger.LogWarning(ex, "Failed to serialize variable '{VariableId}' of type '{VariableType}' for workflow |
| | 2 | 41 | | id, value?.GetType().FullName ?? "null"); |
| | 2 | 42 | | } |
| | 402 | 43 | | }); |
| | 201 | 44 | | return ValueTask.CompletedTask; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public ValueTask<object?> ReadAsync(string id, StorageDriverContext context) |
| | | 49 | | { |
| | 26 | 50 | | var dictionary = GetVariablesDictionary(context); |
| | 26 | 51 | | var node = dictionary.GetValueOrDefault(id); |
| | 26 | 52 | | var variable = context.Variable; |
| | 26 | 53 | | var variableType = variable.GetVariableType(); |
| | 26 | 54 | | var serializerOptions = GetSerializerOptions(); |
| | | 55 | | |
| | 26 | 56 | | if (variableType == typeof(object)) |
| | 7 | 57 | | return new(node?.Deserialize(typeof(object), serializerOptions)); |
| | | 58 | | |
| | 19 | 59 | | var options = new ObjectConverterOptions |
| | 19 | 60 | | { |
| | 19 | 61 | | DeserializeJsonObjectToObject = true, |
| | 19 | 62 | | SerializerOptions = serializerOptions |
| | 19 | 63 | | }; |
| | 19 | 64 | | var result = node.TryConvertTo(variableType, options); |
| | 19 | 65 | | if (result.IsSuccess) |
| | 17 | 66 | | return new(result.Value); |
| | | 67 | | |
| | 2 | 68 | | logger.LogWarning(result.Exception, "Failed to convert stored variable '{VariableId}' to type '{VariableType}'. |
| | 2 | 69 | | id, variableType.FullName); |
| | | 70 | | |
| | 2 | 71 | | if (ObjectConverter.StrictMode) |
| | 1 | 72 | | result.ThrowIfFailure(); |
| | | 73 | | |
| | 1 | 74 | | return new((object?)null); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public ValueTask DeleteAsync(string id, StorageDriverContext context) |
| | | 79 | | { |
| | 246 | 80 | | UpdateVariablesDictionary(context, dictionary => dictionary.Remove(id)); |
| | 123 | 81 | | return ValueTask.CompletedTask; |
| | | 82 | | } |
| | | 83 | | |
| | 227 | 84 | | private JsonSerializerOptions GetSerializerOptions() => payloadSerializer.GetOptions().Clone(); |
| | | 85 | | |
| | | 86 | | private static Type GetSerializationType(object? value, StorageDriverContext context) |
| | | 87 | | { |
| | 201 | 88 | | var declaredType = context.Variable.GetVariableType(); |
| | 201 | 89 | | if (declaredType == typeof(object)) |
| | 80 | 90 | | return typeof(object); |
| | | 91 | | |
| | 121 | 92 | | return value?.GetType() ?? declaredType; |
| | | 93 | | } |
| | | 94 | | |
| | 468 | 95 | | private VariablesDictionary GetVariablesDictionary(StorageDriverContext context) => context.ExecutionContext.Propert |
| | 324 | 96 | | private void SetVariablesDictionary(StorageDriverContext context, VariablesDictionary dictionary) => context.Executi |
| | | 97 | | |
| | | 98 | | private void UpdateVariablesDictionary(StorageDriverContext context, Action<VariablesDictionary> update) |
| | | 99 | | { |
| | 324 | 100 | | var dictionary = GetVariablesDictionary(context); |
| | 324 | 101 | | update(dictionary); |
| | 324 | 102 | | SetVariablesDictionary(context, dictionary); |
| | 324 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public class VariablesDictionary : Dictionary<string, JsonNode>; |