Ejemplo n.º 1
0
        public void Start(TextWriter inputWriter)
        {
            _httpClientWrapper = new HttpClientWrapper();
            var reporter   = new CertificateCheckReporter(inputWriter);
            var targetUrls = TargetUrl.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var targetUrl in targetUrls)
            {
                var healthCheck = new CertificateValidityCheck(
                    App.Title,
                    targetUrl,
                    ValidityDays);

                _certificateCheckTasks.Add(new CertificateCheckTask(
                                               healthCheck,
                                               TimeSpan.FromSeconds(IntervalSeconds),
                                               reporter,
                                               _httpClientWrapper,
                                               Log));
            }
        }
        private static async Task Run(CertificateValidityCheck certificateCheck, TimeSpan interval, CertificateCheckReporter reporter, HttpClientWrapper httpClientWrapper, ILogger diagnosticLog, CancellationToken cancel)
        {
            try
            {
                while (!cancel.IsCancellationRequested)
                {
                    var sw     = Stopwatch.StartNew();
                    var result = await certificateCheck.CheckNow(httpClientWrapper, cancel, diagnosticLog).ConfigureAwait(false);

                    reporter.Report(result);
                    sw.Stop();
                    var total = sw.Elapsed.TotalMilliseconds;

                    if (total < interval.TotalMilliseconds)
                    {
                        var delay = (int)(interval.TotalMilliseconds - total);
                        await Task.Delay(delay, cancel).ConfigureAwait(false);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                // Unloading
            }
            catch (Exception ex)
            {
                diagnosticLog.Fatal(ex, "The health check task threw an unhandled exception");
            }
        }
 public CertificateCheckTask(CertificateValidityCheck certificateCheck, TimeSpan interval, CertificateCheckReporter reporter, HttpClientWrapper httpClientWrapper, ILogger diagnosticLog)
 {
     if (certificateCheck == null)
     {
         throw new ArgumentNullException(nameof(certificateCheck));
     }
     if (reporter == null)
     {
         throw new ArgumentNullException(nameof(reporter));
     }
     _httpClientWrapper    = httpClientWrapper;
     _certificateCheckTask =
         Task.Run(() => Run(certificateCheck, interval, reporter, _httpClientWrapper, diagnosticLog, _cancel.Token), _cancel.Token);
 }