Files
ServerManagementUtils/robin/Services/Publishing/ElasticPublishingService.cs
T
Alexandru-Victor Macocian e40e42ba40 Setup reading and persistence
2025-11-28 17:07:37 +01:00

36 lines
1.1 KiB
C#

using System.Extensions;
using System.Extensions.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Nest;
namespace Robin.Services.Publishing;
public sealed class ElasticPublishingService(
IElasticClient elasticClient,
IOptions<ElasticOptions> options,
ILogger<ElasticPublishingService> logger
) : IPublishingService
{
private readonly IElasticClient elasticClient = elasticClient;
private readonly ElasticOptions options = options.Value;
private readonly ILogger<ElasticPublishingService> logger = logger;
public async Task<bool> PublishAsync<T>(T item, CancellationToken cancellationToken)
where T : class
{
var response = await this.elasticClient.IndexDocumentAsync(item, cancellationToken);
if (response.IsValid)
{
return true;
}
var scopedLogger = this.logger.CreateScopedLogger();
scopedLogger.LogError(
"Failed to publish document to Elasticsearch. Error: {Error}",
response.DebugInformation
);
return false;
}
}