Beispiel #1
0
        private async Task HandleServiceStatusRequest(HttpContext context, IEnumerable <IServiceStatusCheck> serviceStatusChecks)
        {
            string responsibility = context.Request.Query["responsibility"].ToString() ?? string.Empty;

            // Create a queryable list of service status checks
            IQueryable <IServiceStatusCheck> checksToMake = serviceStatusChecks.AsQueryable().Where(check => check.IsEnabled());

            // If the responsibility string has been set
            // filter to only the responsibilities that are requested
            if (!string.IsNullOrEmpty(responsibility))
            {
                checksToMake = checksToMake.Where(x => x.Responsibilities.Any(y => y.Key.Equals(responsibility, StringComparison.OrdinalIgnoreCase)));
            }

            // No checks to make
            if (!checksToMake.Any())
            {
                context.Response.StatusCode = 404;
            }

            // Prepare a list of tasks to run through
            var checkTasks = checksToMake.ToDictionary(x => x, x => DoServiceCheck(x));

            await Task.WhenAll(checkTasks.Values);

            // Get the result of service checks in a new dictionary
            var checks = checkTasks.ToDictionary(x => x.Key, x => x.Value.Result);

            // Initialize the status object that we will be returning
            var status = new ServiceStatusDetailed(checks)
            {
                // Set version of your service
                Version = _settings.Value?.Version ?? "0.0.0.0",
                Branch  = _settings.Value?.Branch ?? null
            };

            // Validate the status of this service,
            // where no CORE responsibilities are allowed to fail
            status.ValidateStatus(ResponsibilityTypes.Core);

            context.Response.StatusCode  = 200;
            context.Response.ContentType = "application/json";
            await context.Response.WriteAsync(JsonConvert.SerializeObject(status));
        }
Beispiel #2
0
        /// <summary>
        /// Evaluate the response from a HTTP service
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        public override async Task <bool> EvaluateResponse(HttpResponseMessage response)
        {
            if (response.IsSuccessStatusCode)
            {
                string result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                ServiceStatusDetailed serviceStatus = JsonConvert.DeserializeObject <ServiceStatusDetailed>(result);

                if (serviceStatus?.Responsibilities != null)
                {
                    return(_responsibilities.All(x =>
                    {
                        KeyValuePair <string, string> responsibility = serviceStatus.Responsibilities.FirstOrDefault(y => string.Compare(y.Key, x, true) == 0);
                        return string.Compare(responsibility.Value, StatusTypes.OK, true) == 0;
                    }));
                }
            }

            return(false);
        }