|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Reflection; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Autofac; |
| 11 | +using Microsoft.Extensions.Configuration; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Microsoft.Extensions.Options; |
| 14 | +using NuGet.Jobs.Montoring.PackageLag; |
| 15 | +using NuGet.Jobs.Validation; |
| 16 | +using NuGet.Services.AzureManagement; |
| 17 | + |
| 18 | +namespace NuGet.Monitoring.RebootSearchInstance |
| 19 | +{ |
| 20 | + public class Job : JsonConfigurationJob |
| 21 | + { |
| 22 | + private const string AzureManagementSectionName = "AzureManagement"; |
| 23 | + private const string MonitorConfigurationSectionName = "MonitorConfiguration"; |
| 24 | + |
| 25 | + public override async Task Run() |
| 26 | + { |
| 27 | + using (var scope = _serviceProvider.CreateScope()) |
| 28 | + { |
| 29 | + var rebooter = scope.ServiceProvider.GetRequiredService<ISearchInstanceRebooter>(); |
| 30 | + await rebooter.RunAsync(CancellationToken.None); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + protected override void ConfigureJobServices(IServiceCollection services, IConfigurationRoot configurationRoot) |
| 35 | + { |
| 36 | + services.Configure<MonitorConfiguration>(configurationRoot.GetSection(MonitorConfigurationSectionName)); |
| 37 | + services.Configure<SearchServiceConfiguration>(configurationRoot.GetSection(MonitorConfigurationSectionName)); |
| 38 | + services.Configure<AzureManagementAPIWrapperConfiguration>(configurationRoot.GetSection(AzureManagementSectionName)); |
| 39 | + |
| 40 | + services.AddTransient<ITelemetryService, TelemetryService>(); |
| 41 | + services.AddTransient<ISearchInstanceRebooter, SearchInstanceRebooter>(); |
| 42 | + services.AddTransient<IFeedClient, FeedClient>(); |
| 43 | + services.AddTransient<ISearchServiceClient, SearchServiceClient>(); |
| 44 | + services.AddSingleton<IAzureManagementAPIWrapper, AzureManagementAPIWrapper>(); |
| 45 | + services.AddTransient<IAzureManagementAPIWrapperConfiguration>(p => p.GetService<IOptionsSnapshot<AzureManagementAPIWrapperConfiguration>>().Value); |
| 46 | + |
| 47 | + services.AddSingleton(p => |
| 48 | + { |
| 49 | + var assembly = Assembly.GetEntryAssembly(); |
| 50 | + var assemblyName = assembly.GetName().Name; |
| 51 | + var assemblyVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0"; |
| 52 | + |
| 53 | + var client = new HttpClient(new WebRequestHandler |
| 54 | + { |
| 55 | + AllowPipelining = true, |
| 56 | + AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate), |
| 57 | + ServerCertificateCustomValidationCallback = |
| 58 | + (httpRequestMessage, cert, cetChain, policyErrors) => |
| 59 | + { |
| 60 | + if (policyErrors == System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch || policyErrors == System.Net.Security.SslPolicyErrors.None) |
| 61 | + { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + return false; |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + client.DefaultRequestHeaders.Add("User-Agent", $"{assemblyName}/{assemblyVersion}"); |
| 70 | + client.Timeout = TimeSpan.FromSeconds(10); |
| 71 | + |
| 72 | + return client; |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + protected override void ConfigureAutofacServices(ContainerBuilder containerBuilder) |
| 77 | + { |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments