< 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 />
 42110public class VariablePersistenceManager(IStorageDriverManager storageDriverManager, ILogger<VariablePersistenceManager> 
 11{
 12    /// <inheritdoc />
 13    public async Task LoadVariablesAsync(WorkflowExecutionContext workflowExecutionContext, IEnumerable<string>? exclude
 14    {
 44215        var cancellationToken = workflowExecutionContext.CancellationToken;
 44216        var contexts = workflowExecutionContext.ActivityExecutionContexts.ToList();
 44217        var excludeTagsList = excludeTags?.ToList();
 18
 115219        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        }
 44269    }
 70
 71    /// <inheritdoc />
 72    public async Task SaveVariablesAsync(WorkflowExecutionContext workflowExecutionContext)
 73    {
 43674        var cancellationToken = workflowExecutionContext.CancellationToken;
 43675        var contexts = workflowExecutionContext.ActivityExecutionContexts.ToList();
 76
 707877        foreach (var context in contexts)
 78        {
 310379            var variables = GetLocalVariables(context).ToList();
 80
 645481            foreach (var variable in variables)
 82            {
 12483                var block = variable.GetBlock(context.ExpressionExecutionContext);
 12484                var metadata = (VariableBlockMetadata)block.Metadata!;
 12485                var driver = storageDriverManager.Get(metadata.StorageDriverType!);
 86
 12487                if (driver == null)
 88                    continue;
 89
 8990                var id = GetStateId(variable);
 8991                var value = block.Value;
 8992                var storageDriverContext = new StorageDriverContext(context, variable, cancellationToken);
 93
 8994                if (value == null)
 295                    await driver.DeleteAsync(id, storageDriverContext);
 96                else
 8797                    await driver.WriteAsync(id, value, storageDriverContext);
 98            }
 310399        }
 436100    }
 101
 102    /// <inheritdoc />
 103    public async Task DeleteVariablesAsync(ActivityExecutionContext context, IEnumerable<string>? includeTags = default)
 104    {
 2541105        var register = context.ExpressionExecutionContext.Memory;
 2541106        var variableList = GetLocalVariables(context).ToList();
 2541107        var cancellationToken = context.CancellationToken;
 2541108        var includeTagsList = includeTags?.ToList();
 109
 5260110        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        }
 2541129    }
 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
 5778143    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
 134152    private string GetStateId(Variable variable) => variable.Id;
 153}