From d4140d72167fb402304c3c354002b9c0ac074eaa Mon Sep 17 00:00:00 2001 From: Yaroslav Husynin Date: Fri, 20 Mar 2020 10:31:47 +0200 Subject: [PATCH] wip #52 * added logic of IsHealty * added IAmResponceManager * added HttpClientResponceManager --- .../WebWatcher/IAmUrlResponseManager.cs | 10 ++ .../HttpClientUrlResponseManager.cs | 78 ++++++++++++++ .../ServiceManager/WebWatcherManager.cs | 101 ------------------ .../WebWatcher/WebWatcher.cs | 6 +- 4 files changed, 91 insertions(+), 104 deletions(-) create mode 100644 Elfo.Wardein.Abstractions/WebWatcher/IAmUrlResponseManager.cs create mode 100644 Elfo.Wardein.Core/ServiceManager/HttpClientUrlResponseManager.cs delete mode 100644 Elfo.Wardein.Core/ServiceManager/WebWatcherManager.cs diff --git a/Elfo.Wardein.Abstractions/WebWatcher/IAmUrlResponseManager.cs b/Elfo.Wardein.Abstractions/WebWatcher/IAmUrlResponseManager.cs new file mode 100644 index 0000000..bd2ec6e --- /dev/null +++ b/Elfo.Wardein.Abstractions/WebWatcher/IAmUrlResponseManager.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; + +namespace Elfo.Firmenich.Wardein.Abstractions.WebWatcher +{ + public interface IAmUrlResponseManager + { + Task IsHealty(bool assertWithStatusCode, string assertWithRegex); + void RestartPool(string poolName); + } +} diff --git a/Elfo.Wardein.Core/ServiceManager/HttpClientUrlResponseManager.cs b/Elfo.Wardein.Core/ServiceManager/HttpClientUrlResponseManager.cs new file mode 100644 index 0000000..5953713 --- /dev/null +++ b/Elfo.Wardein.Core/ServiceManager/HttpClientUrlResponseManager.cs @@ -0,0 +1,78 @@ +using Elfo.Firmenich.Wardein.Abstractions.WebWatcher; +using Elfo.Wardein.Core.ServiceManager; +using System; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; + +namespace Elfo.Firmenich.Wardein.Core.ServiceManager +{ + public class HttpClientUrlResponseManager : IAmUrlResponseManager + { + + public Task IsHealty(bool assertWithStatusCode, string assertWithRegex) + { + var endPoint = ""; + using (var handler = new HttpClientHandler()) + using (var client = new HttpClient(handler)) + { + client.BaseAddress = new Uri("http://localhost:55587/"); + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("Keep-Alive")); + + var response = client.GetAsync(endPoint); + HttpResponseMessage httpWebResponse = response.Result; + + if (!assertWithStatusCode) + { + if (!string.IsNullOrEmpty(assertWithRegex)) + { + var isMatch = false; + //check if html content matches the regex + if (isMatch) + { + return Task.FromResult(false); + } + else + { + return Task.FromResult(true); + } + } + else + { + return Task.FromResult(true); + } + } + else + { + if (httpWebResponse.StatusCode != HttpStatusCode.OK) + { + return Task.FromResult(false); + } + else + { + if (!string.IsNullOrEmpty(assertWithRegex)) + { + var isMatch = false; + //check if html content matches the regex + if (isMatch) + { + return Task.FromResult(false); + } + else + { + return Task.FromResult(true); + } + } + } + + } + return Task.FromResult(false); + } + } + + public void RestartPool(string poolName) + => new IISPoolManager(poolName).Restart(); + } +} diff --git a/Elfo.Wardein.Core/ServiceManager/WebWatcherManager.cs b/Elfo.Wardein.Core/ServiceManager/WebWatcherManager.cs deleted file mode 100644 index 751241d..0000000 --- a/Elfo.Wardein.Core/ServiceManager/WebWatcherManager.cs +++ /dev/null @@ -1,101 +0,0 @@ -using Elfo.Wardein.Abstractions.Services; -using Elfo.Wardein.Core.ExtensionMethods; -using Microsoft.Web.Administration; -using NLog; -using System; -using System.Linq; - -namespace Elfo.Firmenich.Wardein.Core.ServiceManager -{ - public class WebWatcherManager : IAmServiceManager - { - #region Private variables - private readonly ApplicationPool applicationPool; - private readonly static Logger log = LogManager.GetCurrentClassLogger(); - #endregion - - public WebWatcherManager(string appPoolName) : base(appPoolName) - { - this.applicationPool = new ServerManager().ApplicationPools.FirstOrDefault(x => x.Name == appPoolName); - - if (applicationPool == null) - throw new ArgumentNullException($"ApplicationPool {appPoolName} doesn't exist"); - } - - public override bool IsStillAlive => applicationPool.State == ObjectState.Started; - - public override void ForceKill() - { - try - { - log.Info($"Stopping pool {serviceName} @ {DateTime.UtcNow}"); - applicationPool.Stop(); - applicationPool.WaitForStatus(ObjectState.Stopped, TimeSpan.FromSeconds(30)); - log.Info($"{serviceName} pool stopped @ {DateTime.UtcNow}"); - } - catch (Exception ex) - { - log.Error(ex, $"Error while killing {serviceName} IIS pool"); - throw; - } - } - - public override void Restart() - { - try - { - Stop(); - Start(); - } - catch (Exception ex) - { - log.Error(ex, $"Error while restarting {serviceName} IIS pool"); - throw; - } - } - - public override void Start() - { - try - { - log.Info($"Starting pool {base.serviceName} @ {DateTime.UtcNow}"); - if (!IsStillAlive) - this.applicationPool.Start(); - this.applicationPool.WaitForStatus(ObjectState.Started, TimeSpan.FromSeconds(30)); - log.Info($"{base.serviceName} pool started @ {DateTime.UtcNow}"); - } - catch (Exception ex) - { - log.Error(ex, $"Error while starting {base.serviceName} IIS pool"); - throw; - } - } - - public override void Stop() - { - try - { - if (IsStillAlive) - ForceKill(); - } - catch (Exception ex) - { - log.Error(ex, $"Error while stopping {serviceName} IIS pool"); - throw; - } - } - - public override string GetStatus() - { - try - { - return applicationPool.State.ToString(); - } - catch (Exception ex) - { - log.Error(ex, $"Error while getting status for pool {base.serviceName} IIS pool"); - throw; - } - } - } -} diff --git a/Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs b/Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs index 3e9f2fc..163145a 100644 --- a/Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs +++ b/Elfo.Wardein.Watchers/WebWatcher/WebWatcher.cs @@ -149,7 +149,7 @@ internal virtual async Task RunCheck() { using (var persistenceService = ServicesContainer.PersistenceService(Const.DB_PATH)) { - IAmServiceManager serviceManager = GetServiceManager(); + IAmResponceManager serviceManager = GetServiceManager(); if (serviceManager == null) continue; // If the service doesn't exist, skip the check @@ -168,9 +168,9 @@ internal virtual async Task RunCheck() #region Local Functions - IAmServiceManager GetServiceManager() + IAmResponceManager GetServiceManager() { - IAmServiceManager svc = null; + IAmResponceManager svc = null; try { svc = ServicesContainer.ServiceManager(service.ServiceName, service.ServiceManagerType);