Retructured solution
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Abstractions;
|
||||
|
||||
public abstract class EventBusQueueBase
|
||||
{
|
||||
protected readonly Channel<IEvent> channel = Channel.CreateBounded<IEvent>(Constants.QueueBounds);
|
||||
|
||||
public ChannelWriter<IEvent> Outgoing => channel.Writer;
|
||||
|
||||
public ChannelReader<IEvent> Incoming => channel.Reader;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Abstractions;
|
||||
|
||||
public interface IEventBus
|
||||
{
|
||||
Task<Result> PublishAsync<TEvent>(TEvent notification, CancellationToken cancellationToken = default)
|
||||
where TEvent : class, IEvent;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Abstractions;
|
||||
|
||||
public interface IEventBusQueue
|
||||
{
|
||||
ChannelWriter<IEvent> Outgoing { get; }
|
||||
|
||||
ChannelReader<IEvent> Incoming { get; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace LiteCharms.Features.ServiceBus;
|
||||
|
||||
public static class Constants
|
||||
{
|
||||
public const int QueueBounds = 100000;
|
||||
|
||||
public const string EmailServiceBus = nameof(EmailServiceBus);
|
||||
public const string GeneralServiceBus = nameof(GeneralServiceBus);
|
||||
public const string SalesServiceBus = nameof(SalesServiceBus);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
using LiteCharms.Features.ServiceBus.Abstractions;
|
||||
using LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus;
|
||||
|
||||
public class EmailServiceBus(EmailQueue messages) : IEventBus
|
||||
{
|
||||
public async Task<Result> PublishAsync<TEvent>(TEvent notification, CancellationToken cancellationToken = default)
|
||||
where TEvent : class, IEvent
|
||||
{
|
||||
try
|
||||
{
|
||||
await messages.Outgoing.WriteAsync(notification, cancellationToken);
|
||||
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
using LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Exchanges;
|
||||
|
||||
public class EmailExchange(EmailQueue messages, ILogger<EmailExchange> logger, IPublisher mediator) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await foreach(IEvent? message in messages.Incoming.ReadAllAsync(stoppingToken))
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (message.Name)
|
||||
{
|
||||
case "SendShopEmailEnquiryEvent":
|
||||
await mediator.Publish(message, stoppingToken);
|
||||
break;
|
||||
case "ProcessEmailNotificationsEvent":
|
||||
await mediator.Publish(message, stoppingToken);
|
||||
break;
|
||||
default:
|
||||
logger.LogWarning("Unsupported email event {Event}", message.Name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Exchanges;
|
||||
|
||||
public class GeneralExchange(GeneralQueue messages) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
if (messages.Incoming.CanCount)
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Exchanges;
|
||||
|
||||
public class SalesExchange(SalesQueue messages) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
if (messages.Incoming.CanCount)
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
using LiteCharms.Features.ServiceBus.Abstractions;
|
||||
using LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus;
|
||||
|
||||
public class GeneralServiceBus(GeneralQueue messages) : IEventBus
|
||||
{
|
||||
public async Task<Result> PublishAsync<TEvent>(TEvent notification, CancellationToken cancellationToken = default)
|
||||
where TEvent : class, IEvent
|
||||
{
|
||||
try
|
||||
{
|
||||
await messages.Outgoing.WriteAsync(notification, cancellationToken);
|
||||
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using LiteCharms.Features.ServiceBus.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
public class EmailQueue : EventBusQueueBase, IEventBusQueue;
|
||||
@@ -0,0 +1,5 @@
|
||||
using LiteCharms.Features.ServiceBus.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
public class GeneralQueue : EventBusQueueBase, IEventBusQueue;
|
||||
@@ -0,0 +1,5 @@
|
||||
using LiteCharms.Features.ServiceBus.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
public class SalesQueue : EventBusQueueBase, IEventBusQueue;
|
||||
@@ -0,0 +1,23 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
using LiteCharms.Features.ServiceBus.Abstractions;
|
||||
using LiteCharms.Features.ServiceBus.Queues;
|
||||
|
||||
namespace LiteCharms.Features.ServiceBus;
|
||||
|
||||
public class SalesServiceBus(SalesQueue messages) : IEventBus
|
||||
{
|
||||
public async Task<Result> PublishAsync<TEvent>(TEvent notification, CancellationToken cancellationToken = default)
|
||||
where TEvent : class, IEvent
|
||||
{
|
||||
try
|
||||
{
|
||||
await messages.Outgoing.WriteAsync(notification, cancellationToken);
|
||||
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user