using Microsoft.Extensions.Logging; using Dapr.Client; using MaksIT.Results; using MaksIT.Core.Extensions; namespace MaksIT.Dapr.Services; /// /// Dapr sidecar health and metadata with outcomes. /// public interface IDaprSidecarService { /// /// Checks sidecar health. /// Task> CheckHealthAsync(CancellationToken cancellationToken = default); /// /// Checks outbound health. /// Task> CheckOutboundHealthAsync(CancellationToken cancellationToken = default); /// /// Blocks until the sidecar is ready. /// Task WaitForSidecarAsync(CancellationToken cancellationToken = default); /// /// Gets sidecar metadata. /// Task> GetMetadataAsync(CancellationToken cancellationToken = default); /// /// Requests sidecar shutdown. /// Task ShutdownAsync(CancellationToken cancellationToken = default); } /// /// Default . /// public class DaprSidecarService( ILogger logger, DaprClient client ) : IDaprSidecarService { private const string ErrorMessage = "MaksIT.Dapr - Sidecar error"; /// public async Task> CheckHealthAsync(CancellationToken cancellationToken = default) { try { var healthy = await client.CheckHealthAsync(cancellationToken); return Result.Ok(healthy); } catch (OperationCanceledException) { throw; } catch (Exception ex) { logger.LogError(ex, ErrorMessage); return Result.InternalServerError(false, [ErrorMessage, .. ex.ExtractMessages()]); } } /// public async Task> CheckOutboundHealthAsync(CancellationToken cancellationToken = default) { try { var healthy = await client.CheckOutboundHealthAsync(cancellationToken); return Result.Ok(healthy); } catch (OperationCanceledException) { throw; } catch (Exception ex) { logger.LogError(ex, ErrorMessage); return Result.InternalServerError(false, [ErrorMessage, .. ex.ExtractMessages()]); } } /// public async Task WaitForSidecarAsync(CancellationToken cancellationToken = default) { try { await client.WaitForSidecarAsync(cancellationToken); return Result.Ok(); } catch (OperationCanceledException) { throw; } catch (Exception ex) { logger.LogError(ex, ErrorMessage); return Result.InternalServerError([ErrorMessage, .. ex.ExtractMessages()]); } } /// public async Task> GetMetadataAsync(CancellationToken cancellationToken = default) { try { var metadata = await client.GetMetadataAsync(cancellationToken); return Result.Ok(metadata); } catch (OperationCanceledException) { throw; } catch (Exception ex) { logger.LogError(ex, ErrorMessage); return Result.InternalServerError(default!, [ErrorMessage, .. ex.ExtractMessages()]); } } /// public async Task ShutdownAsync(CancellationToken cancellationToken = default) { try { await client.ShutdownSidecarAsync(cancellationToken); return Result.Ok(); } catch (OperationCanceledException) { throw; } catch (Exception ex) { logger.LogError(ex, ErrorMessage); return Result.InternalServerError([ErrorMessage, .. ex.ExtractMessages()]); } } }