last works on Event Service ...
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xEventService.Configuration;
|
||||
using xEventService.Constants;
|
||||
using xEventService.Extensions;
|
||||
using xExceptions.Constants;
|
||||
|
||||
namespace xEventService.Providers
|
||||
{
|
||||
/// <summary>
|
||||
/// Base Event Consumer Service ...
|
||||
/// </summary>
|
||||
public abstract class XBaseEventConsumerBackgroundService : XResumableBackgroundService
|
||||
{
|
||||
/// <summary>
|
||||
/// Consumed Topic Messages ...
|
||||
/// </summary>
|
||||
protected readonly string topic;
|
||||
|
||||
/// <summary>
|
||||
/// Allowed Senders ...
|
||||
/// </summary>
|
||||
public readonly string[] allowedSenders;
|
||||
|
||||
/// <summary>
|
||||
/// Allowed Actions ...
|
||||
/// </summary>
|
||||
public readonly string[] allowedActions;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration ...
|
||||
/// </summary>
|
||||
protected readonly XEventServiceConfiguration configuration;
|
||||
|
||||
protected XBaseEventConsumerBackgroundService(
|
||||
string[] allowedActions,
|
||||
string[] allowedSenders,
|
||||
IServiceProvider serviceProvider,
|
||||
XEventServiceConfiguration configuration,
|
||||
ILogger<XResumableBackgroundService> logger,
|
||||
string topic = XEventServiceConstants.XDefaultTopic
|
||||
) : base(
|
||||
logger: logger,
|
||||
serviceProvider: serviceProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Actions ...
|
||||
if (!allowedActions
|
||||
.IsAllowedCollection() ||
|
||||
!allowedSenders
|
||||
.IsAllowedCollection() ||
|
||||
!configuration.IsValid()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
this.topic = topic;
|
||||
this.configuration = configuration;
|
||||
this.allowedActions = allowedActions;
|
||||
this.allowedSenders = allowedSenders;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 ...
|
||||
|
||||
@@ -115,7 +115,7 @@ namespace xEventService.Providers
|
||||
);
|
||||
|
||||
//
|
||||
result = true;
|
||||
result = !response.IsNullOrDefault();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using xCommons.Extensions;
|
||||
using xEventService.Configuration;
|
||||
using xEventService.Constants;
|
||||
using xEventService.Extensions;
|
||||
using xEventService.Models;
|
||||
using xExceptions.Constants;
|
||||
|
||||
namespace xEventService.Providers
|
||||
@@ -15,36 +17,233 @@ namespace xEventService.Providers
|
||||
/// <summary>
|
||||
/// an Abstract Kafka Consumer Service ...
|
||||
/// </summary>
|
||||
public class XRabbitMQConsumerServiceBase : BackgroundService
|
||||
public abstract class XRabbitMQConsumerServiceBase : XBaseEventConsumerBackgroundService
|
||||
{
|
||||
protected readonly IModel channel;
|
||||
protected readonly IConnection connection;
|
||||
protected readonly XEventServiceConfiguration configuration;
|
||||
protected readonly ILogger<XRabbitMQConsumerServiceBase> logger;
|
||||
protected IModel channel;
|
||||
protected IConnection connection;
|
||||
protected AsyncEventingBasicConsumer consumer;
|
||||
|
||||
public XRabbitMQConsumerServiceBase(
|
||||
protected XRabbitMQConsumerServiceBase(
|
||||
string[] allowedActions,
|
||||
string[] allowedSenders,
|
||||
IServiceProvider serviceProvider,
|
||||
XEventServiceConfiguration configuration,
|
||||
ILogger<XRabbitMQConsumerServiceBase> logger
|
||||
ILogger<XRabbitMQConsumerServiceBase> logger,
|
||||
string topic = XEventServiceConstants.XDefaultTopic
|
||||
) : base(
|
||||
topic: topic,
|
||||
logger: logger,
|
||||
configuration: configuration,
|
||||
allowedActions: allowedActions,
|
||||
allowedSenders: allowedSenders,
|
||||
serviceProvider: serviceProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
this.logger = logger;
|
||||
this.configuration = configuration;
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
if (!configuration.IsValid() ||
|
||||
!configuration.RabbitMQ.IsValid() ||
|
||||
configuration.Broker != Constants.XEventBroker.XRabbitMQ
|
||||
// Validating Configuration ...
|
||||
if (!configuration.RabbitMQ.IsValid() ||
|
||||
configuration.Broker != XEventBroker.XRabbitMQ
|
||||
)
|
||||
{
|
||||
XException.InvalidConfiguration.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
SetupConnection();
|
||||
DeclareTopology();
|
||||
consumer = new AsyncEventingBasicConsumer(channel);
|
||||
}
|
||||
|
||||
public abstract Task HandleMessageAsync(
|
||||
XEventMessage message,
|
||||
|
||||
/// <summary>
|
||||
/// Default Action Execution for Background Services ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
protected override async Task ExecuteAsync(
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
)
|
||||
{
|
||||
//
|
||||
// For Fix Blocking Synchronous ...
|
||||
await Task.Yield();
|
||||
using var cts = new CancellationTokenSource();
|
||||
|
||||
//
|
||||
// Subscribe to Topic ...
|
||||
consumer.Received += async (model, ea) =>
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var body = ea.Body.ToArray();
|
||||
var json = Encoding.UTF8.GetString(body);
|
||||
var message = json.FromJSON<XEventMessage>();
|
||||
if (!message.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
await DoWorkAsync(
|
||||
payload: message,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
if (configuration.RabbitMQ.ManualAck)
|
||||
{
|
||||
//
|
||||
channel.BasicAck(
|
||||
multiple: false,
|
||||
deliveryTag: ea.DeliveryTag
|
||||
);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
//
|
||||
if (configuration.RabbitMQ.ManualAck)
|
||||
{
|
||||
//
|
||||
channel.BasicNack(
|
||||
requeue: true,
|
||||
multiple: false,
|
||||
deliveryTag: ea.DeliveryTag
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
consumer.Shutdown += (model, ea) =>
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
//
|
||||
channel.BasicQos(
|
||||
prefetchSize: 0,
|
||||
global: false,
|
||||
prefetchCount: configuration.RabbitMQ.PrefetchCount
|
||||
);
|
||||
|
||||
//
|
||||
channel.BasicConsume(
|
||||
queue: GetQueueName(),
|
||||
consumer: consumer,
|
||||
autoAck: !configuration.RabbitMQ.ManualAck
|
||||
);
|
||||
|
||||
//
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(1000, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overriding Stop Action ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
//
|
||||
channel.Close();
|
||||
channel.Dispose();
|
||||
connection.Close();
|
||||
connection.Dispose();
|
||||
|
||||
//
|
||||
await base.StopAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose Implementation ...
|
||||
/// </summary>
|
||||
public override void Dispose()
|
||||
{
|
||||
//
|
||||
channel.Dispose();
|
||||
connection.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
private string GetQueueName()
|
||||
{
|
||||
//
|
||||
var result = string.Empty;
|
||||
if (!configuration.RabbitMQ.Queue.IsNullOrEmpty())
|
||||
{
|
||||
result = configuration.RabbitMQ.Queue;
|
||||
}
|
||||
|
||||
//
|
||||
if (result.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
var digits = Guid.NewGuid().ToString().GetDigits();
|
||||
result = $"{configuration.RabbitMQ.Exchange}[{digits}]";
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
private void SetupConnection()
|
||||
{
|
||||
var factory = new ConnectionFactory
|
||||
{
|
||||
DispatchConsumersAsync = true,
|
||||
Port = configuration.GetRabbitMQPort(),
|
||||
HostName = configuration.GetRabbitMQHost(),
|
||||
UserName = configuration.RabbitMQ.Username,
|
||||
Password = configuration.RabbitMQ.Password,
|
||||
NetworkRecoveryInterval = TimeSpan.FromSeconds(
|
||||
configuration.RabbitMQ.NetworkRecoveryIntervalSeconds
|
||||
),
|
||||
VirtualHost = configuration.RabbitMQ.VirtualHost,
|
||||
AutomaticRecoveryEnabled = configuration.RabbitMQ.AutomaticRecoveryEnabled,
|
||||
};
|
||||
|
||||
//
|
||||
connection = factory.CreateConnection();
|
||||
channel = connection.CreateModel();
|
||||
}
|
||||
|
||||
private void DeclareTopology()
|
||||
{
|
||||
//
|
||||
channel.ExchangeDeclare(
|
||||
arguments: null,
|
||||
exchange: configuration.RabbitMQ.Exchange,
|
||||
durable: configuration.RabbitMQ.ExchangeDurable,
|
||||
autoDelete: configuration.RabbitMQ.ExchangeAutoDelete,
|
||||
type: configuration.RabbitMQ.ExchangeType.GetStringValue()
|
||||
);
|
||||
|
||||
//
|
||||
var queueName = GetQueueName();
|
||||
|
||||
//
|
||||
channel.QueueDeclare(
|
||||
arguments: null,
|
||||
queue: queueName,
|
||||
durable: configuration.RabbitMQ.QueueDurable,
|
||||
exclusive: configuration.RabbitMQ.QueueExclusive,
|
||||
autoDelete: configuration.RabbitMQ.QueueAutoDelete
|
||||
);
|
||||
|
||||
//
|
||||
channel.QueueBind(
|
||||
arguments: null,
|
||||
queue: queueName,
|
||||
exchange: configuration.RabbitMQ.Exchange,
|
||||
routingKey: configuration.RabbitMQ.RoutingKey
|
||||
);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -231,7 +231,7 @@ namespace xEventService.Providers
|
||||
/// <summary>
|
||||
/// Ensure Connection Established ...
|
||||
/// </summary>
|
||||
public async Task<bool> EnsureConnectionAsync()
|
||||
private async Task<bool> EnsureConnectionAsync()
|
||||
{
|
||||
//
|
||||
try
|
||||
@@ -289,9 +289,6 @@ namespace xEventService.Providers
|
||||
type: configuration.RabbitMQ.ExchangeType.GetStringValue()
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user