| | | 1 | | using Azure.Storage.Blobs; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Api.Services; |
| | | 5 | | |
| | | 6 | | public class BlobStorageHealthCheckService : HealthCheckServiceBase |
| | | 7 | | { |
| | | 8 | | private readonly IConfiguration _configuration; |
| | | 9 | | |
| | | 10 | | public BlobStorageHealthCheckService( |
| | | 11 | | IConfiguration configuration, |
| | | 12 | | ILogger<BlobStorageHealthCheckService> logger) |
| | 0 | 13 | | : base(logger) |
| | | 14 | | { |
| | 0 | 15 | | _configuration = configuration; |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | protected override async Task<(string Status, string? Message)> PerformHealthCheckAsync() |
| | | 19 | | { |
| | 0 | 20 | | var connectionString = _configuration["BlobStorage:ConnectionString"]; |
| | 0 | 21 | | var containerName = _configuration["BlobStorage:ContainerName"]; |
| | | 22 | | |
| | 0 | 23 | | if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(containerName)) |
| | | 24 | | { |
| | 0 | 25 | | return (HealthStatus.Unhealthy, "Blob storage configuration missing"); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | try |
| | | 29 | | { |
| | 0 | 30 | | var blobServiceClient = new BlobServiceClient(connectionString); |
| | 0 | 31 | | var containerClient = blobServiceClient.GetBlobContainerClient(containerName); |
| | | 32 | | |
| | | 33 | | // Check if container exists and is accessible |
| | 0 | 34 | | var exists = await containerClient.ExistsAsync(); |
| | | 35 | | |
| | 0 | 36 | | if (!exists) |
| | | 37 | | { |
| | 0 | 38 | | return (HealthStatus.Unhealthy, "Container does not exist or is not accessible"); |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | return (HealthStatus.Healthy, "Blob storage accessible"); |
| | | 42 | | } |
| | 0 | 43 | | catch (Exception ex) |
| | | 44 | | { |
| | 0 | 45 | | return (HealthStatus.Unhealthy, $"Blob storage error: {ex.Message}"); |
| | | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | } |