public async Task Invoke(HttpContext httpContext)
        {
            var httpMethod = httpContext.Request.Method;
            var path       = httpContext.Request.Path.Value;

            // check request security
            if (_appOptions.SecurityHandler != null)
            {
                var validRequest = _appOptions.SecurityHandler.Invoke(httpContext.Request);
                if (!validRequest)
                {
                    httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                    return;
                }
            }

            if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{_appOptions.RoutePrefix}/?$"))
            {
                var healthCheckResult = await _healthChecker.Check().ConfigureAwait(false);

                if (healthCheckResult == null)
                {
                    throw new ArgumentNullException(nameof(healthCheckResult));
                }

                httpContext.Response.StatusCode  = (int)healthCheckResult.HttpStatus;
                httpContext.Response.ContentType = "application/json";
                var jsonString = JsonConvert.SerializeObject(healthCheckResult);
                await httpContext.Response.WriteAsync(jsonString, Encoding.UTF8).ConfigureAwait(false);

                return;
            }

            await _next(httpContext);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <IEnumerable <HealthStatus> > > GetHealthStatus()
        {
            // just for test !!!

            IUrl url        = new Url("https://www.dotnettips.info/", _client);
            IUrl url2       = new Url("https://www.dotnettips.info/", _client, "post/3003/شروع-به-کار-با-dntframeworkcore-قسمت-5-مکانیزم-eventing-و-استفاده-از-سرویس‌های-موجودیت‌ها");
            IUrl url3       = new Url("http://random-domain.ir/", _client);
            IUrl url4       = new Url("http://ali895.com/", _client);
            IUrl invalidUrl = new Url("", _client);

            var urls = new Urls(new[] { url, url2, url3, url4, invalidUrl });


            var healthStatuses = await _healthChecker.Check(urls);

            //----------------

            await _healthChecker.UnavailableUrls(urls);

            //-----------------

            //await _healthChecker.AvailableUrls(urls);

            await _healthJob.Save(healthStatuses);

            return(healthStatuses);
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <IEnumerable <HealthStatus> > > GetHealthStatus()
        {
            var healthStatuses = await _healthChecker.Check(_urls.Value);

            await _healthJob.Save(healthStatuses);

            return(healthStatuses);
        }
        public IHttpActionResult HealthCheck(bool?details = false, bool?warn = false)
        {
            var result = healthChecker.Check(
                new List <IHealthCheck>()
            {
                new ApplicationDatabaseHealthCheck()
            }
                );

            var response = Request.CreateResponse();

            response.Headers.CacheControl = new CacheControlHeaderValue
            {
                NoCache        = true,
                NoStore        = true,
                MustRevalidate = true
            };

            if (!warn.HasValue || warn.Value)
            {
                if (result.Status == HealthCheckStatus.Healthy)
                {
                    response.StatusCode = HttpStatusCode.OK;
                    response.Content    = new StringContent(HealthCheckSuccessfulResponseContent);
                }
                else
                {
                    response.StatusCode = HttpStatusCode.ServiceUnavailable;
                    response.Content    = new StringContent(HealthCheckFailedResponseContent);
                }
            }
            else if (!details.HasValue || details.Value)
            {
                response.StatusCode = HttpStatusCode.OK;
                response.Content    = new StringContent(
                    JsonConvert.SerializeObject(
                        result,
                        Formatting.Indented,
                        new StringEnumConverter()
                        )
                    );
            }
            else
            {
                if (result.Status == HealthCheckStatus.Error)
                {
                    response.StatusCode = HttpStatusCode.ServiceUnavailable;
                    response.Content    = new StringContent(HealthCheckFailedResponseContent);
                }
                else
                {
                    response.StatusCode = HttpStatusCode.OK;
                    response.Content    = new StringContent(HealthCheckSuccessfulResponseContent);
                }
            }

            return(ResponseMessage(response));
        }
Ejemplo n.º 5
0
        public void ShouldTimeOutWhenHealthCheckTimeOutIsSpecified()
        {
            IHealthChecker healthChecker = GetHealthChecker(b => b.WithHealthChecks(() => new[] { new SlowRunningHealthCheck(5000) }).WithTimeout(TimeSpan.FromMilliseconds(500)));

            var result = healthChecker.Check();

            result.Status.ShouldBe(HealthCheckStatus.NotOkay);
            result.Results.Length.ShouldBe(1);
            ((FailedIndividualHealthCheckResult)result.Results[0]).FailureReason.ShouldBe("Health check timed out.");
        }
        private Response ExecuteHealthChecks()
        {
            var result     = _healthChecker.Check();
            var statusCode = result.Status == HealthCheckStatus.Okay ? 200 : 503;
            var response   = Response.AsJson(result)
                             .WithStatusCode(statusCode)
                             .WithContentType("text/plain")
                             .WithHeader("Content-Disposition", "inline")
                             .WithHeader("Cache-Control", "no-cache");

            return(response);
        }
Ejemplo n.º 7
0
        private async Task ProcessRequest(HttpContext context)
        {
            var result = _healthChecker.Check();

            var response = context.Response;
            var json     = JsonConvert.SerializeObject(result, Formatting.Indented, new StringEnumConverter());

            response.StatusCode  = result.Status == HealthCheckStatus.Okay ? 200 : 503;
            response.ContentType = "text/plain";
            response.Headers["Content-Disposition"] = "inline";
            response.Headers["Cache-Control"]       = "no-cache";

            await response.WriteAsync(json);
        }
        private async Task <HealthReport> CheckHealth(string login)
        {
            if (!_telemetryStorage.Exist(login))
            {
                Response.StatusCode = 404;
                return(new HealthReport());
            }

            var bodyTelemetry = await _telemetryStorage.Get(login);

            var healthReport = await _healthChecker.Check(bodyTelemetry);

            return(healthReport);
        }
Ejemplo n.º 9
0
        public void ProcessRequest(HttpContext context)
        {
            var result = _healthChecker.Check();

            var response = context.Response;
            var json     = JsonConvert.SerializeObject(result, Formatting.Indented, new StringEnumConverter());

            response.StatusCode  = result.Status == HealthCheckStatus.Okay ? 200 : 503;
            response.ContentType = "text/plain";
            response.Headers["Content-Disposition"] = "inline";
            response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.Write(json);
            response.End();
        }
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            return(Task.Factory.StartNew(() =>
            {
                var result = _healthChecker.Check();

                var json = JsonConvert.SerializeObject(result, Formatting.Indented, new StringEnumConverter());
                var statusCode1 = result.Status == HealthCheckStatus.Okay ? HttpStatusCode.OK : HttpStatusCode.ServiceUnavailable;

                var response = request.CreateResponse(statusCode1);
                response.Content = new StringContent(json);
                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    NoCache = true
                };

                return response;
            },
                                         cancellationToken));
        }
Ejemplo n.º 11
0
        public async Task <ActionResult <IEnumerable <HealthStatus> > > GetHealthStatus()
        {
            var result = await _healthChecker.Check(_urls.Value);

            return(result);
        }