Преглед изворни кода

HandlerInject支持指定多个范围

xf пре 9 месеци
родитељ
комит
c939799701

+ 3 - 0
src/Hotline.Api/Program.cs

@@ -1,4 +1,7 @@
+using EnumsNET;
 using Hotline.Api;
+using Hotline.EventBus;
+using Novacode;
 using Serilog;
 
 Log.Logger = new LoggerConfiguration()

+ 4 - 1
src/Hotline.Api/StartupHelper.cs

@@ -313,7 +313,10 @@ namespace Hotline.Api
                 {
                     var attr = d.GetCustomAttribute(typeof(HandlerInjectAttribute)) as HandlerInjectAttribute;
                     if (attr is null) return true;
-                    return attr.Enable && attr.AppScope == appScope;
+                    return attr.Enable &&
+                           attr.AppScopes.ToString()
+                               .Split(',', StringSplitOptions.RemoveEmptyEntries)
+                               .Any(x => string.Compare(x, appScope, StringComparison.Ordinal) == 0);
                 };
                 cfg.RegisterServicesFromAssemblyContaining<Program>();
             });

+ 34 - 0
src/Hotline.Application/Handlers/Order/OrderDelayWorkflowEndHandler.cs

@@ -0,0 +1,34 @@
+using Hotline.FlowEngine.Notifications;
+using MediatR;
+using Hotline.EventBus;
+using Microsoft.Extensions.Logging;
+
+namespace Hotline.Application.Handlers.Order
+{
+    [HandlerInject(AppScopes = EAppScope.LuZhou | EAppScope.YiBin)]
+    public class OrderDelayWorkflowEndHandler : INotificationHandler<EndWorkflowNotify>
+    {
+        private readonly ILogger<OrderDelayWorkflowEndHandler> _logger;
+
+        public OrderDelayWorkflowEndHandler(ILogger<OrderDelayWorkflowEndHandler> logger)
+        {
+            _logger = logger;
+        }
+
+        /// <summary>Handles a notification</summary>
+        /// <param name="notification">The notification</param>
+        /// <param name="cancellationToken">Cancellation token</param>
+        public async Task Handle(EndWorkflowNotify notification, CancellationToken cancellationToken)
+        {
+            try
+            {
+                throw new NotImplementedException();
+            }
+            catch (Exception e)
+            {
+                _logger.LogError(e.Message);
+                throw;
+            }
+        }
+    }
+}

+ 12 - 0
src/Hotline/EventBus/EAppScope.cs

@@ -0,0 +1,12 @@
+namespace Hotline.EventBus;
+
+/// <summary>
+/// 适用范围(prop名称与appsettings.json保持一致)
+/// </summary>
+[Flags]
+public enum EAppScope
+{
+    YiBin = 1 << 0,
+    ZiGong = 1 << 1,
+    LuZhou = 1 << 2,
+}

+ 1 - 1
src/Hotline/EventBus/HandlerInjectAttribute.cs

@@ -12,7 +12,7 @@ namespace Hotline.EventBus
         /// <summary>
         /// 适用范围
         /// </summary>
-        public string AppScope { get; set; }
+        public EAppScope AppScopes { get; set; }
 
         /// <summary>
         /// 是否启用

+ 7 - 1
src/Hotline/FlowEngine/Workflows/WorkflowDomainService.cs

@@ -15,6 +15,7 @@ using MediatR;
 using Microsoft.Extensions.Logging;
 using SqlSugar;
 using System.Diagnostics;
+using Hotline.EventBus;
 using XF.Domain.Authentications;
 using XF.Domain.Dependency;
 using XF.Domain.Entities;
@@ -36,6 +37,7 @@ namespace Hotline.FlowEngine.Workflows
         private readonly ISessionContext _sessionContext;
         private readonly IMapper _mapper;
         private readonly IMediator _mediator;
+        private readonly Publisher _publisher;
         private readonly ILogger<WorkflowDomainService> _logger;
         private readonly IFileRepository _fileRepository;
         private readonly IRepository<User> _userRepository;
@@ -50,6 +52,7 @@ namespace Hotline.FlowEngine.Workflows
             ISessionContext sessionContext,
             IMapper mapper,
             IMediator mediator,
+            Publisher publisher,
             ILogger<WorkflowDomainService> logger,
             IFileRepository fileRepository)
         {
@@ -62,6 +65,7 @@ namespace Hotline.FlowEngine.Workflows
             _sessionContext = sessionContext;
             _mapper = mapper;
             _mediator = mediator;
+            _publisher = publisher;
             _logger = logger;
             _fileRepository = fileRepository;
         }
@@ -178,7 +182,9 @@ namespace Hotline.FlowEngine.Workflows
             await _workflowRepository.UpdateAsync(workflow, cancellationToken);
 
             //publish
-            await _mediator.Publish(new StartWorkflowNotify(workflow, dto, flowAssignInfo, trace), cancellationToken);
+            //await _mediator.Publish(new StartWorkflowNotify(workflow, dto, flowAssignInfo, trace), cancellationToken);
+            await _publisher.PublishAsync(new StartWorkflowNotify(workflow, dto, flowAssignInfo, trace),
+                PublishStrategy.ParallelNoWait, cancellationToken);
         }
 
         public async Task<Workflow> GetWorkflowAsync(string workflowId,