示例#1
0
        /// <summary>
        /// Provides a custom healthcheck endpoint for each service to configure
        /// </summary>
        /// <remarks>The healthcheck if specified at plugin registration is executed</remarks>
        public object Get(HealthCheck check)
        {
            var healthCheck = HostContext.GetPlugin <ConsulFeature>().Settings.GetHealthCheck();

            if (healthCheck == null)
            {
                return(new HealthCheck(ServiceHealth.Ok, "Not implemented"));
            }

            try
            {
                // based on https://www.consul.io/docs/agent/checks.html
                var result = healthCheck.HealthCheckDelegate.Invoke(HostContext.AppHost);
                switch (result.HealthResult)
                {
                case ServiceHealth.Warning:
                    // return nonstandard code http://stackoverflow.com/a/22645395/191877
                    return(new HttpError((System.Net.HttpStatusCode) 429, result.Message));

                case ServiceHealth.Critical:
                    return(new HttpError(HttpStatusCode.ServiceUnavailable, result.Message));
                }
                return(result);
            }
            catch (Exception ex)
            {
                return(new HttpError(HttpStatusCode.ServiceUnavailable, $"Health check threw unexpected exception {ex}"));
            }
        }
        /// <summary>
        /// Provides a custom healthcheck endpoint for each service to configure
        /// </summary>
        /// <remarks>The healthcheck if specified at plugin registration is executed</remarks>
        public object Get(HealthCheck check)
        {
            var healthCheck = HostContext.GetPlugin<ConsulFeature>().Settings.GetHealthCheck();
            if (healthCheck == null) return new HealthCheck(ServiceHealth.Ok, "Not implemented");

            try
            {
                // based on https://www.consul.io/docs/agent/checks.html
                var result = healthCheck.HealthCheckDelegate.Invoke(HostContext.AppHost);
                switch (result.HealthResult)
                {
                    case ServiceHealth.Warning:
                        // return nonstandard code http://stackoverflow.com/a/22645395/191877
                        return new HttpError((System.Net.HttpStatusCode)429, result.Message);
                    case ServiceHealth.Critical:
                        return new HttpError(HttpStatusCode.ServiceUnavailable, result.Message);
                }
                return result;
            }
            catch (Exception ex)
            {
                return new HttpError(HttpStatusCode.ServiceUnavailable, $"Health check threw unexpected exception {ex}");
            }
        }