From 5931fd6a8adcc1c278f7cfeecdbf73c0d7276e5b Mon Sep 17 00:00:00 2001 From: Felix Glang Date: Mon, 29 Apr 2024 20:21:46 +0200 Subject: [PATCH] Fix Bug If UmlautAdaparr was started before the *Arr. The BeReachable test failed, although the config was correct. Now it is tested every 15 seconds for 3 minutes whether the corresponding application can be reached. Before the test fails --- .../GlobalInstanceOptionsValidator.cs | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs b/UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs index 73b49e0..70263d6 100644 --- a/UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs +++ b/UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs @@ -15,8 +15,8 @@ namespace UmlautAdaptarr.Validator RuleFor(x => x.Host) .NotEmpty().WithMessage("Host is required when Enabled is true.") - .Must(BeAValidUrl).WithMessage("Host must start with http:// or https:// and be a valid address.") - .Must(BeReachable).WithMessage("Host is not reachable. Please check your Host or your UmlautAdaptrr Settings"); + .Must(BeAValidUrl).WithMessage("Host/Url must start with http:// or https:// and be a valid address.") + .Must(BeReachable).WithMessage("Host/Url is not reachable. Please check your Host or your UmlautAdaptrr Settings"); RuleFor(x => x.ApiKey) .NotEmpty().WithMessage("ApiKey is required when Enabled is true."); @@ -29,18 +29,37 @@ namespace UmlautAdaptarr.Validator && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); } - private bool BeReachable(string url) + private static bool BeReachable(string url) { - try + DateTime endTime = DateTime.Now.AddMinutes(3); + bool reachable = false; + var request = WebRequest.Create(url); + + while (DateTime.Now < endTime) { - var request = WebRequest.Create(url); - var response = (HttpWebResponse)request.GetResponse(); - return response.StatusCode == HttpStatusCode.OK; - } - catch - { - return false; + try + { + + var response = (HttpWebResponse)request.GetResponse(); + reachable = response.StatusCode == HttpStatusCode.OK; + if (reachable) + { + break; + } + else + { + Console.WriteLine($"The URL \"{url}\" is not reachable. Next attempt in 15 seconds..."); + } + } + catch + { + return false; + } + // Wait for 15 seconds + System.Threading.Thread.Sleep(15000); } + + return reachable; } } }