| | | 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] |
| | 421 | 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 | | { |
| | 87 | 31 | | UpdateVariablesDictionary(context, dictionary => |
| | 87 | 32 | | { |
| | 87 | 33 | | try |
| | 87 | 34 | | { |
| | 87 | 35 | | var node = JsonSerializer.SerializeToNode(value); |
| | 87 | 36 | | dictionary[id] = node!; |
| | 87 | 37 | | } |
| | 0 | 38 | | catch (Exception ex) when (ex is JsonException or NotSupportedException or ObjectDisposedException) |
| | 87 | 39 | | { |
| | 0 | 40 | | logger.LogWarning(ex, "Failed to serialize variable '{VariableId}' of type '{VariableType}' for workflow |
| | 0 | 41 | | id, value?.GetType().FullName ?? "null"); |
| | 87 | 42 | | |
| | 0 | 43 | | dictionary.Remove(id); |
| | 0 | 44 | | } |
| | 174 | 45 | | }); |
| | 87 | 46 | | return ValueTask.CompletedTask; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public ValueTask<object?> ReadAsync(string id, StorageDriverContext context) |
| | | 51 | | { |
| | 15 | 52 | | var dictionary = GetVariablesDictionary(context); |
| | 15 | 53 | | var node = dictionary.GetValueOrDefault(id); |
| | 15 | 54 | | var variable = context.Variable; |
| | 15 | 55 | | var variableType = variable.GetVariableType(); |
| | 15 | 56 | | var options = new ObjectConverterOptions |
| | 15 | 57 | | { |
| | 15 | 58 | | DeserializeJsonObjectToObject = true, |
| | 15 | 59 | | SerializerOptions = payloadSerializer.GetOptions() |
| | 15 | 60 | | }; |
| | 15 | 61 | | var result = node.TryConvertTo(variableType, options); |
| | 15 | 62 | | var parsedValue = result.Success ? result.Value : node; |
| | 15 | 63 | | return new (parsedValue); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public ValueTask DeleteAsync(string id, StorageDriverContext context) |
| | | 68 | | { |
| | 62 | 69 | | UpdateVariablesDictionary(context, dictionary => dictionary.Remove(id)); |
| | 31 | 70 | | return ValueTask.CompletedTask; |
| | | 71 | | } |
| | | 72 | | |
| | 188 | 73 | | private VariablesDictionary GetVariablesDictionary(StorageDriverContext context) => context.ExecutionContext.Propert |
| | 118 | 74 | | private void SetVariablesDictionary(StorageDriverContext context, VariablesDictionary dictionary) => context.Executi |
| | | 75 | | |
| | | 76 | | private void UpdateVariablesDictionary(StorageDriverContext context, Action<VariablesDictionary> update) |
| | | 77 | | { |
| | 118 | 78 | | var dictionary = GetVariablesDictionary(context); |
| | 118 | 79 | | update(dictionary); |
| | 118 | 80 | | SetVariablesDictionary(context, dictionary); |
| | 118 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public class VariablesDictionary : Dictionary<string, JsonNode>; |