Skip to content

Commit

Permalink
Log a message when an unhandled exception occurs in a task runner
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinchalet committed Jan 3, 2025
1 parent a926eef commit b772eb9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/OpenNetty/OpenNettyGatewayOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,39 @@ when message.Medium is OpenNettyMedium.Bus
_ => TimeSpan.FromSeconds(60)
}),
MaxRetryAttempts = int.MaxValue,
ShouldHandle = static arguments => ValueTask.FromResult(
!arguments.Context.CancellationToken.IsCancellationRequested &&
arguments.Outcome.Exception is not null)
ShouldHandle = static arguments =>
{
if (!arguments.Context.Properties.TryGetValue(
key : new ResiliencePropertyKey<OpenNettyGateway>(nameof(OpenNettyGateway)),
value: out OpenNettyGateway? gateway))
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0074));
}

if (!arguments.Context.Properties.TryGetValue(
key : new ResiliencePropertyKey<OpenNettyLogger<OpenNettyWorker>>(nameof(OpenNettyLogger<OpenNettyWorker>)),
value: out OpenNettyLogger<OpenNettyWorker>? logger))
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0074));
}

if (!arguments.Context.Properties.TryGetValue(
key : new ResiliencePropertyKey<OpenNettySessionType>(nameof(OpenNettySessionType)),
value: out OpenNettySessionType type))
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0074));
}

if (arguments.Outcome.Exception is not Exception exception)
{
return ValueTask.FromResult(false);
}

logger.SessionErrored(exception, gateway, type);

// Always recreate new sessions on failed attempts, unless the operation was canceled by the worker.
return ValueTask.FromResult(!arguments.Context.CancellationToken.IsCancellationRequested);
}
})
.Build()
};
Expand Down
12 changes: 12 additions & 0 deletions src/OpenNetty/OpenNettyLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,16 @@ public OpenNettyLogger(ILogger<TService> logger)
Level = LogLevel.Error,
Message = "An error occurred while trying to connect to the MQTT broker.")]
public partial void MqttBrokerConnectionError(Exception exception);

/// <summary>
/// Logs a message indicating that an error occcured while opening a session.
/// </summary>
/// <param name="exception">The exception.</param>
/// <param name="gateway">The gateway.</param>
/// <param name="type">The session type.</param>
[LoggerMessage(
EventId = 6020,
Level = LogLevel.Error,
Message = "An error occurred while trying to open a session of type {Type} to gateway {Gateway}.")]
public partial void SessionErrored(Exception exception, OpenNettyGateway gateway, OpenNettySessionType type);
}
2 changes: 2 additions & 0 deletions src/OpenNetty/OpenNettyWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async Task CreateSharedSessionWorkerAsync(OpenNettyGateway gateway, OpenNettySes
{
var context = ResilienceContextPool.Shared.Get(cancellationToken);
context.Properties.Set(new ResiliencePropertyKey<OpenNettyGateway>(nameof(OpenNettyGateway)), gateway);
context.Properties.Set(new ResiliencePropertyKey<OpenNettyLogger<OpenNettyWorker>>(nameof(OpenNettyLogger<OpenNettyWorker>)), _logger);
context.Properties.Set(new ResiliencePropertyKey<OpenNettySessionType>(nameof(OpenNettySessionType)), type);

_logger.TaskRunnerScheduled(gateway, type);
Expand Down Expand Up @@ -134,6 +135,7 @@ async Task CreateAdHocSessionWorkerAsync(
{
var context = ResilienceContextPool.Shared.Get(cancellationToken);
context.Properties.Set(new ResiliencePropertyKey<OpenNettyGateway>(nameof(OpenNettyGateway)), gateway);
context.Properties.Set(new ResiliencePropertyKey<OpenNettyLogger<OpenNettyWorker>>(nameof(OpenNettyLogger<OpenNettyWorker>)), _logger);
context.Properties.Set(new ResiliencePropertyKey<OpenNettySessionType>(nameof(OpenNettySessionType)), type);

_logger.TaskRunnerScheduled(gateway, type);
Expand Down

0 comments on commit b772eb9

Please sign in to comment.