static async Task Run( HttpHealthCheck healthCheck, TimeSpan interval, HealthCheckReporter reporter, ILogger diagnosticLog, CancellationToken cancel) { try { while (!cancel.IsCancellationRequested) { var result = await healthCheck.CheckNow(cancel); reporter.Report(result); if (result.Elapsed < interval.TotalMilliseconds) { var delay = (int)(interval.TotalMilliseconds - result.Elapsed); await Task.Delay(delay, cancel); } } } catch (OperationCanceledException) { // Unloading } catch (Exception ex) { diagnosticLog.Fatal(ex, "The health check task threw an unhandled exception"); } }
public void Start(TextWriter inputWriter) { _httpClient = HttpHealthCheckClient.Create(); var reporter = new HealthCheckReporter(inputWriter); JsonDataExtractor extractor = null; if (!string.IsNullOrWhiteSpace(DataExtractionExpression)) { extractor = new JsonDataExtractor(DataExtractionExpression); } var targetUrls = TargetUrl.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var targetUrl in targetUrls) { var healthCheck = new HttpHealthCheck( _httpClient, App.Title, targetUrl, extractor, BypassHttpCaching); _healthCheckTasks.Add(new HealthCheckTask( healthCheck, TimeSpan.FromSeconds(IntervalSeconds), reporter, Log)); } }
public HealthCheckTask(HttpHealthCheck healthCheck, TimeSpan interval, HealthCheckReporter reporter, ILogger diagnosticLog) { if (healthCheck == null) { throw new ArgumentNullException(nameof(healthCheck)); } if (reporter == null) { throw new ArgumentNullException(nameof(reporter)); } _healthCheckTask = Task.Run(() => Run(healthCheck, interval, reporter, diagnosticLog, _cancel.Token), _cancel.Token); }