Ejemplo n.º 1
0
        public async Task <IEnumerable <LivenessResult> > IsHealthy(string path, BeatPulseOptions options, HttpContext httpContext)
        {
            _logger.LogInformation($"BeatPulse is checking health on all registered liveness on [BeatPulsePath]/{path}.");

            using (_logger.BeginScope($"BeatPulse is checking health status on all registered liveness from [BeatPulsePath]/{path} path."))
            {
                var livenessResults = new List <LivenessResult>();

                foreach (var registration in _beatPulseContext.GetAllLiveness(path))
                {
                    var liveness = _beatPulseContext
                                   .CreateLivenessFromRegistration(registration);

                    var executionContext = LivenessExecutionContext
                                           .FromRegistration(registration, showDetailedErrors: options.DetailedErrors);

                    var livenessResult = await RunLiveness(liveness, executionContext, options);

                    await RunTrackers(livenessResult);

                    livenessResults.Add(livenessResult);

                    if (!livenessResult.IsHealthy && !options.DetailedOutput)
                    {
                        //if is unhealthy and not detailed options is true return inmediatly

                        _logger.LogWarning($"Liveness {executionContext.Name} is not healthy. Breaking liveness execution because detailed output is false.");

                        return(livenessResults);
                    }
                }

                return(livenessResults);
            }
        }
        public async Task execute_definded_action_for_health_check()
        {
            var taskResult = Task.FromResult(LivenessResult.UnHealthy("action liveness is not working"));

            var livenessContext = new LivenessExecutionContext();

            var liveness = new ActionLiveness((cancellationToken) => taskResult);

            (await liveness.IsHealthy(livenessContext))
            .Should().Be(taskResult.Result);
        }
Ejemplo n.º 3
0
        public async Task execute_definded_action_for_health_check()
        {
            var taskResult = Task.FromResult((string.Empty, false));

            var livenessContext = new LivenessExecutionContext();

            var liveness = new ActionLiveness((context, cancellationToken) => taskResult);

            (await liveness.IsHealthy(new DefaultHttpContext(), livenessContext))
            .Should().Be(taskResult.Result);
        }
Ejemplo n.º 4
0
        async Task <LivenessResult> RunLiveness(IBeatPulseLiveness liveness, LivenessExecutionContext livenessContext, HttpContext httpContext, BeatPulseOptions beatPulseOptions)
        {
            _logger.LogInformation($"Executing liveness {livenessContext.Name}.");

            var livenessResult = new LivenessResult(livenessContext.Name, livenessContext.Path);

            livenessResult.StartCounter();

            try
            {
                using (var cancellationTokenSource = new CancellationTokenSource())
                {
                    var livenessTask = liveness.IsHealthy(httpContext, livenessContext, cancellationTokenSource.Token);

                    if (await Task.WhenAny(livenessTask, Task.Delay(beatPulseOptions.Timeout, cancellationTokenSource.Token)) == livenessTask)
                    {
                        // The liveness is executed successfully and get the results
                        var(message, healthy) = await livenessTask;

                        livenessResult.StopCounter(message, healthy);

                        _logger.LogInformation($"The liveness {livenessContext.Name} is executed.");
                    }
                    else
                    {
                        // The liveness is timeout ( from configured options)
                        _logger.LogWarning($"The liveness {livenessContext.Name} is timeout, execution is cancelled.");

                        cancellationTokenSource.Cancel();

                        livenessResult.StopCounter(BeatPulseKeys.BEATPULSE_TIMEOUT_MESSAGE, false);
                    }
                }
            }
            catch (Exception ex)
            {
                // The uri executed is now well formed, dns not found
                // or any unexpected errror

                _logger.LogError(ex, $"The liveness {livenessContext.Name} is unhealthy.");

                var message = _environment.IsDevelopment()
                    ? ex.Message : string.Format(BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_ERROR_MESSAGE, livenessContext.Name);

                livenessResult.StopCounter(message, false);
            }

            return(livenessResult);
        }
Ejemplo n.º 5
0
        async Task <LivenessResult> RunLiveness(IBeatPulseLiveness liveness, LivenessExecutionContext executionContext, BeatPulseOptions options)
        {
            _logger.LogInformation($"Executing liveness {executionContext.Name}.");

            var clock = Clock.StartNew();

            try
            {
                using (var cancellationTokenSource = new CancellationTokenSource())
                {
                    var livenessTask = liveness.IsHealthy(executionContext, cancellationTokenSource.Token);

                    if (await Task.WhenAny(livenessTask, Task.Delay(options.Timeout, cancellationTokenSource.Token)) == livenessTask)
                    {
                        _logger.LogInformation($"The liveness {executionContext.Name} is executed.");

                        return((await livenessTask)
                               .SetEnforced(name: executionContext.Name, path: executionContext.Path, duration: clock.Elapsed(), detailedErrors: options.DetailedErrors));
                    }
                    else
                    {
                        _logger.LogWarning($"The liveness {executionContext.Name} return timeout, execution is cancelled.");

                        cancellationTokenSource.Cancel();

                        return(LivenessResult.TimedOut()
                               .SetEnforced(name: executionContext.Name, path: executionContext.Path, duration: clock.Elapsed(), detailedErrors: options.DetailedErrors));
                    }
                }
            }
            catch (Exception ex)
            {
                // The uri executed is now well formed, dns not found
                // or any other unexpected exceptions from liveness executions.

                _logger.LogError(ex, $"The liveness {executionContext.Name} is unhealthy.");

                return(LivenessResult.UnHealthy(ex)
                       .SetEnforced(name: executionContext.Name, path: executionContext.Path, duration: clock.Elapsed(), detailedErrors: options.DetailedErrors));
            }
        }
Ejemplo n.º 6
0
 public Task <LivenessResult> IsHealthy(LivenessExecutionContext context, CancellationToken cancellationToken = default)
 {
     return(_check(cancellationToken));
 }