Skip to content

Commit

Permalink
Change health check to test if a single bucket exists (#72)
Browse files Browse the repository at this point in the history
* Add Rider-specific files to the .gitignore

* Change health check to test if a single bucket with a customizable name exists ('health' by default) instead of listing all buckets
  • Loading branch information
cvetomir-todorov authored Mar 7, 2024
1 parent 035d7fc commit e70f172
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ bld/
[Bb]in/
[Oo]bj/

# IDE-specific files (Rider)
.idea

# Visual Studio 2015 cache/options directory
.vs/
.vscode/
Expand Down
8 changes: 6 additions & 2 deletions src/Minio.AspNetCore.HealthChecks/MinioHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Minio.DataModel.Args;

namespace Minio.AspNetCore.HealthChecks
{
public class MinioHealthCheck : IHealthCheck
{
private readonly MinioClient minioClient;
private readonly string bucket;

public MinioHealthCheck(MinioClient minioClient)
public MinioHealthCheck(MinioClient minioClient, string bucket)
{
this.minioClient = minioClient;
this.bucket = bucket;
}

public async Task<HealthCheckResult> CheckHealthAsync(
Expand All @@ -17,7 +20,8 @@ public async Task<HealthCheckResult> CheckHealthAsync(
{
try
{
await minioClient.ListBucketsAsync(cancellationToken).ConfigureAwait(false);
var bucketExistsArgs = new BucketExistsArgs().WithBucket(bucket);
await minioClient.BucketExistsAsync(bucketExistsArgs, cancellationToken).ConfigureAwait(false);

return HealthCheckResult.Healthy();
}
Expand Down
16 changes: 15 additions & 1 deletion src/Minio.AspNetCore.HealthChecks/MinioHealthChecksExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ namespace Minio.AspNetCore.HealthChecks
{
public static class MinioHealthChecksExtensions
{
/// <summary>
/// Adds a health check for MinIO to the specified <see cref="builder"/>.
/// The health check tests if a bucket with the specified <see cref="bucket"/> name exists.
/// Whether the bucket actually exists doesn't matter - the health check is successful if the call to MinIO doesn't throw.
/// </summary>
public static IHealthChecksBuilder AddMinio(
this IHealthChecksBuilder builder,
Func<IServiceProvider, MinioClient> factory,
string name = "minio",
string bucket = "health",
HealthStatus? failureStatus = null,
IEnumerable<string>? tags = null,
TimeSpan? timeout = null)
Expand All @@ -17,10 +23,18 @@ public static IHealthChecksBuilder AddMinio(
{
throw new ArgumentNullException(nameof(builder));
}
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Health check name should not be null or whitespace.", nameof(name));
}
if (string.IsNullOrWhiteSpace(bucket))
{
throw new ArgumentException("Health check bucket should not be null or whitespace.", nameof(bucket));
}

return builder.Add(new HealthCheckRegistration(
name,
sp => new MinioHealthCheck(factory.Invoke(sp)),
sp => new MinioHealthCheck(factory.Invoke(sp), bucket),
failureStatus,
tags,
timeout));
Expand Down

0 comments on commit e70f172

Please sign in to comment.