|
| 1 | +using System.Linq; |
| 2 | +using System.Net; |
1 | 3 | using Hangfire; |
2 | 4 | using Hangfire.Console; |
3 | 5 | using Hangfire.PostgreSql; |
4 | 6 | using MassTransit; |
5 | 7 | using Microsoft.Extensions.Configuration; |
6 | 8 | using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Http.Resilience; |
| 10 | +using Microsoft.Extensions.Logging; |
7 | 11 | using OneGround.ZGW.Common.Batching; |
8 | 12 | using OneGround.ZGW.Common.CorrelationId; |
9 | 13 | using OneGround.ZGW.Common.Extensions; |
|
21 | 25 | using OneGround.ZGW.Notificaties.Messaging.Jobs.Notificatie; |
22 | 26 | using OneGround.ZGW.Notificaties.Messaging.Services; |
23 | 27 | using Polly; |
| 28 | +using Polly.Retry; |
24 | 29 |
|
25 | 30 | namespace OneGround.ZGW.Notificaties.Messaging; |
26 | 31 |
|
@@ -64,10 +69,44 @@ public void ConfigureServices(IServiceCollection services) |
64 | 69 | serviceName, |
65 | 70 | (builder, context) => |
66 | 71 | { |
67 | | - context.EnableReloads<HttpResiliencePipelineOptions>(optionsKey); |
68 | | - var options = context.GetOptions<HttpResiliencePipelineOptions>(optionsKey); |
| 72 | + // Enable dynamic reloads of this pipeline whenever the named ResiliencePipelineNotificaties change |
| 73 | + context.EnableReloads<HttpResiliencePipelineOptions>("PollyConfig:NotificatiesSender"); |
| 74 | + |
| 75 | + // Retrieve the named options |
| 76 | + var options = context.GetOptions<HttpResiliencePipelineOptions>("PollyConfig:NotificatiesSender"); |
| 77 | + |
| 78 | + builder.AddRetry( |
| 79 | + new RetryStrategyOptions<HttpResponseMessage> |
| 80 | + { |
| 81 | + MaxRetryAttempts = options.Retry.MaxRetryAttempts, |
| 82 | + BackoffType = options.Retry.BackoffType, |
| 83 | + UseJitter = options.Retry.UseJitter, |
| 84 | + Delay = options.Retry.Delay, |
| 85 | + ShouldHandle = arg => |
| 86 | + { |
| 87 | + if (arg.Outcome.Result == null) // This flow is when service did not respond at all (gives no HTTP statuscode) |
| 88 | + { |
| 89 | + // Always retry on this flow |
| 90 | + return ValueTask.FromResult(true); |
| 91 | + } |
| 92 | + |
| 93 | + // Retry depending on the HTTP status code |
| 94 | + var shouldRetry = DefaultRetryOnHttpStatusCodes |
| 95 | + .Concat(HttpStatusCodesStringToEnumerable(options.AddRetryOnHttpStatusCodes)) |
| 96 | + .Any(statuscode => arg.Outcome.Result.StatusCode == statuscode); |
| 97 | + |
| 98 | + return ValueTask.FromResult(shouldRetry); |
| 99 | + }, |
| 100 | + OnRetry = arg => |
| 101 | + { |
| 102 | + LogRetry(context, arg); |
| 103 | + |
| 104 | + // Event handlers can be asynchronous; here, we return an empty ValueTask. |
| 105 | + return default; |
| 106 | + }, |
| 107 | + } |
| 108 | + ); |
69 | 109 |
|
70 | | - builder.AddRetry(options.Retry); |
71 | 110 | builder.AddTimeout(options.Timeout); |
72 | 111 | } |
73 | 112 | ); |
@@ -166,6 +205,24 @@ public void ConfigureServices(IServiceCollection services) |
166 | 205 | ); |
167 | 206 | } |
168 | 207 |
|
| 208 | + private static IEnumerable<HttpStatusCode> HttpStatusCodesStringToEnumerable(string addRetryOnHttpStatusCodes) |
| 209 | + { |
| 210 | + var result = addRetryOnHttpStatusCodes |
| 211 | + .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) |
| 212 | + .Select(codeString => |
| 213 | + { |
| 214 | + if (Enum.TryParse<HttpStatusCode>(codeString, out var statusCode)) |
| 215 | + { |
| 216 | + return (HttpStatusCode?)statusCode; |
| 217 | + } |
| 218 | + return null; |
| 219 | + }) |
| 220 | + .Where(code => code.HasValue) |
| 221 | + .Select(code => code!.Value); |
| 222 | + |
| 223 | + return result; |
| 224 | + } |
| 225 | + |
169 | 226 | private bool IsExpireFailedJobsScannerEnabled => |
170 | 227 | !string.IsNullOrWhiteSpace(_hangfireConfiguration.ExpireFailedJobsScanAt) |
171 | 228 | && !DisabledValues.Contains(_hangfireConfiguration.ExpireFailedJobsScanAt.Trim()); |
@@ -228,4 +285,57 @@ private AutomaticRetryAttribute GetRetryPolicyFromConfig() |
228 | 285 | LogEvents = false, |
229 | 286 | }; |
230 | 287 | } |
| 288 | + |
| 289 | + private static void LogRetry(ResilienceHandlerContext context, OnRetryArguments<HttpResponseMessage> arg) |
| 290 | + { |
| 291 | + using var scope = context.ServiceProvider.CreateScope(); |
| 292 | + |
| 293 | + var logger = scope.ServiceProvider.GetRequiredService<ILogger<ServiceConfiguration>>(); |
| 294 | + |
| 295 | + // Get the context in which a retry should be taken and log... |
| 296 | + if ( |
| 297 | + arg.Context.Properties.TryGetValue( |
| 298 | + new ResiliencePropertyKey<HttpRequestMessage>("Resilience.Http.RequestMessage"), |
| 299 | + out var httpRequestMessage |
| 300 | + ) |
| 301 | + ) |
| 302 | + { |
| 303 | + if (arg.Outcome.Result is { } httpResponseMessage) |
| 304 | + { |
| 305 | + logger.LogDebug( |
| 306 | + "OnRetry, Attempt# {AttemptNumber} on {Method} {RequestUri} => {ReasonPhrase} [{StatusCode}]. Next over {TotalSeconds} second(s).", |
| 307 | + arg.AttemptNumber, |
| 308 | + httpRequestMessage.Method, |
| 309 | + httpRequestMessage.RequestUri, |
| 310 | + httpResponseMessage.ReasonPhrase, |
| 311 | + (int)httpResponseMessage.StatusCode, |
| 312 | + arg.RetryDelay.TotalSeconds |
| 313 | + ); |
| 314 | + } |
| 315 | + else |
| 316 | + { |
| 317 | + logger.LogDebug( |
| 318 | + "OnRetry, Attempt# {AttemptNumber} on {Method} {RequestUri} => (no response). Next over {TotalSeconds} second(s).", |
| 319 | + arg.AttemptNumber, |
| 320 | + httpRequestMessage.Method, |
| 321 | + httpRequestMessage.RequestUri, |
| 322 | + arg.RetryDelay.TotalSeconds |
| 323 | + ); |
| 324 | + } |
| 325 | + } |
| 326 | + else |
| 327 | + { |
| 328 | + logger.LogDebug("OnRetry, Attempt# {AttemptNumber}. Next over {TotalSeconds} second(s).", arg.AttemptNumber, arg.RetryDelay.TotalSeconds); |
| 329 | + } |
| 330 | + } |
| 331 | + |
| 332 | + private static IEnumerable<HttpStatusCode> DefaultRetryOnHttpStatusCodes => |
| 333 | + [ |
| 334 | + HttpStatusCode.RequestTimeout, |
| 335 | + HttpStatusCode.TooManyRequests, |
| 336 | + HttpStatusCode.InternalServerError, |
| 337 | + HttpStatusCode.BadGateway, |
| 338 | + HttpStatusCode.ServiceUnavailable, |
| 339 | + HttpStatusCode.GatewayTimeout, |
| 340 | + ]; |
231 | 341 | } |
0 commit comments