Add WorkflowExecutionLogRecordExtractor

Introduced a new service, WorkflowExecutionLogRecordExtractor, to abstract the logic for extracting workflow execution logs records from the WorkflowExecutionContext. Updated StoreWorkflowExecutionLogSink to use this new service, which simplifies the execution log persistence method. This modification enhances code readability and enables potential reuse of the extraction logic.
This commit is contained in:
Raymond den Haan 2024-07-08 16:13:58 +02:00
parent 218f3b46e4
commit f3a8ff5e47
3 changed files with 47 additions and 25 deletions

View file

@ -0,0 +1,10 @@
using Elsa.Workflows.Runtime.Entities;
namespace Elsa.Workflows.Runtime;
/// Extracts workflow execution log records.
public interface IWorkflowExecutionLogRecordExtractor
{
/// Extracts workflow execution logs from a workflow execution context.
IEnumerable<WorkflowExecutionLogRecord> ExtractWorkflowExecutionLogs(WorkflowExecutionContext context);
}

View file

@ -1,5 +1,4 @@
using Elsa.Mediator.Contracts;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Runtime.Contracts;
using Elsa.Workflows.Runtime.Entities;
using Elsa.Workflows.Runtime.Notifications;
@ -9,34 +8,12 @@ namespace Elsa.Workflows.Runtime.Services;
/// <summary>
/// This implementation saves <see cref="WorkflowExecutionLogRecord"/> directly through the store.
/// </summary>
public class StoreWorkflowExecutionLogSink(IWorkflowExecutionLogStore store, IIdentityGenerator identityGenerator, INotificationSender notificationSender) : IWorkflowExecutionLogSink
public class StoreWorkflowExecutionLogSink(IWorkflowExecutionLogStore store, IWorkflowExecutionLogRecordExtractor extractor, INotificationSender notificationSender) : IWorkflowExecutionLogSink
{
/// <inheritdoc />
public async Task PersistExecutionLogsAsync(WorkflowExecutionContext context, CancellationToken cancellationToken)
{
var records = context.ExecutionLog.Select(x => new WorkflowExecutionLogRecord
{
Id = identityGenerator.GenerateId(),
ActivityInstanceId = x.ActivityInstanceId,
ParentActivityInstanceId = x.ParentActivityInstanceId,
ActivityNodeId = x.NodeId,
ActivityId = x.ActivityId,
ActivityType = x.ActivityType,
ActivityTypeVersion = x.ActivityTypeVersion,
ActivityName = x.ActivityName,
Message = x.Message,
EventName = x.EventName,
WorkflowDefinitionId = context.Workflow.Identity.DefinitionId,
WorkflowDefinitionVersionId = context.Workflow.Identity.Id,
WorkflowInstanceId = context.Id,
WorkflowVersion = context.Workflow.Version,
Source = x.Source,
ActivityState = x.ActivityState,
Payload = x.Payload,
Timestamp = x.Timestamp,
Sequence = x.Sequence
}).ToList();
var records = extractor.ExtractWorkflowExecutionLogs(context).ToList();
await store.AddManyAsync(records, context.CancellationTokens.SystemCancellationToken);
await notificationSender.SendAsync(new WorkflowExecutionLogUpdated(context), context.CancellationTokens.SystemCancellationToken);
}

View file

@ -0,0 +1,35 @@
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Runtime.Entities;
namespace Elsa.Workflows.Runtime.Mappers;
/// <inheritdoc />
public class WorkflowExecutionLogRecordExtractor(IIdentityGenerator identityGenerator) : IWorkflowExecutionLogRecordExtractor
{
/// <inheritdoc />
public IEnumerable<WorkflowExecutionLogRecord> ExtractWorkflowExecutionLogs(WorkflowExecutionContext context)
{
return context.ExecutionLog.Select(x => new WorkflowExecutionLogRecord
{
Id = identityGenerator.GenerateId(),
ActivityInstanceId = x.ActivityInstanceId,
ParentActivityInstanceId = x.ParentActivityInstanceId,
ActivityNodeId = x.NodeId,
ActivityId = x.ActivityId,
ActivityType = x.ActivityType,
ActivityTypeVersion = x.ActivityTypeVersion,
ActivityName = x.ActivityName,
Message = x.Message,
EventName = x.EventName,
WorkflowDefinitionId = context.Workflow.Identity.DefinitionId,
WorkflowDefinitionVersionId = context.Workflow.Identity.Id,
WorkflowInstanceId = context.Id,
WorkflowVersion = context.Workflow.Version,
Source = x.Source,
ActivityState = x.ActivityState,
Payload = x.Payload,
Timestamp = x.Timestamp,
Sequence = x.Sequence
});
}
}