Fix Worker 1348
This commit is contained in:
@@ -9,6 +9,8 @@ using Serilog;
|
||||
using System.Net.Http;
|
||||
using System.Net.Security;
|
||||
using System.Security.Authentication;
|
||||
using Polly;
|
||||
using Polly.Extensions.Http;
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.Console()
|
||||
@@ -37,7 +39,7 @@ builder.Services.AddHttpClient("ElectoralApiClient", client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(baseUrl);
|
||||
}
|
||||
|
||||
|
||||
// --- TIMEOUT MÁS LARGO ---
|
||||
// Aumentamos el tiempo de espera a 90 segundos.
|
||||
// Esto le dará a las peticiones lentas de la API tiempo suficiente para responder.
|
||||
@@ -75,7 +77,9 @@ builder.Services.AddHttpClient("ElectoralApiClient", client =>
|
||||
}
|
||||
|
||||
return handler;
|
||||
});
|
||||
})
|
||||
|
||||
.AddPolicyHandler(GetRetryPolicy());
|
||||
|
||||
builder.Services.AddSingleton<IElectoralApiService, ElectoralApiService>();
|
||||
|
||||
@@ -83,7 +87,8 @@ builder.Services.AddHostedService<Worker>();
|
||||
|
||||
var host = builder.Build();
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
host.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -93,4 +98,20 @@ catch (Exception ex)
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
|
||||
{
|
||||
return HttpPolicyExtensions
|
||||
// Manejar peticiones que fallaron por errores de red O que devolvieron un error de servidor (como 504)
|
||||
.HandleTransientHttpError()
|
||||
// O que devolvieron un código 504 Gateway Timeout específicamente
|
||||
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.GatewayTimeout)
|
||||
// Esperar y reintentar. La espera se duplica en cada reintento.
|
||||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
|
||||
onRetry: (outcome, timespan, retryAttempt, context) =>
|
||||
{
|
||||
// Opcional: Loguear cada reintento. Necesitarías pasar ILogger aquí.
|
||||
// Log.Warning("Retrying due to {StatusCode}. Waited {seconds}s. Attempt {retryAttempt}/3", outcome.Result.StatusCode, timespan.TotalSeconds, retryAttempt);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user