2025-08-14 13:12:16 -03:00
|
|
|
using Elecciones.Database;
|
2025-08-14 15:27:45 -03:00
|
|
|
using Elecciones.Infrastructure.Services;
|
2025-08-14 12:37:57 -03:00
|
|
|
using Elecciones.Worker;
|
2025-08-14 13:12:16 -03:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-08-15 17:31:51 -03:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2025-08-16 10:52:07 -03:00
|
|
|
using System.Net;
|
2025-08-15 17:31:51 -03:00
|
|
|
using Serilog;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Security;
|
|
|
|
|
using System.Security.Authentication;
|
|
|
|
|
|
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.CreateBootstrapLogger();
|
|
|
|
|
|
|
|
|
|
Log.Information("Iniciando Elecciones.Worker Host...");
|
2025-08-14 12:37:57 -03:00
|
|
|
|
|
|
|
|
var builder = Host.CreateApplicationBuilder(args);
|
2025-08-14 13:12:16 -03:00
|
|
|
|
2025-08-15 17:31:51 -03:00
|
|
|
builder.Services.AddSerilog(config =>
|
|
|
|
|
config
|
|
|
|
|
.ReadFrom.Configuration(builder.Configuration)
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.WriteTo.File("logs/worker-.log", rollingInterval: RollingInterval.Day));
|
2025-08-14 13:12:16 -03:00
|
|
|
|
2025-08-15 17:31:51 -03:00
|
|
|
// --- Configuración de Servicios ---
|
2025-08-14 13:12:16 -03:00
|
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
|
|
|
|
builder.Services.AddDbContext<EleccionesDbContext>(options =>
|
|
|
|
|
options.UseSqlServer(connectionString));
|
2025-08-14 15:27:45 -03:00
|
|
|
|
|
|
|
|
builder.Services.AddHttpClient("ElectoralApiClient", client =>
|
|
|
|
|
{
|
|
|
|
|
var baseUrl = builder.Configuration["ElectoralApi:BaseUrl"];
|
|
|
|
|
if (!string.IsNullOrEmpty(baseUrl))
|
|
|
|
|
{
|
|
|
|
|
client.BaseAddress = new Uri(baseUrl);
|
|
|
|
|
}
|
2025-08-17 20:47:51 -03:00
|
|
|
|
|
|
|
|
// --- 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.
|
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(90);
|
2025-08-16 09:37:54 -03:00
|
|
|
|
|
|
|
|
client.DefaultRequestHeaders.Clear();
|
|
|
|
|
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36");
|
2025-08-17 20:47:51 -03:00
|
|
|
client.DefaultRequestHeaders.Add("Accept", "*/*");
|
|
|
|
|
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
|
|
|
|
|
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
|
2025-08-16 09:46:25 -03:00
|
|
|
|
2025-08-15 17:31:51 -03:00
|
|
|
})
|
|
|
|
|
.ConfigurePrimaryHttpMessageHandler(() =>
|
|
|
|
|
{
|
2025-08-16 09:46:25 -03:00
|
|
|
var handler = new SocketsHttpHandler
|
2025-08-15 17:31:51 -03:00
|
|
|
{
|
2025-08-16 10:52:07 -03:00
|
|
|
// Habilita la descompresión automática de respuestas GZIP y Deflate.
|
|
|
|
|
// Esto soluciona el error de JsonException ('0x1F').
|
|
|
|
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
|
|
|
|
|
2025-08-15 17:31:51 -03:00
|
|
|
SslOptions = new SslClientAuthenticationOptions
|
|
|
|
|
{
|
|
|
|
|
EnabledSslProtocols = SslProtocols.Tls13,
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-08-16 09:46:25 -03:00
|
|
|
|
|
|
|
|
if (!OperatingSystem.IsWindows())
|
|
|
|
|
{
|
|
|
|
|
handler.SslOptions.CipherSuitesPolicy = new CipherSuitesPolicy(new[]
|
|
|
|
|
{
|
|
|
|
|
TlsCipherSuite.TLS_AES_128_GCM_SHA256,
|
|
|
|
|
TlsCipherSuite.TLS_AES_256_GCM_SHA384,
|
|
|
|
|
TlsCipherSuite.TLS_CHACHA20_POLY1305_SHA256
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handler;
|
2025-08-14 15:27:45 -03:00
|
|
|
});
|
2025-08-15 17:31:51 -03:00
|
|
|
|
2025-08-14 15:27:45 -03:00
|
|
|
builder.Services.AddSingleton<IElectoralApiService, ElectoralApiService>();
|
2025-08-16 09:46:25 -03:00
|
|
|
|
2025-08-14 12:37:57 -03:00
|
|
|
builder.Services.AddHostedService<Worker>();
|
|
|
|
|
|
2025-08-14 15:27:45 -03:00
|
|
|
var host = builder.Build();
|
2025-08-15 17:31:51 -03:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
host.Run();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Fatal(ex, "El Host de Elecciones.Worker terminó inesperadamente");
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|