forked from dotnet/aspire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop health checks running until the underlying resource enters the r…
…unning state. (dotnet#5601)
- Loading branch information
1 parent
24b51a1
commit f0ab1b9
Showing
5 changed files
with
121 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning", | ||
"Aspire.Hosting.Dcp": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Hosting.ApplicationModel; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Aspire.Hosting.Health; | ||
|
||
internal class ResourceHealthCheckScheduler : BackgroundService | ||
{ | ||
private readonly ResourceNotificationService _resourceNotificationService; | ||
private readonly DistributedApplicationModel _model; | ||
private readonly Dictionary<string, bool> _checkEnablement = new(); | ||
|
||
public ResourceHealthCheckScheduler(ResourceNotificationService resourceNotificationService, DistributedApplicationModel model) | ||
{ | ||
_resourceNotificationService = resourceNotificationService ?? throw new ArgumentNullException(nameof(resourceNotificationService)); | ||
_model = model ?? throw new ArgumentNullException(nameof(model)); | ||
|
||
foreach (var resource in model.Resources) | ||
{ | ||
UpdateCheckEnablement(resource, false); | ||
} | ||
|
||
Predicate = (check) => | ||
{ | ||
return _checkEnablement.TryGetValue(check.Name, out var enabled) ? enabled : false; | ||
}; | ||
} | ||
|
||
public Func<HealthCheckRegistration, bool> Predicate { get; init; } | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
var resourceEvents = _resourceNotificationService.WatchAsync(stoppingToken); | ||
|
||
await foreach (var resourceEvent in resourceEvents) | ||
{ | ||
if (resourceEvent.Snapshot.State == KnownResourceStates.Running) | ||
{ | ||
// Each time we receive an event that tells us that the resource is | ||
// running we need to enable the health check annotation. | ||
UpdateCheckEnablement(resourceEvent.Resource, true); | ||
} | ||
} | ||
} | ||
|
||
private void UpdateCheckEnablement(IResource resource, bool enabled) | ||
{ | ||
if (resource.TryGetAnnotationsOfType<HealthCheckAnnotation>(out var annotations)) | ||
{ | ||
foreach (var annotation in annotations) | ||
{ | ||
_checkEnablement[annotation.Key] = enabled; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters