Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blob Storage to Health Check #902

Merged
merged 30 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0991939
feat: Add Azure Blob Storage to Health Check
MH321Productions Sep 25, 2024
22764db
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Sep 26, 2024
fae71fa
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Sep 27, 2024
22a3e47
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Sep 27, 2024
77ab3c8
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Sep 29, 2024
41d6521
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Sep 30, 2024
035343a
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Sep 30, 2024
5a38bdf
chore: Move Azure Blob Storage Health Check to Files Module
MH321Productions Sep 30, 2024
98346c5
feat: Add Google Cloud Storage Health Check
MH321Productions Oct 7, 2024
3ec4a7a
Merge branch 'main' into add-azure-blob-storage-to-health-check
MH321Productions Oct 7, 2024
1db7d70
Merge branch 'main' into add-azure-blob-storage-to-health-check
MH321Productions Oct 7, 2024
398f7e4
test: Change Dependency on Azure Storage emulator
MH321Productions Oct 7, 2024
b11f477
chore: Revert dependency change
MH321Productions Oct 7, 2024
b2ec054
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 8, 2024
695e7f0
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 8, 2024
cdc5699
refactor: Move the health checks into their respective AddStorage met…
MH321Productions Oct 8, 2024
7a63014
refactor: Rewrite and simplify Google Cloud Health Check
MH321Productions Oct 8, 2024
e3ec39d
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 8, 2024
3701862
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 9, 2024
49fcfe8
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 10, 2024
af80fd8
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 11, 2024
5c45d20
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 11, 2024
5bb3c8c
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 11, 2024
d428b8b
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 11, 2024
5c9aa02
refactor: Rewrite Google Cloud Storage Health Check as common Health …
MH321Productions Oct 11, 2024
b559bb2
refactor: Use common health check
MH321Productions Oct 11, 2024
e17e3bc
Merge remote-tracking branch 'origin/add-azure-blob-storage-to-health…
MH321Productions Oct 11, 2024
2385c64
refactor: Register the common Health check in the common `AddBlobStor…
MH321Productions Oct 11, 2024
0c1bf01
Merge branch 'main' into add-azure-blob-storage-to-health-check
mergify[bot] Oct 14, 2024
39875c1
chore: replace Azure blob storage library with a more generic one
tnotheis Oct 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="Serilog" Version="4.0.2" />
<PackageReference Include="System.Interactive.Async" Version="6.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.Azure.Storage.Blobs" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Text;
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.Persistence.BlobStorage;
using Backbone.Tooling.Extensions;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Backbone.BuildingBlocks.Infrastructure.Persistence.BlobStorage;

public class BlobStorageHealthCheck : IHealthCheck
{
private const string FILE_TEXT = "healthcheck";
private const int MAX_NUMBER_OF_TRIES = 5;

private static bool? _isHealthy;
private static int _numberOfTries = 0;

private readonly IBlobStorage _storage;
private readonly string _bucketName;

public BlobStorageHealthCheck(IBlobStorage storage, string bucketName)
{
_storage = storage;
_bucketName = bucketName;
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
if (!_isHealthy.HasValue || (_isHealthy == false && _numberOfTries < MAX_NUMBER_OF_TRIES))
{
var filename = Guid.NewGuid().ToString();
var isUploadPossible = await IsUploadPossible(filename);
var isDownloadPossible = await IsDownloadPossible(filename);
var isDeletionPossible = await IsDeletionPossible(filename);

_isHealthy = isUploadPossible && isDownloadPossible && isDeletionPossible;
_numberOfTries++;
}

return _isHealthy.Value ? HealthCheckResult.Healthy() : HealthCheckResult.Unhealthy();
}

private async Task<bool> IsUploadPossible(string filename)
{
try
{
_storage.Add(_bucketName, filename, FILE_TEXT.GetBytes());
await _storage.SaveAsync();
return true;
}
catch (Exception)
{
return false;
}
}

private async Task<bool> IsDownloadPossible(string filename)
{
try
{
var downloadBytes = await _storage.FindAsync(_bucketName, filename);
var downloadedString = Encoding.UTF8.GetString(downloadBytes);
return downloadedString == FILE_TEXT;
}
catch (Exception)
{
return false;
}
}

private async Task<bool> IsDeletionPossible(string filename)
{
try
{
_storage.Remove(_bucketName, filename);
await _storage.SaveAsync();
return true;
}
catch (Exception)
{
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.Persistence.BlobStorage;
using Backbone.BuildingBlocks.Infrastructure.Persistence.BlobStorage.AzureStorageAccount;
using Backbone.BuildingBlocks.Infrastructure.Persistence.BlobStorage.GoogleCloudStorage;
using Backbone.Tooling.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Backbone.BuildingBlocks.Infrastructure.Persistence.BlobStorage;

Expand Down Expand Up @@ -41,6 +43,14 @@ public static void AddBlobStorage(this IServiceCollection services, BlobStorageO
$"{options.CloudProvider} is not a currently supported cloud provider.");
}
}

services.AddHealthChecks().Add(
new HealthCheckRegistration(
"blob_storage",
sp => new BlobStorageHealthCheck(sp.GetRequiredService<IBlobStorage>(), options.Container),
HealthStatus.Unhealthy, null
)
);
}
}

Expand Down