Introduce the possibility to exclude variable value retrieval for certain storage drivers

This commit is contained in:
Marius Vasile Vușcan 2024-12-11 14:37:56 +02:00
parent c50a53f776
commit 16122182e2
No known key found for this signature in database
GPG key ID: E0EC75631D902E06
12 changed files with 42 additions and 15 deletions

View file

@ -27,7 +27,7 @@ internal class List(IWorkflowInstanceVariableManager workflowInstanceVariableMan
return;
}
var variables = await workflowInstanceVariableManager.GetVariablesAsync(workflowInstanceId, cancellationToken).ToList();
var variables = await workflowInstanceVariableManager.GetVariablesAsync(workflowInstanceId, true,cancellationToken).ToList();
var variableModels = variables.Select(x => new ResolvedVariableModel(x.Variable.Id, x.Variable.Name, x.Value)).ToList();
var response = new ListResponse<ResolvedVariableModel>(variableModels);
await SendOkAsync(response, cancellationToken);

View file

@ -10,6 +10,11 @@ public interface IStorageDriver
/// </summary>
double Priority { get; }
/// <summary>
/// A list of tags assigned to the driver used to describe the data contents.
/// </summary>
StorageDriverTag[] Tags { get; }
/// <summary>
/// Writes a value to the storage driver.
/// </summary>

View file

@ -6,9 +6,12 @@ namespace Elsa.Workflows;
public interface IVariablePersistenceManager
{
/// <summary>
/// Loads the variables into the specified <see cref="WorkflowExecutionContext"/>.
/// Loads the variables into the specified <see cref="WorkflowExecutionContext"/>.
/// </summary>
Task LoadVariablesAsync(WorkflowExecutionContext context);
/// <param name="context"></param>
/// <param name="excludeValueRetrievalForLargeDataDrivers"></param>
/// <returns></returns>
Task LoadVariablesAsync(WorkflowExecutionContext context, bool excludeValueRetrievalForLargeDataDrivers = false);
/// <summary>
/// Persists all persistable variables from the specified <see cref="WorkflowExecutionContext"/>.

View file

@ -9,7 +9,8 @@ public interface IWorkflowInstanceVariableReader
/// Retrieves all variables from the specified <see cref="WorkflowExecutionContext"/>.
/// </summary>
/// <param name="workflowExecutionContext">The context of the workflow execution.</param>
/// <param name="excludeValueRetrievalForLargeDataDrivers"></param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a collection of <see cref="ResolvedVariable"/> instances.</returns>
Task<IEnumerable<ResolvedVariable>> GetVariables(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken = default);
Task<IEnumerable<ResolvedVariable>> GetVariables(WorkflowExecutionContext workflowExecutionContext, bool excludeValueRetrievalForLargeDataDrivers = false, CancellationToken cancellationToken = default);
}

View file

@ -0,0 +1,6 @@
namespace Elsa.Workflows;
public enum StorageDriverTag
{
LargeData
}

View file

@ -2,7 +2,7 @@ namespace Elsa.Workflows;
public class DefaultWorkflowInstanceVariableReader(IVariablePersistenceManager variablePersistenceManager) : IWorkflowInstanceVariableReader
{
public async Task<IEnumerable<ResolvedVariable>> GetVariables(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken = default)
public async Task<IEnumerable<ResolvedVariable>> GetVariables(WorkflowExecutionContext workflowExecutionContext, bool excludeValueRetrievalForLargeDataDrivers = false, CancellationToken cancellationToken = default)
{
var workflow = workflowExecutionContext.Workflow;
var workflowVariables = workflow.Variables;
@ -11,7 +11,7 @@ public class DefaultWorkflowInstanceVariableReader(IVariablePersistenceManager v
if (rootWorkflowActivityExecutionContext == null)
return [];
await variablePersistenceManager.LoadVariablesAsync(workflowExecutionContext);
await variablePersistenceManager.LoadVariablesAsync(workflowExecutionContext, excludeValueRetrievalForLargeDataDrivers);
var resolvedVariables = new List<ResolvedVariable>();
foreach (var workflowVariable in workflowVariables)

View file

@ -9,8 +9,10 @@ namespace Elsa.Workflows;
public class MemoryStorageDriver : IStorageDriver
{
private readonly IDictionary<string, object> _dictionary = new Dictionary<string, object>();
/// <inheritdoc />
public double Priority => 0;
/// <inheritdoc />
public StorageDriverTag[] Tags => [];
/// <inheritdoc />
public ValueTask WriteAsync(string id, object value, StorageDriverContext context)

View file

@ -8,7 +8,7 @@ namespace Elsa.Workflows;
public class VariablePersistenceManager(IStorageDriverManager storageDriverManager) : IVariablePersistenceManager
{
/// <inheritdoc />
public async Task LoadVariablesAsync(WorkflowExecutionContext workflowExecutionContext)
public async Task LoadVariablesAsync(WorkflowExecutionContext workflowExecutionContext, bool excludeValueRetrievalForLargeDataDrivers = false)
{
var cancellationToken = workflowExecutionContext.CancellationToken;
var contexts = workflowExecutionContext.ActivityExecutionContexts.ToList();
@ -34,6 +34,9 @@ public class VariablePersistenceManager(IStorageDriverManager storageDriverManag
if (driver == null)
continue;
if (excludeValueRetrievalForLargeDataDrivers && driver.Tags.Contains(StorageDriverTag.LargeData))
continue;
var id = GetStateId(variable);
var value = await driver.ReadAsync(id, storageDriverContext);
if (value == null) continue;

View file

@ -17,8 +17,11 @@ public class WorkflowInstanceStorageDriver : IStorageDriver
/// The key used to store the variables in the workflow state.
/// </summary>
public const string VariablesDictionaryStateKey = "Variables";
/// <inheritdoc />
public double Priority => 5;
/// <inheritdoc />
public StorageDriverTag[] Tags => [];
/// <inheritdoc />
public ValueTask WriteAsync(string id, object value, StorageDriverContext context)

View file

@ -20,6 +20,8 @@ public class WorkflowStorageDriver : IStorageDriver
/// <inheritdoc />
public double Priority => -1;
/// <inheritdoc />
public StorageDriverTag[] Tags => [];
/// <inheritdoc />
public ValueTask WriteAsync(string id, object value, StorageDriverContext context)

View file

@ -6,17 +6,19 @@ public interface IWorkflowInstanceVariableManager
/// Retrieves all variables for the specified workflow instance.
/// </summary>
/// <param name="workflowInstanceId">The ID of the workflow instance.</param>
/// <param name="excludeValueRetrievalForLargeDataDrivers"></param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a collection of <see cref="ResolvedVariable"/> instances.</returns>
Task<IEnumerable<ResolvedVariable>> GetVariablesAsync(string workflowInstanceId, CancellationToken cancellationToken = default);
Task<IEnumerable<ResolvedVariable>> GetVariablesAsync(string workflowInstanceId, bool excludeValueRetrievalForLargeDataDrivers = false, CancellationToken cancellationToken = default);
/// <summary>
/// Retrieves all variables from the specified <see cref="WorkflowExecutionContext"/>.
/// </summary>
/// <param name="workflowExecutionContext">The context of the workflow execution.</param>
/// <param name="excludeValueRetrievalForLargeDataDrivers"></param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a collection of <see cref="ResolvedVariable"/> instances.</returns>
Task<IEnumerable<ResolvedVariable>> GetVariablesAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken = default);
Task<IEnumerable<ResolvedVariable>> GetVariablesAsync(WorkflowExecutionContext workflowExecutionContext, bool excludeValueRetrievalForLargeDataDrivers = false, CancellationToken cancellationToken = default);
/// <summary>
/// Sets the specified variables in the specified workflow instance.

View file

@ -7,16 +7,16 @@ public class WorkflowInstanceVariableManager(
IWorkflowInstanceVariableReader variableReader,
IWorkflowInstanceVariableWriter variableWriter) : IWorkflowInstanceVariableManager
{
public async Task<IEnumerable<ResolvedVariable>> GetVariablesAsync(string workflowInstanceId, CancellationToken cancellationToken = default)
public async Task<IEnumerable<ResolvedVariable>> GetVariablesAsync(string workflowInstanceId, bool excludeValueRetrievalForLargeDataDrivers = false, CancellationToken cancellationToken = default)
{
var workflowExecutionContext = await GetWorkflowExecutionContextAsync(workflowInstanceId, cancellationToken);
if (workflowExecutionContext == null) return [];
return await variableReader.GetVariables(workflowExecutionContext, cancellationToken);
return await variableReader.GetVariables(workflowExecutionContext, excludeValueRetrievalForLargeDataDrivers, cancellationToken);
}
public Task<IEnumerable<ResolvedVariable>> GetVariablesAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken = default)
public Task<IEnumerable<ResolvedVariable>> GetVariablesAsync(WorkflowExecutionContext workflowExecutionContext, bool excludeValueRetrievalForLargeDataDrivers = false, CancellationToken cancellationToken = default)
{
return variableReader.GetVariables(workflowExecutionContext, cancellationToken);
return variableReader.GetVariables(workflowExecutionContext, excludeValueRetrievalForLargeDataDrivers, cancellationToken);
}
public async Task<IEnumerable<ResolvedVariable>> SetVariablesAsync(string workflowInstanceId, IEnumerable<VariableUpdateValue> variables, CancellationToken cancellationToken = default)