| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | IHost host = Host.CreateDefaultBuilder(args) | 
					
						
							|  |  |  |     .ConfigureServices((hostContext, services) => | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         IConfiguration configuration = hostContext.Configuration; | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:56:06 -03:00
										 |  |  |         // El resto del código no cambia. IConfiguration recogerá automáticamente | 
					
						
							|  |  |  |         // las variables de entorno que cargamos correctamente. | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  |         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, EmailNotificationService>(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         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-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(); |