mirror of
https://github.com/AlexMacocian/ServerManagementUtils.git
synced 2026-07-15 15:19:58 +00:00
36 lines
1.1 KiB
C#
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;
|
|
}
|
|
}
|