warden-stack / Warden

Define "health checks" for your applications, resources and infrastructure. Keep your Warden on the watch.
https://getwarden.net
MIT License
615 stars 66 forks source link

[Question] Problem with WebWatcher and Timeouts #135

Closed clsource closed 3 years ago

clsource commented 7 years ago

Hello I´m trying to create a watcher. What I need is checking if the server went down. This is the code that I´m using.

 wardenConfiguration.AddWebWatcher(
                    WebWatcherConfiguration.Create("http://localhost"l)
                    .WithTimeout(TimeSpan.FromMilliseconds(3000))
                    .Build(),
                    hooks: hooks =>
                     {
                        // hooks
                     }, interval: TimeSpan.FromMilliseconds(15000));
....

.SetAggregatedWatcherHooks((hooks, integrations) =>
             {
                 hooks.OnFirstFailureAsync(results => OnFirstFailureAsyncWithResults(results, integrations))

                     .OnFirstSuccessAsync(results =>
                        OnFirstSuccessAsyncWithResults(results, integrations)
                     )

                     .OnFirstErrorAsync(exception => OnFirstErrorAsyncWithResults(exception, integrations));

        });

Basically the code wants to make a request every 15 seconds and if the server does not respond for 3 seconds it must send an alert.

My issue is that if I keep the 3 second timeout the code triggers an System.InvalidOperationException (This instance already started one or more requests) associated with the HttpClient. If I remove the 3 second timeout the code triggers an System.Net.Http.HttpRequestException. Both cases triggers only the OnFirstErrorAsync hook and not the OnFirstFailureAsync hook. The problem with this is that I cannot configure the timeout and can not trigger the failure hook if the server is down.

Should I use the Server Watcher instead?.

Thanks for any help :+1: and congrats for the great project :100:

spetz commented 7 years ago

Hi, Thank you for a detailed explanation of your issue. I took a quick look into the Web Watcher and it turns out that it returns an invalid result only if the TaskCanceledException happened.

I thought that this one will occur when there's a timeout but it seems that I was wrong :). I'll need to think how to fix this, as I'd rather do not change the current behavior of these 2 system exceptions (make them trigger OnError), but the timeout shouldn't be treated this way for sure.

Server watcher most likely works the same way but you can give it a try.

I'm glad that you like this library, thanks! :)

tolispy commented 7 years ago

Replace the line :

.WithTimeout(TimeSpan.FromMilliseconds(3000))

With the line :

.WithHttpServiceProvider(() => new HttpService(new HttpClient() { Timeout = TimeSpan.FromMilliseconds(3000) }))

spetz commented 7 years ago

@tolispy seems like a good workaround :).