From f3a8ff5e479b91cd0034a18a7a137c18f23f997d Mon Sep 17 00:00:00 2001 From: Raymond den Haan Date: Mon, 8 Jul 2024 16:13:58 +0200 Subject: [PATCH] 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. --- .../IWorkflowExecutionLogRecordExtractor.cs | 10 ++++++ .../Services/StoreWorkflowExecutionLogSink.cs | 27 ++------------ .../Services/WorkflowExecutionContext.cs | 35 +++++++++++++++++++ 3 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowExecutionLogRecordExtractor.cs create mode 100644 src/modules/Elsa.Workflows.Runtime/Services/WorkflowExecutionContext.cs diff --git a/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowExecutionLogRecordExtractor.cs b/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowExecutionLogRecordExtractor.cs new file mode 100644 index 000000000..0e704bd55 --- /dev/null +++ b/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowExecutionLogRecordExtractor.cs @@ -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 ExtractWorkflowExecutionLogs(WorkflowExecutionContext context); +} \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Runtime/Services/StoreWorkflowExecutionLogSink.cs b/src/modules/Elsa.Workflows.Runtime/Services/StoreWorkflowExecutionLogSink.cs index ecff347d0..d32bb89db 100644 --- a/src/modules/Elsa.Workflows.Runtime/Services/StoreWorkflowExecutionLogSink.cs +++ b/src/modules/Elsa.Workflows.Runtime/Services/StoreWorkflowExecutionLogSink.cs @@ -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; /// /// This implementation saves directly through the store. /// -public class StoreWorkflowExecutionLogSink(IWorkflowExecutionLogStore store, IIdentityGenerator identityGenerator, INotificationSender notificationSender) : IWorkflowExecutionLogSink +public class StoreWorkflowExecutionLogSink(IWorkflowExecutionLogStore store, IWorkflowExecutionLogRecordExtractor extractor, INotificationSender notificationSender) : IWorkflowExecutionLogSink { /// 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); } diff --git a/src/modules/Elsa.Workflows.Runtime/Services/WorkflowExecutionContext.cs b/src/modules/Elsa.Workflows.Runtime/Services/WorkflowExecutionContext.cs new file mode 100644 index 000000000..7badfd3ae --- /dev/null +++ b/src/modules/Elsa.Workflows.Runtime/Services/WorkflowExecutionContext.cs @@ -0,0 +1,35 @@ +using Elsa.Workflows.Contracts; +using Elsa.Workflows.Runtime.Entities; + +namespace Elsa.Workflows.Runtime.Mappers; + +/// +public class WorkflowExecutionLogRecordExtractor(IIdentityGenerator identityGenerator) : IWorkflowExecutionLogRecordExtractor +{ + /// + public IEnumerable 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 + }); + } +} \ No newline at end of file