public void Process(HealthCheckPipelineArgs args)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                try
                {
                    _watch.Restart();

                    connection.Open();

                    using (var command = connection.CreateCommand())
                    {
                        command.CommandTimeout = _commandTimeout;
                        command.CommandText    = "SELECT 1";
                        command.ExecuteScalar();
                    }

                    args.AddMessage($"{_name} OK ({_watch.ElapsedMilliseconds}ms)", PipelineMessageType.Information);
                }
                catch (Exception ex) when(ex is InvalidOperationException || ex is SqlException || ex is ConfigurationErrorsException)
                {
                    args.AddMessage($"{_name} failed ({_watch.ElapsedMilliseconds}ms)", PipelineMessageType.Error);
                }
                finally
                {
                    _watch.Stop();
                }
            }
        }
Exemplo n.º 2
0
        public void Process(HealthCheckPipelineArgs args)
        {
            var request = (HttpWebRequest)WebRequest.Create(_uri);

            request.Timeout           = _timeoutMilliseconds;
            request.Method            = _method;
            request.AllowAutoRedirect = true;

            try
            {
                _watch.Restart();

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var statusCode = response.StatusCode;

                    if (statusCode == _expectedStatusCode)
                    {
                        args.AddMessage($"{_name} OK ({_watch.ElapsedMilliseconds}ms)", PipelineMessageType.Information);
                    }
                    else
                    {
                        args.AddMessage($"{_name} failed ({_watch.ElapsedMilliseconds}ms), status code was {statusCode}", PipelineMessageType.Error);
                    }
                }
            }
            catch
            {
                args.AddMessage($"{_name} failed ({_watch.ElapsedMilliseconds}ms)", PipelineMessageType.Error);
            }
            finally
            {
                _watch.Stop();
            }
        }
Exemplo n.º 3
0
        public override void Process(PreprocessRequestArgs args)
        {
            var context = args.HttpContext;

            if (context.Request.HttpMethod != HttpMethod.Get.Method && context.Request.HttpMethod != HttpMethod.Head.Method)
            {
                return;
            }

            if (!(context.Request.Path.Equals(_livenessPath, StringComparison.OrdinalIgnoreCase) ||
                  context.Request.Path.Equals(_readinessPath, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }

            args.AbortPipeline();

            context.Items["SitecoreOn"] = false;

            var pipelineArgs = new HealthCheckPipelineArgs();

            if (context.Request.Path.Equals(_livenessPath, StringComparison.OrdinalIgnoreCase))
            {
                CorePipeline.Run(LivenessHealthCheckPipelineName, pipelineArgs, true);
            }
            else if (context.Request.Path.Equals(_readinessPath, StringComparison.OrdinalIgnoreCase))
            {
                CorePipeline.Run(ReadinessHealthCheckPipelineName, pipelineArgs, true);
            }

            context.Response.ContentType = "text/plain";

            foreach (var message in pipelineArgs.GetAllMessages())
            {
                context.Response.Write(message + Environment.NewLine);
            }

            if (pipelineArgs.IsSuccess())
            {
                context.Response.StatusCode = 200;
            }
            else
            {
                context.Response.TrySkipIisCustomErrors = true;
                context.Response.StatusCode             = 503;
            }

            context.ApplicationInstance.CompleteRequest();
        }
 public void Process(HealthCheckPipelineArgs args)
 {
     args.AddMessage("OK", PipelineMessageType.Information);
 }