| 
									
										
										
										
											2025-07-01 12:27:28 -03:00
										 |  |  | using System.Text; | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | using Mercados.Infrastructure; | 
					
						
							|  |  |  | using Mercados.Infrastructure.DataFetchers; | 
					
						
							|  |  |  | using Mercados.Infrastructure.Persistence; | 
					
						
							|  |  |  | using Mercados.Infrastructure.Persistence.Repositories; | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | using Mercados.Worker; | 
					
						
							| 
									
										
										
										
											2025-07-03 12:11:08 -03:00
										 |  |  | using Polly; | 
					
						
							|  |  |  | using Polly.Extensions.Http; | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | using Mercados.Infrastructure.Services; | 
					
						
							|  |  |  | using DotNetEnv; | 
					
						
							|  |  |  | using DotNetEnv.Configuration; | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | var envFilePath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../../.env")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Cargamos el archivo .env desde la ruta explícita. | 
					
						
							|  |  |  | // Si no lo encuentra, Load retornará false. | 
					
						
							|  |  |  | if (!Env.Load(envFilePath).Any()) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Console.WriteLine($"ADVERTENCIA: No se pudo encontrar el archivo .env en la ruta: {envFilePath}"); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:27:28 -03:00
										 |  |  | Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | IHost host = Host.CreateDefaultBuilder(args) | 
					
						
							|  |  |  |     .ConfigureServices((hostContext, services) => | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  |         // La línea 'config.AddDotNetEnv(optional: true);' ha sido eliminada. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  |         IConfiguration configuration = hostContext.Configuration; | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  |         // --- 1. Registro de Servicios de Infraestructura --- | 
					
						
							|  |  |  |         services.AddSingleton<IDbConnectionFactory, SqlConnectionFactory>(); | 
					
						
							|  |  |  |         services.AddScoped<ICotizacionGanadoRepository, CotizacionGanadoRepository>(); | 
					
						
							|  |  |  |         services.AddScoped<ICotizacionGranoRepository, CotizacionGranoRepository>(); | 
					
						
							|  |  |  |         services.AddScoped<ICotizacionBolsaRepository, CotizacionBolsaRepository>(); | 
					
						
							|  |  |  |         services.AddScoped<IFuenteDatoRepository, FuenteDatoRepository>(); | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  |         //services.AddScoped<INotificationService, ConsoleNotificationService>(); | 
					
						
							|  |  |  |         services.AddScoped<INotificationService, EmailNotificationService>(); | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // --- 2. Registro de los Data Fetchers --- | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  |         // Descomentados para la versión final y funcional. | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  |         services.AddScoped<IDataFetcher, MercadoAgroFetcher>(); | 
					
						
							|  |  |  |         services.AddScoped<IDataFetcher, BcrDataFetcher>(); | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  |         services.AddScoped<IDataFetcher, FinnhubDataFetcher>(); | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  |         services.AddScoped<IDataFetcher, YahooFinanceDataFetcher>(); | 
					
						
							| 
									
										
										
										
											2025-07-03 12:11:08 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  |         // --- 3. Configuración de Clientes HTTP con Polly --- | 
					
						
							|  |  |  |         services.AddHttpClient("MercadoAgroFetcher").AddPolicyHandler(GetRetryPolicy()); | 
					
						
							|  |  |  |         services.AddHttpClient("BcrDataFetcher").AddPolicyHandler(GetRetryPolicy()); | 
					
						
							|  |  |  |         services.AddHttpClient("FinnhubDataFetcher").AddPolicyHandler(GetRetryPolicy()); | 
					
						
							| 
									
										
										
										
											2025-07-03 12:11:08 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  |         // --- 4. Registro del Worker Principal --- | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  |         services.AddHostedService<DataFetchingService>(); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .Build(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 12:11:08 -03:00
										 |  |  | static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return HttpPolicyExtensions | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  |         .HandleTransientHttpError() | 
					
						
							|  |  |  |         .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.RequestTimeout) | 
					
						
							|  |  |  |         .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), | 
					
						
							| 
									
										
										
										
											2025-07-03 12:11:08 -03:00
										 |  |  |             onRetry: (outcome, timespan, retryAttempt, context) => | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Console.WriteLine($"[Polly] Reintentando petición... Intento {retryAttempt}. Esperando {timespan.TotalSeconds}s. Causa: {outcome.Exception?.Message ?? outcome.Result.ReasonPhrase}"); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | await host.RunAsync(); |