Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix issues-570 #624

Merged
merged 6 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Eve
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Events.Tests.Scenes.CheckMethodsType", "src\Contrib\Dispatcher\Tests\Scenes\CheckMethodsType\Masa.Contrib.Dispatcher.Events.Tests.Scenes.CheckMethodsType.csproj", "{2355FDB2-ACC3-44F3-9D2E-DCFFCC82DB45}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Events.Tests.Scenes.HandlerOrder", "src\Contrib\Dispatcher\Tests\Scenes\HandlerOrder\Masa.Contrib.Dispatcher.Events.Tests.Scenes.HandlerOrder.csproj", "{5CD85A3F-428B-44B8-830C-05A97BA7610B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Events.Tests.Scenes.OnlyCancelHandler", "src\Contrib\Dispatcher\Tests\Scenes\OnlyCancelHandler\Masa.Contrib.Dispatcher.Events.Tests.Scenes.OnlyCancelHandler.csproj", "{A6DEC75B-B7FB-43C8-815E-F4F4689326C3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Events.Tests.Scenes.OrderEqualBySaga", "src\Contrib\Dispatcher\Tests\Scenes\OrderEqualBySaga\Masa.Contrib.Dispatcher.Events.Tests.Scenes.OrderEqualBySaga.csproj", "{F1FD125A-9795-4533-86CA-37274EAAD540}"
Expand Down Expand Up @@ -691,8 +689,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{3C12BE69
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.StackSdks.Alert.Tests", "src\Contrib\StackSdks\Tests\Masa.Contrib.StackSdks.Alert.Tests\Masa.Contrib.StackSdks.Alert.Tests.csproj", "{1221F32F-7310-49FF-94DD-2BCF570E03F2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Dispatcher.Events.Tests.Scenes.IntegrationEvent", "src\Contrib\Dispatcher\Tests\Scenes\Masa.Contrib.Dispatcher.Events.Tests.Scenes.IntegrationEvent\Masa.Contrib.Dispatcher.Events.Tests.Scenes.IntegrationEvent.csproj", "{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.UoW.EFCore.Tests", "src\Contrib\Data\UoW\Tests\Masa.Contrib.Data.UoW.EFCore.Tests\Masa.Contrib.Data.UoW.EFCore.Tests.csproj", "{9208B446-A7E0-4B5A-A42D-37E63B76E875}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scenes", "Scenes", "{02591EE4-1033-4575-984F-7AB290E7C6FA}"
Expand Down Expand Up @@ -1779,14 +1775,6 @@ Global
{2355FDB2-ACC3-44F3-9D2E-DCFFCC82DB45}.Release|Any CPU.Build.0 = Release|Any CPU
{2355FDB2-ACC3-44F3-9D2E-DCFFCC82DB45}.Release|x64.ActiveCfg = Release|Any CPU
{2355FDB2-ACC3-44F3-9D2E-DCFFCC82DB45}.Release|x64.Build.0 = Release|Any CPU
{5CD85A3F-428B-44B8-830C-05A97BA7610B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5CD85A3F-428B-44B8-830C-05A97BA7610B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5CD85A3F-428B-44B8-830C-05A97BA7610B}.Debug|x64.ActiveCfg = Debug|Any CPU
{5CD85A3F-428B-44B8-830C-05A97BA7610B}.Debug|x64.Build.0 = Debug|Any CPU
{5CD85A3F-428B-44B8-830C-05A97BA7610B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5CD85A3F-428B-44B8-830C-05A97BA7610B}.Release|Any CPU.Build.0 = Release|Any CPU
{5CD85A3F-428B-44B8-830C-05A97BA7610B}.Release|x64.ActiveCfg = Release|Any CPU
{5CD85A3F-428B-44B8-830C-05A97BA7610B}.Release|x64.Build.0 = Release|Any CPU
{A6DEC75B-B7FB-43C8-815E-F4F4689326C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6DEC75B-B7FB-43C8-815E-F4F4689326C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6DEC75B-B7FB-43C8-815E-F4F4689326C3}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -2507,14 +2495,6 @@ Global
{1221F32F-7310-49FF-94DD-2BCF570E03F2}.Release|Any CPU.Build.0 = Release|Any CPU
{1221F32F-7310-49FF-94DD-2BCF570E03F2}.Release|x64.ActiveCfg = Release|Any CPU
{1221F32F-7310-49FF-94DD-2BCF570E03F2}.Release|x64.Build.0 = Release|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Debug|x64.ActiveCfg = Debug|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Debug|x64.Build.0 = Debug|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Release|Any CPU.Build.0 = Release|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Release|x64.ActiveCfg = Release|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Release|x64.Build.0 = Release|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -2742,7 +2722,6 @@ Global
{44B0463B-8F56-49A6-8373-0141B66461BE} = {08B138B5-2599-4F42-9584-6AE736673882}
{41E12E1D-CC99-48D1-B7D2-928BE23EC3DD} = {08B138B5-2599-4F42-9584-6AE736673882}
{2355FDB2-ACC3-44F3-9D2E-DCFFCC82DB45} = {08B138B5-2599-4F42-9584-6AE736673882}
{5CD85A3F-428B-44B8-830C-05A97BA7610B} = {08B138B5-2599-4F42-9584-6AE736673882}
{A6DEC75B-B7FB-43C8-815E-F4F4689326C3} = {08B138B5-2599-4F42-9584-6AE736673882}
{F1FD125A-9795-4533-86CA-37274EAAD540} = {08B138B5-2599-4F42-9584-6AE736673882}
{6616A9E2-66F3-4AEB-BE0D-77346B3F3B06} = {48853497-9C2D-4ADA-AED3-61280AF1418D}
Expand Down Expand Up @@ -2865,7 +2844,6 @@ Global
{EEACDE24-9D4A-4F65-B13A-644E89A7918D} = {8A9DBB76-6618-4982-87D7-6CBD8375EB15}
{1221F32F-7310-49FF-94DD-2BCF570E03F2} = {EC7A08E9-3355-486B-BA30-41A1F8CAC5F5}
{9208B446-A7E0-4B5A-A42D-37E63B76E875} = {3C12BE69-247B-4448-9700-0ACEE8440A34}
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71} = {08B138B5-2599-4F42-9584-6AE736673882}
{02591EE4-1033-4575-984F-7AB290E7C6FA} = {48853497-9C2D-4ADA-AED3-61280AF1418D}
{A5B30EEA-2DE1-4233-8584-936CB862191B} = {02591EE4-1033-4575-984F-7AB290E7C6FA}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
global using Masa.BuildingBlocks.Configuration;
global using Masa.BuildingBlocks.Configuration.Options;
global using Masa.BuildingBlocks.Data;
global using Masa.BuildingBlocks.Development.DaprStarter;
global using Masa.BuildingBlocks.Ddd.Domain.Events;
global using Masa.BuildingBlocks.Development.DaprStarter;
global using Masa.BuildingBlocks.Dispatcher.IntegrationEvents;
global using Masa.BuildingBlocks.Dispatcher.IntegrationEvents.Logs;
global using Masa.BuildingBlocks.Isolation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
global using Masa.BuildingBlocks.Configuration.Options;
global using Masa.BuildingBlocks.Data;
global using Masa.BuildingBlocks.Data.UoW;
global using Masa.BuildingBlocks.Development.DaprStarter;
global using Masa.BuildingBlocks.Ddd.Domain.Events;
global using Masa.BuildingBlocks.Development.DaprStarter;
global using Masa.BuildingBlocks.Dispatcher.Events;
global using Masa.BuildingBlocks.Dispatcher.IntegrationEvents;
global using Masa.BuildingBlocks.Dispatcher.IntegrationEvents.Logs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Masa.Contrib.Dispatcher.Events;

[ExcludeFromCodeCoverage]
public class DefaultExceptionStrategyProvider : IExceptionStrategyProvider
{
private readonly ILogger<DefaultExceptionStrategyProvider>? _logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.Contrib.Dispatcher.Events;

public enum ExecuteStatus
{
/// <summary>
/// Wait for the event handler to execute
/// </summary>
WaitingExecute,

/// <summary>
/// in progress
/// </summary>
InProgress,

/// <summary>
/// Event execution succeeded
/// </summary>
Succeed,

/// <summary>
/// Event execution failed, But Event Rollback Succeeded
/// </summary>
RollbackSucceeded,

/// <summary>
/// The event execution failed, and there is no compensation handler or an error occurred before the Handler was executed
/// </summary>
Failed,

/// <summary>
/// Event execution failed and Rollback failed
/// </summary>
RollbackFailed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Dispatcher.Events;

public class EventExecuteInfo
{
public ExecuteStatus Status { get; set; }

public Exception? Exception { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static IServiceCollection AddEventBus(

services.AddSingleton<EventBusProvider>();

services.TryAddEnumerable(new ServiceDescriptor(typeof(IEventMiddleware<>), typeof(ExceptionEventMiddleware<>),
ServiceLifetime.Transient));
services.TryAddEnumerable(new ServiceDescriptor(typeof(IEventMiddleware<>), typeof(TransactionEventMiddleware<>),
ServiceLifetime.Transient));

Expand All @@ -38,49 +40,32 @@ public static IServiceCollection AddEventBus(
MasaArgumentException.ThrowIfNullOrEmptyCollection(assemblies);

var assemblyArray = assemblies.Distinct().ToArray();
var dispatcherOptions = new DispatcherOptions(services, assemblyArray);
services.AddSingleton(typeof(IOptions<DispatcherOptions>),
_ => Microsoft.Extensions.Options.Options.Create(dispatcherOptions));
services.AddSingleton(new SagaDispatcher(services, assemblyArray).Build(lifetime));
services.AddSingleton(new Dispatcher(services, assemblyArray).Build(lifetime));
services.TryAddSingleton<IExceptionStrategyProvider, DefaultExceptionStrategyProvider>();
services.TryAdd(typeof(IExecutionStrategy), typeof(ExecutionStrategy), ServiceLifetime.Singleton);
services.TryAddScoped<IInitializeServiceProvider, InitializeServiceProvider>();

services.AddScoped<ILocalEventBus, LocalEventBus>();
services.AddScoped<IEventBus>(serviceProvider => new EventBus(
serviceProvider.GetRequiredService<ILocalEventBus>(),
new Lazy<IIntegrationEventBus?>(serviceProvider.GetService<IIntegrationEventBus>())));
MasaApp.TrySetServiceCollection(services);
return services;
}
var dispatchNetworkBuilder = new DispatchRelationNetworkBuilder();
var loggerFactory = services.BuildServiceProvider().GetService<ILoggerFactory>();
dispatchNetworkBuilder.Add(new DefaultDispatchNetworkProvider(assemblyArray, loggerFactory));
dispatchNetworkBuilder.Add(new DefaultSagaDispatchNetworkProvider(assemblyArray, loggerFactory));
var dispatchNetworkRoot = dispatchNetworkBuilder.Build();
services.AddSingleton<IDispatchNetworkRoot>(_ => dispatchNetworkRoot);

public static IServiceCollection AddTestEventBus(
this IServiceCollection services,
IEnumerable<Assembly> assemblies,
ServiceLifetime lifetime,
Action<EventBusBuilder>? eventBusBuilder = null)
{
if (services.Any(service => service.ImplementationType == typeof(EventBusProvider)))
return services;
var serviceTypes =
DispatchNetworkUtils.GetServiceTypes(dispatchNetworkRoot.DispatchNetworks.SelectMany(item => item.Value).ToList());
foreach (var serviceType in serviceTypes)
{
services.TryAdd(new ServiceDescriptor(serviceType, serviceType, lifetime));
}

services.AddSingleton<EventBusProvider>();
services.TryAddSingleton<IExceptionStrategyProvider, DefaultExceptionStrategyProvider>();
services.TryAdd(typeof(IExecutionStrategy), typeof(ExecutionStrategy), ServiceLifetime.Singleton);
services.TryAddScoped<IExecuteProvider, DefaultExecuteProvider>();
services.TryAdd(new ServiceDescriptor(typeof(ILocalEventBus), typeof(LocalEventBus), lifetime));

eventBusBuilder?.Invoke(new EventBusBuilder(services));
var dispatcherOptions = new DispatcherOptions(services, assemblyArray);
services.TryAdd(new ServiceDescriptor(typeof(ILocalEventBusWrapper), serviceProvider => new LocalEventBusWrapper(serviceProvider, dispatcherOptions), lifetime));

MasaArgumentException.ThrowIfNullOrEmptyCollection(assemblies);
services.TryAdd(new ServiceDescriptor(typeof(IEventBus), typeof(EventBus), lifetime));

var assemblyArray = assemblies.Distinct().ToArray();
var dispatcherOptions = new DispatcherOptions(services, assemblyArray);
services.AddSingleton(typeof(IOptions<DispatcherOptions>),
_ => Microsoft.Extensions.Options.Options.Create(dispatcherOptions));
services.AddSingleton(new SagaDispatcher(services, assemblyArray, true).Build(lifetime));
services.AddSingleton(new Dispatcher(services, assemblyArray).Build(lifetime));
services.TryAddSingleton<IExceptionStrategyProvider, DefaultExceptionStrategyProvider>();
services.TryAdd(typeof(IExecutionStrategy), typeof(ExecutionStrategy), ServiceLifetime.Singleton);
services.TryAddScoped<IInitializeServiceProvider, InitializeServiceProvider>();
services.AddTransient(typeof(IEventMiddleware<>), typeof(TransactionEventMiddleware<>));
services.AddScoped(typeof(IEventBus), typeof(LocalEventBus));
MasaApp.TrySetServiceCollection(services);

return services;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Dispatcher.Events.Internal.Dispatch;

internal class DefaultDispatchNetworkProvider : IDispatchNetworkProvider
{
private readonly Assembly[] _assemblies;
private readonly List<EventHandlerAttribute> _handlerNetwork;
private readonly List<EventHandlerAttribute> _cancelHandlerNetwork;

public IReadOnlyList<EventHandlerAttribute> HandlerNetwork => _handlerNetwork;

public IReadOnlyList<EventHandlerAttribute> CancelHandlerNetwork => _cancelHandlerNetwork;

private readonly ILogger<DefaultDispatchNetworkProvider>? _logger;

public DefaultDispatchNetworkProvider(Assembly[] assemblies, ILoggerFactory? loggerFactory)
{
_assemblies = assemblies;
_logger = loggerFactory?.CreateLogger<DefaultDispatchNetworkProvider>();
_handlerNetwork = new List<EventHandlerAttribute>();
_cancelHandlerNetwork = new List<EventHandlerAttribute>();
BuildDispatchNetwork();
}

private void BuildDispatchNetwork()
{
foreach (var assembly in _assemblies)
{
BuildDispatchNetwork(assembly);
}
}

private void BuildDispatchNetwork(Assembly assembly)
{
foreach (var type in assembly.GetTypes())
{
if (!type.IsConcrete())
continue; //Handler and Cancel must be normal classes, not abstract classes or interfaces

foreach (var method in type.GetMethods())
{
var eventHandlerAttribute = TryAddRelationNetwork(type, method);

if (eventHandlerAttribute == null)
continue;

if (eventHandlerAttribute.IsCancel)
{
_cancelHandlerNetwork.Add(eventHandlerAttribute);
}
else
{
_handlerNetwork.Add(eventHandlerAttribute);
}
}
}
}

private EventHandlerAttribute? TryAddRelationNetwork(Type type, MethodInfo method)
{
try
{
if (DispatchNetworkUtils.IsSagaMode(type, method))
return null;

var attribute = method.GetCustomAttributes(typeof(EventHandlerAttribute), true).FirstOrDefault();
if (attribute is not EventHandlerAttribute handler)
return null;

var parameters = method
.GetParameters()
.Where(parameter => typeof(IEvent).IsAssignableFrom(parameter.ParameterType))
.ToList();

if (parameters.Count != 1)
throw new ArgumentOutOfRangeException(
$"[{method.Name}] only allows one parameter and inherits from Event, other parameters must support getting from DI");

var parameter = parameters[0];

handler.ActionMethodInfo = method;
handler.InstanceType = type;
handler.EventType = parameter.ParameterType;
handler.BuildExpression();

return handler;
}
catch (Exception ex)
{
_logger?.LogError(ex,
"Dispatcher: Failed to get EventBus network, type name: [{TypeName}], method: [{MethodName}]",
type.FullName ?? type.Name,
method.Name);
throw;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Dispatcher.Events.Internal.Dispatch;

internal class DefaultExecuteProvider : IExecuteProvider
{
public int Timer { get; private set; }

public EventExecuteInfo ExecuteResult { get; private set; }

public DefaultExecuteProvider()
=> ResetTimer();

public void ResetTimer()
=> Timer = 0;

public void ExecuteHandler() => Timer++;

public void Initialize()
{
ExecuteResult = new EventExecuteInfo()
{
Exception = null,
Status = ExecuteStatus.InProgress
};
}

public void SetExecuteResult(EventExecuteInfo executeInfo)
=> ExecuteResult = executeInfo;
}
Loading