From bf2d25cb7cc3ffa13e918ceac5beb864e1d1f09c Mon Sep 17 00:00:00 2001 From: Yaroslav Husynin Date: Fri, 20 Mar 2020 16:24:04 +0200 Subject: [PATCH] wip #53 * addded HttpClientUrlResponseManager to dependency injection * added parameter to IsHealthy --- .../WebWatcher/IAmUrlResponseManager.cs | 5 +++-- Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs | 5 ++++- .../ServiceManager/HttpClientUrlResponseManager.cs | 4 ++-- Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs | 6 +++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Elfo.Wardein.Abstractions/WebWatcher/IAmUrlResponseManager.cs b/Elfo.Wardein.Abstractions/WebWatcher/IAmUrlResponseManager.cs index 5db080c..896d2a4 100644 --- a/Elfo.Wardein.Abstractions/WebWatcher/IAmUrlResponseManager.cs +++ b/Elfo.Wardein.Abstractions/WebWatcher/IAmUrlResponseManager.cs @@ -1,10 +1,11 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; namespace Elfo.Firmenich.Wardein.Abstractions.WebWatcher { public interface IAmUrlResponseManager { - Task IsHealthy(bool assertWithStatusCode, string assertWithRegex); + Task IsHealthy(bool assertWithStatusCode, string assertWithRegex, Uri url); Task RestartPool(string poolName); } } diff --git a/Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs b/Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs index 782336e..4b75e5b 100644 --- a/Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs +++ b/Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs @@ -1,7 +1,9 @@ using Elfo.Firmenich.Wardein.Abstractions.HeartBeat; using Elfo.Firmenich.Wardein.Abstractions.Watchers; +using Elfo.Firmenich.Wardein.Abstractions.WebWatcher; using Elfo.Firmenich.Wardein.Core.HeartBeat; using Elfo.Firmenich.Wardein.Core.Persistence; +using Elfo.Firmenich.Wardein.Core.ServiceManager; using Elfo.Wardein.Abstractions.Configuration; using Elfo.Wardein.Abstractions.Services; using Elfo.Wardein.Abstractions.Services.Models; @@ -74,7 +76,8 @@ protected void Configure() => new OracleWardeinHeartBeatPersistanceService(new OracleConnectionConfiguration.Builder(connectionString).Build()) // TODO add clientinfo etc ).AddTransient>(sp => (connectionString) => new OracleWatcherPersistenceService(new OracleConnectionConfiguration.Builder(connectionString).Build()) // TODO add clientinfo etc - ); + ) + .AddTransient>(); serviceProvider = serviceCollection.BuildServiceProvider(); diff --git a/Elfo.Wardein.Core/ServiceManager/HttpClientUrlResponseManager.cs b/Elfo.Wardein.Core/ServiceManager/HttpClientUrlResponseManager.cs index cb1f3f8..173a068 100644 --- a/Elfo.Wardein.Core/ServiceManager/HttpClientUrlResponseManager.cs +++ b/Elfo.Wardein.Core/ServiceManager/HttpClientUrlResponseManager.cs @@ -12,14 +12,14 @@ namespace Elfo.Firmenich.Wardein.Core.ServiceManager public class HttpClientUrlResponseManager : IAmUrlResponseManager { - public async Task IsHealthy(bool assertWithStatusCode, string assertWithRegex) + public async Task IsHealthy(bool assertWithStatusCode, string assertWithRegex, Uri url) { var endPoint = ""; using (var handler = new HttpClientHandler()) using (var client = new HttpClient(handler)) { // TODO support authentication - client.BaseAddress = new Uri("http://google.com/"); + client.BaseAddress = url; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); diff --git a/Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs b/Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs index d1ae50a..a41209b 100644 --- a/Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs +++ b/Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs @@ -16,6 +16,7 @@ public class WebWatcher : WardeinWatcher { private readonly WebWatcherConfig configuration; private readonly IAmWatcherPersistenceService watcherPersistenceService; + private readonly HttpClientUrlResponseManager urlResponseManager; protected static ILogger log = LogManager.GetCurrentClassLogger(); protected WebWatcher(string name, WebWatcherConfig config, string group) : base(name, config, group) @@ -31,6 +32,7 @@ protected WebWatcher(string name, WebWatcherConfig config, string group) : base( configuration = config; watcherPersistenceService = ServicesContainer.WatcherPersistenceService(configuration.ConnectionString); + } public override async Task ExecuteWatcherActionAsync() @@ -57,10 +59,8 @@ internal virtual async Task RunCheck() { log.Info($"Starting check on {GetUrlDisplayName}"); - IAmUrlResponseManager urlResponseManager = new HttpClientUrlResponseManager(); - var notificationService = ServicesContainer.NotificationService(NotificationType.Mail); - var isHealthy = await urlResponseManager.IsHealthy(configuration.AssertWithStatusCode, configuration.AssertWithRegex); + var isHealthy = await urlResponseManager.IsHealthy(configuration.AssertWithStatusCode, configuration.AssertWithRegex, configuration.Url); var currentStatus = await watcherPersistenceService.UpsertCurrentStatus ( watcherConfigurationId: configuration.WatcherConfigurationId,