async Task <LivenessResult> RunLiveness(IBeatPulseLiveness liveness, HttpContext httpContext, CancellationToken cancellationToken) { _logger.LogInformation($"Executing liveness {liveness.Name}."); var livenessResult = new LivenessResult(liveness.Name, liveness.DefaultPath); livenessResult.StartCounter(); try { var(message, healthy) = await liveness.IsHealthy(httpContext, _environment.IsDevelopment(), cancellationToken); livenessResult.StopCounter(message, healthy); _logger.LogInformation($"The liveness {liveness.Name} is executed."); } catch (Exception ex) { _logger.LogError(ex, $"The liveness {liveness.Name} is unhealthy."); var message = _environment.IsDevelopment() ? ex.Message : string.Format(BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_ERROR_MESSAGE, liveness.Name); livenessResult.StopCounter(message, false); } return(livenessResult); }
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); }
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); }
Task RunTrackers(LivenessResult responses) { _logger.LogInformation("Sending liveness result to all configured trackers."); var trackerTasks = new List <Task>(); if (_beatPulseContext.GetAllTrackers() != null) { foreach (var tracker in _beatPulseContext.GetAllTrackers()) { _logger.LogInformation($"Sending liveness result to tracker {tracker.GetType().FullName}."); trackerTasks.Add(tracker.Track(responses)); } } return(Task.WhenAll(trackerTasks)); }
public void ConfigureServices(IServiceCollection services) { string name; string path; string path2; var taskResult = Task.FromResult( LivenessResult.Healthy()); services.AddBeatPulse(context => { //liveness context.AddLiveness(nameof(name), opt => { opt.UsePath(nameof(path)); opt.UseLiveness(new ActionLiveness((cancellationToken) => taskResult)); }); context.AddLiveness(nameof(path2), opt => { opt.UseFactory(sp => { var service = sp.GetRequiredService <FooService>(); return(new ActionLiveness((cancellationToken) => taskResult)); }); opt.UsePath(nameof(path2)); }); //trackers context.AddTracker(new TestTracker()); context.AddTracker(sp => { var fooService = sp.GetRequiredService <FooService>(); return(new TestTrackerWithDependencies(fooService)); }); }); services.AddTransient <FooService>(); }
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)); } }
public Task Track(LivenessResult response) { return(Task.CompletedTask); }