From 45169d0e71f8459e24f78690b56bbfa635757ca2 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 14 Sep 2023 11:19:05 +0200 Subject: [PATCH] Add incident timestamp --- .../Mappers/ActivityIncidentStateMapper.cs | 3 ++- .../Proto/WorkflowInstance.Messages.proto | 1 + .../Activities/ExceptionHandlingMiddleware.cs | 8 ++++++-- .../Elsa.Workflows.Core/Models/ActivityIncident.cs | 11 +++++++++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/modules/Elsa.ProtoActor/Mappers/ActivityIncidentStateMapper.cs b/src/modules/Elsa.ProtoActor/Mappers/ActivityIncidentStateMapper.cs index 5c46409b6..56d880286 100644 --- a/src/modules/Elsa.ProtoActor/Mappers/ActivityIncidentStateMapper.cs +++ b/src/modules/Elsa.ProtoActor/Mappers/ActivityIncidentStateMapper.cs @@ -25,7 +25,7 @@ internal class ActivityIncidentStateMapper /// The mapped . public ActivityIncident Map(ProtoActivityIncident source) { - return new(source.ActivityId, source.ActivityType, source.Message, _exceptionMapper.Map(source.Exception)); + return new(source.ActivityId, source.ActivityType, source.Message, _exceptionMapper.Map(source.Exception), DateTimeOffset.Parse(source.Timestamp)); } /// @@ -49,6 +49,7 @@ internal class ActivityIncidentStateMapper Message = source.Message, ActivityType = source.ActivityType, ActivityId = source.ActivityId, + Timestamp = source.Timestamp.ToString("O") }; } diff --git a/src/modules/Elsa.ProtoActor/Proto/WorkflowInstance.Messages.proto b/src/modules/Elsa.ProtoActor/Proto/WorkflowInstance.Messages.proto index 5c8abf336..a9c8201c6 100644 --- a/src/modules/Elsa.ProtoActor/Proto/WorkflowInstance.Messages.proto +++ b/src/modules/Elsa.ProtoActor/Proto/WorkflowInstance.Messages.proto @@ -34,6 +34,7 @@ message ActivityIncident { string ActivityType = 2; string Message = 3; optional ExceptionState Exception = 4; + string Timestamp = 5; // ISO 8601 } message ExceptionState { diff --git a/src/modules/Elsa.Workflows.Core/Middleware/Activities/ExceptionHandlingMiddleware.cs b/src/modules/Elsa.Workflows.Core/Middleware/Activities/ExceptionHandlingMiddleware.cs index 239e73557..cae993a88 100644 --- a/src/modules/Elsa.Workflows.Core/Middleware/Activities/ExceptionHandlingMiddleware.cs +++ b/src/modules/Elsa.Workflows.Core/Middleware/Activities/ExceptionHandlingMiddleware.cs @@ -1,3 +1,4 @@ +using Elsa.Common.Contracts; using Elsa.Workflows.Core.Contracts; using Elsa.Workflows.Core.Models; using Elsa.Workflows.Core.Pipelines.ActivityExecution; @@ -24,15 +25,17 @@ public class ExceptionHandlingMiddleware : IActivityExecutionMiddleware { private readonly ActivityMiddlewareDelegate _next; private readonly IIncidentStrategyResolver _incidentStrategyResolver; + private readonly ISystemClock _systemClock; private readonly ILogger _logger; /// /// Constructor. /// - public ExceptionHandlingMiddleware(ActivityMiddlewareDelegate next, IIncidentStrategyResolver incidentStrategyResolver, ILogger logger) + public ExceptionHandlingMiddleware(ActivityMiddlewareDelegate next, IIncidentStrategyResolver incidentStrategyResolver, ISystemClock systemClock, ILogger logger) { _next = next; _incidentStrategyResolver = incidentStrategyResolver; + _systemClock = systemClock; _logger = logger; } @@ -51,7 +54,8 @@ public class ExceptionHandlingMiddleware : IActivityExecutionMiddleware var activity = context.Activity; var exceptionState = ExceptionState.FromException(e); - var incident = new ActivityIncident(activity.Id, activity.Type, e.Message, exceptionState); + var now = _systemClock.UtcNow; + var incident = new ActivityIncident(activity.Id, activity.Type, e.Message, exceptionState, now); context.WorkflowExecutionContext.Incidents.Add(incident); var strategy = await _incidentStrategyResolver.ResolveStrategyAsync(context); diff --git a/src/modules/Elsa.Workflows.Core/Models/ActivityIncident.cs b/src/modules/Elsa.Workflows.Core/Models/ActivityIncident.cs index 00766b77a..ac8a27c10 100644 --- a/src/modules/Elsa.Workflows.Core/Models/ActivityIncident.cs +++ b/src/modules/Elsa.Workflows.Core/Models/ActivityIncident.cs @@ -16,7 +16,7 @@ public class ActivityIncident { } - + /// /// Initializes a new instance of the class. /// @@ -24,12 +24,14 @@ public class ActivityIncident /// The type of the activity that caused the incident. /// The message of the incident. /// The exception that caused the incident. - public ActivityIncident(string activityId, string activityType, string message, ExceptionState? exception) + /// The timestamp of the incident. + public ActivityIncident(string activityId, string activityType, string message, ExceptionState? exception, DateTimeOffset timestamp) { ActivityId = activityId; ActivityType = activityType; Message = message; Exception = exception; + Timestamp = timestamp; } /// The ID of the activity that caused the incident. @@ -43,4 +45,9 @@ public class ActivityIncident /// The exception that caused the incident. public ExceptionState? Exception { get; } + + /// + /// The timestamp of the incident. + /// + public DateTimeOffset Timestamp { get; } } \ No newline at end of file