-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds StorageInitialized health check (#4674)
- Loading branch information
1 parent
e273b68
commit e86ec89
Showing
8 changed files
with
121 additions
and
3 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
45 changes: 45 additions & 0 deletions
45
src/Microsoft.Health.Fhir.Api/Features/Health/StorageInitializedHealthCheck.cs
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,45 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Health.Core; | ||
using Microsoft.Health.Fhir.Core.Extensions; | ||
using Microsoft.Health.Fhir.Core.Messages.Storage; | ||
|
||
namespace Microsoft.Health.Fhir.Api.Features.Health | ||
{ | ||
public class StorageInitializedHealthCheck : IHealthCheck, INotificationHandler<StorageInitializedNotification> | ||
{ | ||
private const string SuccessfullyInitializedMessage = "Successfully initialized."; | ||
private bool _storageReady; | ||
private readonly DateTimeOffset _started = Clock.UtcNow; | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
if (_storageReady) | ||
{ | ||
return Task.FromResult(HealthCheckResult.Healthy(SuccessfullyInitializedMessage)); | ||
} | ||
|
||
TimeSpan waited = Clock.UtcNow - _started; | ||
if (waited < TimeSpan.FromMinutes(5)) | ||
{ | ||
return Task.FromResult(new HealthCheckResult(HealthStatus.Degraded, $"Storage is initializing. Waited: {(int)waited.TotalSeconds}s.")); | ||
} | ||
|
||
return Task.FromResult(new HealthCheckResult(HealthStatus.Unhealthy, $"Storage has not been initialized. Waited: {(int)waited.TotalSeconds}s.")); | ||
} | ||
|
||
public Task Handle(StorageInitializedNotification notification, CancellationToken cancellationToken) | ||
{ | ||
_storageReady = true; | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...ft.Health.Fhir.Shared.Api.UnitTests/Features/Health/StorageInitializedHealthCheckTests.cs
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,52 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Health.Fhir.Core.Extensions; | ||
using Microsoft.Health.Fhir.Core.Messages.Storage; | ||
using Microsoft.Health.Fhir.Tests.Common; | ||
using Microsoft.Health.Test.Utilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.Health.Fhir.Api.Features.Health; | ||
|
||
[Trait(Traits.OwningTeam, OwningTeam.Fhir)] | ||
[Trait(Traits.Category, Categories.DataSourceValidation)] | ||
public class StorageInitializedHealthCheckTests | ||
{ | ||
private readonly StorageInitializedHealthCheck _sut = new(); | ||
|
||
[Fact] | ||
public async Task GivenStorageInitialized_WhenCheckHealthAsync_ThenReturnsHealthy() | ||
{ | ||
await _sut.Handle(new StorageInitializedNotification(), CancellationToken.None); | ||
|
||
HealthCheckResult result = await _sut.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); | ||
|
||
Assert.Equal(HealthStatus.Healthy, result.Status); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenStorageInitializedHealthCheck_WhenCheckHealthAsync_ThenStartsAsDegraded() | ||
{ | ||
HealthCheckResult result = await _sut.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); | ||
Assert.Equal(HealthStatus.Degraded, result.Status); | ||
} | ||
|
||
#if NET8_0_OR_GREATER | ||
[Fact] | ||
public async Task GivenStorageInitializedHealthCheck_WhenCheckHealthAsync_ThenChangedToUnhealthyAfter5Minute() | ||
{ | ||
using (Mock.Property(() => ClockResolver.TimeProvider, new Microsoft.Extensions.Time.Testing.FakeTimeProvider(DateTimeOffset.Now.AddMinutes(5).AddSeconds(1)))) | ||
{ | ||
HealthCheckResult result = await _sut.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); | ||
Assert.Equal(HealthStatus.Unhealthy, result.Status); | ||
} | ||
} | ||
#endif | ||
} |
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