last works on Event Service ...

This commit is contained in:
2026-08-19 12:34:14 +03:30
parent b20c2f8db7
commit 1e0641643d
17 changed files with 557 additions and 245 deletions
+28 -155
View File
@@ -2,12 +2,10 @@ using System;
using System.Threading;
using System.Threading.Tasks;
using Confluent.Kafka;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Constants;
using xEventService.Extensions;
using xExceptions.Constants;
namespace xEventService.Providers
@@ -15,67 +13,35 @@ namespace xEventService.Providers
/// <summary>
/// an Abstract Kafka Consumer Service ...
/// </summary>
public abstract class XKafkaConsumerServiceBase : BackgroundService
public abstract class XKafkaConsumerServiceBase : XBaseEventConsumerBackgroundService
{
/// <summary>
/// Consumed Topic Messages ...
/// </summary>
protected readonly string topic;
/// <summary>
/// Check is Paused ...
/// </summary>
private bool paused = false;
/// <summary>
/// Allowed Senders ...
/// </summary>
public readonly string[] allowedSenders;
/// <summary>
/// Allowed Actions ...
/// </summary>
public readonly string[] allowedActions;
/// <summary>
/// Kafka Consumer Class ...
/// </summary>
protected readonly IConsumer<Ignore, string> consumer;
protected readonly XEventServiceConfiguration configuration;
protected readonly ILogger<XKafkaConsumerServiceBase> logger;
protected XKafkaConsumerServiceBase(
string[] allowedActions,
string[] allowedSenders,
IServiceProvider serviceProvider,
XEventServiceConfiguration configuration,
ILogger<XKafkaConsumerServiceBase> logger,
string topic = XEventServiceConstants.XDefaultTopic
) : base(
topic: topic,
logger: logger,
configuration: configuration,
allowedActions: allowedActions,
allowedSenders: allowedSenders,
serviceProvider: serviceProvider
)
{
//
this.topic = topic;
this.logger = logger;
this.configuration = configuration;
//
// Validate Actions ...
if (!allowedActions
.IsAllowedCollection())
// Validating Configuration ...
if (configuration.Broker != XEventBroker.XKafka)
{
XException.InvalidArgs.Throw();
XException.InvalidConfiguration.Throw();
}
this.allowedActions = [.. allowedActions];
//
// Validate Senders ...
if (!allowedSenders
.IsAllowedCollection())
{
XException.InvalidArgs.Throw();
}
this.allowedSenders = [.. allowedSenders];
//
// Prepare Kafka Consumer Configuration ...
@@ -89,45 +55,6 @@ namespace xEventService.Providers
consumer = new ConsumerBuilder<Ignore, string>(config).Build();
}
/// <summary>
/// Pause Consuming ...
/// </summary>
/// <returns></returns>
public bool Pause()
{
//
var result = !paused;
if (!result)
{
return result;
}
//
paused = true;
result = paused;
return result;
}
/// <summary>
/// Resume Consuming ...
/// </summary>
/// <returns></returns>
public bool Resume()
{
//
var result = paused;
if (!result)
{
return result;
}
//
paused = false;
result = !paused;
return result;
}
/// <summary>
/// Default Action Execution for Background Services ...
/// </summary>
@@ -142,94 +69,40 @@ namespace xEventService.Providers
await Task.Yield();
using var cts = new CancellationTokenSource();
//
// Subscribe to Topic ...
consumer.Subscribe(topic);
//
// Consuming ...
while (!cancellationToken.IsCancellationRequested)
{
try
{
//
// Consuming Topic ...
await consumer.Consume(async (consumeResult, cancellationToken) =>
var consumeResult = consumer.Consume(cancellationToken);
if (!consumeResult.IsNullOrDefault() &&
!consumeResult.Message.IsNullOrDefault()
)
{
// Check Pausing ...
if (!paused)
{
// Logging Message Recieved ...
if (enableLogging)
{
// Encrypt Message ...
var expirationOffset = DateTimeOffset.UtcNow
.AddMinutes(encryptionLifetimeInMinute);
var encryptedMessage = cryptographyService.Encrypt(
text: consumeResult?.Message.Value ?? string.Empty,
expirationOffset: expirationOffset
);
logger
.LogInformation(
"Consume Recieved Message from Kafka Topic: {Topic}, Offset: {Offset}, Partition: {Partition}, Message: {Message}",
topic,
consumeResult?.Offset,
consumeResult?.Partition,
encryptedMessage
);
}
// Produce Message Model ...
var model = consumeResult
.FillConsumeResult()
?? throw RpkKafkaExceptionHelper
.GetInvalidKafkaConsumeResultException();
// Check Sender and Actions Owning ...
var isOwned = model.IsOwned(
allowedActions,
allowedSenders
);
if (isOwned)
{
// Notify Extended Classes for Consume Message ...
await ConsumeAsync(
model,
cancellationToken
);
}
// Commit Offset after Consuming ...
consumer?.Commit(consumeResult);
}
},
cts.Token);
}
catch (ConsumeException ex)
{
// Log Exception ...
if (enableLogging)
{
logger.LogError(
ex,
"Kafka Message Consume Failed: {Error}",
ex.Error.Reason
//
await DoWorkAsync(
payload: consumeResult.Message,
cancellationToken: cancellationToken
);
}
}
catch (ConsumeException)
{
await Task.Delay(1000, cancellationToken);
}
catch (Exception ex)
catch (Exception)
{
// Log Exception ...
if (enableLogging)
{
logger.LogError(
ex,
"Exception: {Exc}",
ex.Message
);
}
await Task.Delay(1000, cancellationToken);
}
}
}
}
/// <summary>
/// Dispose Implementation ...