< Summary

Information
Class: Elsa.Workflows.VariablePersistenceManager
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/VariablePersistenceManager.cs
Line coverage
83%
Covered lines: 66
Uncovered lines: 13
Coverable lines: 79
Total lines: 153
Line coverage: 83.5%
Branch coverage
70%
Covered branches: 31
Total branches: 44
Branch coverage: 70.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
LoadVariablesAsync()66.66%211880%
SaveVariablesAsync()100%88100%
DeleteVariablesAsync()83.33%1212100%
DeleteVariablesAsync()0%2040%
GetLocalVariables(...)100%11100%
EnsureBlock(...)50%2266.66%
GetStateId(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/VariablePersistenceManager.cs

#LineLine coverage
 1using Elsa.Expressions.Helpers;
 2using Elsa.Expressions.Models;
 3using Elsa.Extensions;
 4using Elsa.Workflows.Memory;
 5using Microsoft.Extensions.Logging;
 6
 7namespace Elsa.Workflows;
 8
 9/// <inheritdoc />
 47210public class VariablePersistenceManager(IStorageDriverManager storageDriverManager, ILogger<VariablePersistenceManager> 
 11{
 12    /// <inheritdoc />
 13    public async Task LoadVariablesAsync(WorkflowExecutionContext workflowExecutionContext, IEnumerable<string>? exclude
 14    {
 49315        var cancellationToken = workflowExecutionContext.CancellationToken;
 49316        var contexts = workflowExecutionContext.ActivityExecutionContexts.ToList();
 49317        var excludeTagsList = excludeTags?.ToList();
 18
 125419        foreach (var context in contexts)
 20        {
 13421            var variables = GetLocalVariables(context).ToList();
 22
 31023            foreach (var variable in variables)
 24            {
 2125                context.ExpressionExecutionContext.Memory.Declare(variable);
 2126                var storageDriverContext = new StorageDriverContext(context, variable, cancellationToken);
 2127                var register = context.ExpressionExecutionContext.Memory;
 2128                var block = EnsureBlock(register, variable);
 2129                var metadata = (VariableBlockMetadata)block.Metadata!;
 2130                var driver = storageDriverManager.Get(metadata.StorageDriverType!);
 31
 2132                block.Metadata = metadata with
 2133                {
 2134                    IsInitialized = true
 2135                };
 36
 2137                if (driver == null)
 38                    continue;
 39
 1540                if (excludeTagsList != null && driver.Tags.Any(excludeTagsList.Contains))
 41                    continue;
 42
 1543                var id = GetStateId(variable);
 44
 45                try
 46                {
 1547                    var value = await driver.ReadAsync(id, storageDriverContext);
 1548                    if (value == null) continue;
 49
 1550                    register.Declare(variable);
 51
 1552                    if (!variable.TryParseValue(value, out var parsedValue))
 53                    {
 054                        logger.LogWarning("Failed to parse value for variable {VariableId} of type {VariableType} with v
 55
 056                        if (!ObjectConverter.StrictMode)
 057                            variable.Set(register, value);
 058                        continue;
 59                    }
 60
 1561                    variable.Set(register, parsedValue);
 1562                }
 063                catch (Exception e)
 64                {
 065                    logger.LogError(e, "Failed to read variable {VariableId} from storage driver {StorageDriverType}", v
 066                }
 1567            }
 13468        }
 49369    }
 70
 71    /// <inheritdoc />
 72    public async Task SaveVariablesAsync(WorkflowExecutionContext workflowExecutionContext)
 73    {
 48774        var cancellationToken = workflowExecutionContext.CancellationToken;
 48775        var contexts = workflowExecutionContext.ActivityExecutionContexts.ToList();
 76
 759477        foreach (var context in contexts)
 78        {
 331079            var variables = GetLocalVariables(context).ToList();
 80
 699281            foreach (var variable in variables)
 82            {
 18683                var block = variable.GetBlock(context.ExpressionExecutionContext);
 18684                var metadata = (VariableBlockMetadata)block.Metadata!;
 18685                var driver = storageDriverManager.Get(metadata.StorageDriverType!);
 86
 18687                if (driver == null)
 88                    continue;
 89
 15190                var id = GetStateId(variable);
 15191                var value = block.Value;
 15192                var storageDriverContext = new StorageDriverContext(context, variable, cancellationToken);
 93
 15194                if (value == null)
 895                    await driver.DeleteAsync(id, storageDriverContext);
 96                else
 14397                    await driver.WriteAsync(id, value, storageDriverContext);
 98            }
 331099        }
 487100    }
 101
 102    /// <inheritdoc />
 103    public async Task DeleteVariablesAsync(ActivityExecutionContext context, IEnumerable<string>? includeTags = default)
 104    {
 2695105        var register = context.ExpressionExecutionContext.Memory;
 2695106        var variableList = GetLocalVariables(context).ToList();
 2695107        var cancellationToken = context.CancellationToken;
 2695108        var includeTagsList = includeTags?.ToList();
 109
 5568110        foreach (var variable in variableList)
 111        {
 89112            if (!register.TryGetBlock(variable.Id, out var block))
 113                continue;
 114
 41115            var metadata = (VariableBlockMetadata)block.Metadata!;
 41116            var driver = storageDriverManager.Get(metadata.StorageDriverType!);
 117
 41118            if (driver == null)
 119                continue;
 120
 30121            if (includeTagsList != null && !driver.Tags.Any(includeTagsList.Contains))
 122                continue;
 123
 30124            var id = GetStateId(variable);
 30125            var storageDriverContext = new StorageDriverContext(context, variable, cancellationToken);
 30126            await driver.DeleteAsync(id, storageDriverContext);
 30127            register.Blocks.Remove(variable.Id);
 30128        }
 2695129    }
 130
 131    /// <inheritdoc />
 132    public async Task DeleteVariablesAsync(WorkflowExecutionContext context, IEnumerable<string>? includeTags = default)
 133    {
 0134        var activityContexts = context.ActivityExecutionContexts.ToList();
 0135        var includeTagsList = includeTags?.ToList();
 136
 0137        foreach (var activityContext in activityContexts)
 138        {
 0139            await DeleteVariablesAsync(activityContext, includeTagsList);
 140        }
 0141    }
 142
 6139143    private IEnumerable<Variable> GetLocalVariables(IExecutionContext context) => context.Variables;
 144
 145    private MemoryBlock EnsureBlock(MemoryRegister register, Variable variable)
 146    {
 21147        if (!register.TryGetBlock(variable.Id, out var block))
 0148            block = register.Declare(variable);
 21149        return block;
 150    }
 151
 196152    private string GetStateId(Variable variable) => variable.Id;
 153}