From e365d446371fc5a4693c403df56897f93e89088d 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 aa4105e3b..bb61acd67 100644 --- a/src/modules/Elsa.Workflows.Core/Contracts/IVariablePersistenceManager.cs +++ b/src/modules/Elsa.Workflows.Core/Contracts/IVariablePersistenceManager.cs @@ -19,7 +19,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 2ca6dcc86..c486c24e8 100644 --- a/src/modules/Elsa.Workflows.Core/Services/VariablePersistenceManager.cs +++ b/src/modules/Elsa.Workflows.Core/Services/VariablePersistenceManager.cs @@ -120,6 +120,33 @@ public class VariablePersistenceManager(IStorageDriverManager storageDriverManag } } + /// + 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)