From f27563a93fa1890937e3b6d3abc4d165f2cb1f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Vasile=20Vu=C8=99can?= Date: Mon, 13 Jan 2025 16:48:24 +0200 Subject: [PATCH] Added DeleteVariablesAsync method for the workflow context --- .../Contracts/IVariablePersistenceManager.cs | 7 ++++- .../Services/VariablePersistenceManager.cs | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/modules/Elsa.Workflows.Core/Contracts/IVariablePersistenceManager.cs b/src/modules/Elsa.Workflows.Core/Contracts/IVariablePersistenceManager.cs index 37115a9ad..b050bb390 100644 --- a/src/modules/Elsa.Workflows.Core/Contracts/IVariablePersistenceManager.cs +++ b/src/modules/Elsa.Workflows.Core/Contracts/IVariablePersistenceManager.cs @@ -16,7 +16,12 @@ public interface IVariablePersistenceManager Task SaveVariablesAsync(WorkflowExecutionContext context); /// - /// Deletes the specified variables from the . + /// Deletes the specified variables from the . /// Task DeleteVariablesAsync(ActivityExecutionContext context); + + /// + /// Deletes the specified variables from the . + /// + Task DeleteVariablesAsync(WorkflowExecutionContext context); } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Core/Services/VariablePersistenceManager.cs b/src/modules/Elsa.Workflows.Core/Services/VariablePersistenceManager.cs index fa9f10b9a..6d608a00f 100644 --- a/src/modules/Elsa.Workflows.Core/Services/VariablePersistenceManager.cs +++ b/src/modules/Elsa.Workflows.Core/Services/VariablePersistenceManager.cs @@ -110,6 +110,33 @@ public class VariablePersistenceManager : IVariablePersistenceManager } } + /// + public async Task DeleteVariablesAsync(WorkflowExecutionContext context) + { + var cancellationToken = context.CancellationTokens.ApplicationCancellationToken; + var activityContexts = context.ActivityExecutionContexts.ToList(); + + foreach (var activityContext in activityContexts) + { + var variables = GetLocalVariables(activityContext).ToList(); + + foreach (var variable in variables) + { + var block = variable.GetBlock(activityContext.ExpressionExecutionContext); + var metadata = (VariableBlockMetadata)block.Metadata!; + var driver = _storageDriverManager.Get(metadata.StorageDriverType!); + + if (driver == null) + continue; + + var id = GetStateId(variable); + var storageDriverContext = new StorageDriverContext(activityContext, variable, cancellationToken); + + await driver.DeleteAsync(id, storageDriverContext); + } + } + } + private IEnumerable GetLocalVariables(IExecutionContext context) => context.Variables; private MemoryBlock EnsureBlock(MemoryRegister register, Variable variable)