Skip to content

Commit

Permalink
Refactor the TinyEventBus
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Sep 11, 2024
1 parent 12286f0 commit 05c468b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 56 deletions.
1 change: 0 additions & 1 deletion src/AgileConfig.Server.Common/EventBus/ITinyEventBus.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using System;

namespace AgileConfig.Server.Common.EventBus
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static IServiceCollection AddTinyEventBus(this IServiceCollection sc)
{
sc.AddSingleton<ITinyEventBus, TinyEventBus>(sp =>
new TinyEventBus(sc));

return sc;
}
}
Expand Down
49 changes: 26 additions & 23 deletions src/AgileConfig.Server.Common/EventBus/TinyEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,68 @@ namespace AgileConfig.Server.Common.EventBus
public class TinyEventBus : ITinyEventBus
{
private readonly IServiceCollection _serviceCollection;
private readonly static ConcurrentDictionary<Type, List<Type>> _eventHandlerMap = new ConcurrentDictionary<Type, List<Type>>();
private static readonly ConcurrentDictionary<Type, List<Type>> EventHandlerMap = new ();
private IServiceProvider _localServiceProvider;
private ILogger _logger;

public TinyEventBus(IServiceCollection serviceCollection)
{
this._serviceCollection = serviceCollection;
_serviceCollection = serviceCollection;
_logger = _serviceCollection.BuildServiceProvider().GetService<ILoggerFactory>().CreateLogger<TinyEventBus>();
}
public void Register<T>() where T : class, IEventHandler
{
var handlerType = typeof(T);
var eventType = handlerType.GetInterfaces().FirstOrDefault(x => x.IsGenericType).GenericTypeArguments.FirstOrDefault();
if (_eventHandlerMap.TryGetValue(eventType, out List<Type> handlerTypes))
var eventType = handlerType.GetInterfaces().FirstOrDefault(x => x.IsGenericType)!.GenericTypeArguments.FirstOrDefault();
if (EventHandlerMap.TryGetValue(eventType, out List<Type> handlerTypes))
{
handlerTypes.Add(handlerType);
}
else
{
_eventHandlerMap.TryAdd(eventType, new List<Type> {
EventHandlerMap.TryAdd(eventType, new List<Type> {
handlerType
});
}
_serviceCollection.AddScoped<T>();

}

/// <summary>
/// Trigger an event. This method must be called before the handler is registered.
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <param name="evt"></param>
public void Fire<TEvent>(TEvent evt) where TEvent : IEvent
{
var sp = _serviceCollection.BuildServiceProvider();
using var scope = sp.CreateScope();
var logger = scope.ServiceProvider.GetService<ILoggerFactory>().CreateLogger<TinyEventBus>();
_localServiceProvider ??= _serviceCollection.BuildServiceProvider();

logger.LogInformation($"Event fired: {typeof(TEvent).Name}");
_logger.LogInformation($"Event fired: {typeof(TEvent).Name}");

var eventType = typeof(TEvent);
if (_eventHandlerMap.TryGetValue(eventType, out List<Type> handlers))
if (EventHandlerMap.TryGetValue(eventType, out List<Type> handlers))
{
if (handlers.Count == 0)
{
logger.LogInformation($"Event fired: {typeof(TEvent).Name}, but no handlers.");
_logger.LogInformation($"Event fired: {typeof(TEvent).Name}, but no handlers.");
return;
}

foreach (var handlerType in handlers)
{
_ = Task.Run(async () =>
{
using var sc = sp.CreateScope();
using var sc = _localServiceProvider.CreateScope();
var handler = sc.ServiceProvider.GetService(handlerType);
if (handler != null)

try
{
var handlerInstance = handler as IEventHandler;
try
{
await handlerInstance.Handle(evt);
}
catch (Exception ex)
{
sc.ServiceProvider.GetService<ILoggerFactory>().CreateLogger<TinyEventBus>()
.LogError(ex, "try run {handler} occur error.", handlerType);
}
await (handler as IEventHandler)?.Handle(evt)!;
}
catch (Exception ex)
{
_logger
.LogError(ex, "try run {handler} occur error.", handlerType);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,32 @@

namespace AgileConfig.Server.Service.EventRegisterService;

public class SystemEventHandlersRegister : IEventHandlerRegister
public class SystemEventHandlersRegister(ITinyEventBus tinyEventBus) : IEventHandlerRegister
{
private readonly ITinyEventBus _tinyEventBus;

public SystemEventHandlersRegister(ITinyEventBus tinyEventBus)
{
_tinyEventBus = tinyEventBus;
}

public void Register()
{
_tinyEventBus.Register<LoginEventHandler>();
_tinyEventBus.Register<InitSaPasswordEventHandler>();
_tinyEventBus.Register<ResetUserPasswordEventHandler>();
_tinyEventBus.Register<ChangeUserPasswordEventHandler>();
_tinyEventBus.Register<AddAppEventHandler>();
_tinyEventBus.Register<EditAppEventHandler>();
_tinyEventBus.Register<DisableOrEnableAppEventHandler>();
_tinyEventBus.Register<DeleteAppEventHandler>();
_tinyEventBus.Register<AddConfigEventHandler>();
_tinyEventBus.Register<EditConfigEventHandler>();
_tinyEventBus.Register<DeleteConfigEventHandler>();
_tinyEventBus.Register<DeleteSomeConfigEventHandler>();
_tinyEventBus.Register<PublishConfigEventHandler>();
_tinyEventBus.Register<RollbackConfigEventHandler>();
_tinyEventBus.Register<DisContectClientEventHandler>();
_tinyEventBus.Register<RegisterAServiceEventHandler>();
_tinyEventBus.Register<UnRegisterAServiceEventHandler>();
_tinyEventBus.Register<AddNodeEventHandler>();
_tinyEventBus.Register<DeleteNodeEventHandler>();
_tinyEventBus.Register<AddUserEventHandler>();
_tinyEventBus.Register<EditUserEventHandler>();
_tinyEventBus.Register<DeleteUserEventHandler>();
tinyEventBus.Register<LoginEventHandler>();
tinyEventBus.Register<InitSaPasswordEventHandler>();
tinyEventBus.Register<ResetUserPasswordEventHandler>();
tinyEventBus.Register<ChangeUserPasswordEventHandler>();
tinyEventBus.Register<AddAppEventHandler>();
tinyEventBus.Register<EditAppEventHandler>();
tinyEventBus.Register<DisableOrEnableAppEventHandler>();
tinyEventBus.Register<DeleteAppEventHandler>();
tinyEventBus.Register<AddConfigEventHandler>();
tinyEventBus.Register<EditConfigEventHandler>();
tinyEventBus.Register<DeleteConfigEventHandler>();
tinyEventBus.Register<DeleteSomeConfigEventHandler>();
tinyEventBus.Register<PublishConfigEventHandler>();
tinyEventBus.Register<RollbackConfigEventHandler>();
tinyEventBus.Register<DisContectClientEventHandler>();
tinyEventBus.Register<RegisterAServiceEventHandler>();
tinyEventBus.Register<UnRegisterAServiceEventHandler>();
tinyEventBus.Register<AddNodeEventHandler>();
tinyEventBus.Register<DeleteNodeEventHandler>();
tinyEventBus.Register<AddUserEventHandler>();
tinyEventBus.Register<EditUserEventHandler>();
tinyEventBus.Register<DeleteUserEventHandler>();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using System.Threading.Tasks;
using System;
using Testcontainers.MySql;
using System.Data;
using AgileConfig.Server.Data.Entity;
using Microsoft.Extensions.Configuration;

namespace AgileConfig.Server.ServiceTests.mysql
{
Expand Down

0 comments on commit 05c468b

Please sign in to comment.