diff --git a/aspnet-core/LINGYUN.MicroService.TaskManagement.sln b/aspnet-core/LINGYUN.MicroService.TaskManagement.sln index 639f7c840..a89192380 100644 --- a/aspnet-core/LINGYUN.MicroService.TaskManagement.sln +++ b/aspnet-core/LINGYUN.MicroService.TaskManagement.sln @@ -44,7 +44,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.TaskManagement. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.BackgroundTasks.TaskManagement", "modules\task-management\LINGYUN.Abp.BackgroundTasks.TaskManagement\LINGYUN.Abp.BackgroundTasks.TaskManagement.csproj", "{7937785C-0D28-46B3-A7E7-0B592821A1F2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LINGYUN.Abp.BackgroundTasks.EventBus", "modules\task-management\LINGYUN.Abp.BackgroundTasks.EventBus\LINGYUN.Abp.BackgroundTasks.EventBus.csproj", "{D17DEF79-635B-478D-89D7-32EAE616869A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.BackgroundTasks.EventBus", "modules\task-management\LINGYUN.Abp.BackgroundTasks.EventBus\LINGYUN.Abp.BackgroundTasks.EventBus.csproj", "{D17DEF79-635B-478D-89D7-32EAE616869A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN.Abp.Cli.csproj b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN.Abp.Cli.csproj index 3eef3841c..78bdee127 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN.Abp.Cli.csproj +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN.Abp.Cli.csproj @@ -5,7 +5,7 @@ Exe net6.0 - 5.2.0 + 5.2.1 colin Use LINGYUN.MicroService.Templates command line true diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs index cbddc8a83..68260f3fb 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs @@ -19,7 +19,9 @@ namespace LINGYUN.Abp.ExceptionHandling.Notifications description: L("Notifications:ExceptionNotifier"), notificationType: NotificationType.System, lifetime: NotificationLifetime.Persistent, - allowSubscriptionToClients: false); + allowSubscriptionToClients: false) + // TODO: 全局常量 + .WithProviders("SignalR"); } protected LocalizableString L(string name) diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN.Abp.Notifications.csproj b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN.Abp.Notifications.csproj index 063bb437d..d98083ef8 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN.Abp.Notifications.csproj +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN.Abp.Notifications.csproj @@ -12,7 +12,6 @@ - diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationCleanupOptions.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationCleanupOptions.cs deleted file mode 100644 index 2fe7b3a1f..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationCleanupOptions.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace LINGYUN.Abp.Notifications -{ - public class AbpNotificationCleanupOptions - { - /// - /// 是否启用清理任务 - /// 默认:启用 - /// - public bool IsEnabled { get; set; } = true; - /// - /// 清理时间间隔 - /// 默认:30_0000ms - /// - public int CleanupPeriod { get; set; } = 30_0000; - /// - /// 清理批次 - /// 默认: 200 - /// - public int CleanupBatchSize { get; set; } = 200; - } -} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationModule.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationModule.cs index 3d6a5d2e0..fafc1e65b 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationModule.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationModule.cs @@ -1,17 +1,12 @@ using LINGYUN.Abp.IdGenerator; -using LINGYUN.Abp.Notifications.Internal; using LINGYUN.Abp.RealTime; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using System; using System.Collections.Generic; -using System.Threading.Tasks; -using Volo.Abp; using Volo.Abp.BackgroundJobs; using Volo.Abp.BackgroundWorkers; using Volo.Abp.Json; using Volo.Abp.Modularity; -using Volo.Abp.Threading; namespace LINGYUN.Abp.Notifications { @@ -30,25 +25,6 @@ namespace LINGYUN.Abp.Notifications AutoAddDefinitionProviders(context.Services); } - public override void OnApplicationInitialization(ApplicationInitializationContext context) - { - AsyncHelper.RunSync(() => OnApplicationInitializationAsync(context)); - } - - public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) - { - var options = context.ServiceProvider.GetRequiredService>().Value; - if (options.IsEnabled) - { - await context.ServiceProvider - .GetRequiredService() - .AddAsync( - context.ServiceProvider - .GetRequiredService() - ); - } - } - private void AutoAddDefinitionProviders(IServiceCollection services) { var definitionProviders = new List(); diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/NotificationCleanupBackgroundWorker.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/NotificationCleanupBackgroundWorker.cs deleted file mode 100644 index 2c84c706e..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/NotificationCleanupBackgroundWorker.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; -using System.Threading.Tasks; -using Volo.Abp.BackgroundWorkers; -using Volo.Abp.Threading; - -namespace LINGYUN.Abp.Notifications.Internal -{ - internal class NotificationCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase - { - protected AbpNotificationCleanupOptions Options { get; } - - public NotificationCleanupBackgroundWorker( - AbpAsyncTimer timer, - IServiceScopeFactory serviceScopeFactory, - IOptions options) - : base(timer, serviceScopeFactory) - { - Options = options.Value; - timer.Period = Options.CleanupPeriod; - } - - protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) - { - try - { - var store = workerContext.ServiceProvider.GetRequiredService(); - Logger.LogDebug("Before cleanup exprition jobs..."); - await store.DeleteNotificationAsync(Options.CleanupBatchSize); - Logger.LogDebug("Exprition jobs cleanup job was successful..."); - } - catch (Exception ex) - { - Logger.LogWarning("Exprition jobs cleanup job was failed..."); - Logger.LogWarning("Error:{0}", ex.Message); - } - } - } -} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJob.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJob.cs deleted file mode 100644 index 954393ce4..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJob.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using System; -using System.Threading.Tasks; -using Volo.Abp.BackgroundJobs; -using Volo.Abp.DependencyInjection; - -namespace LINGYUN.Abp.Notifications -{ - public class NotificationPublishJob : AsyncBackgroundJob, ITransientDependency - { - protected AbpNotificationOptions Options { get; } - protected INotificationStore Store { get; } - protected IServiceProvider ServiceProvider { get; } - - public NotificationPublishJob( - IOptions options, - INotificationStore store, - IServiceProvider serviceProvider) - { - Store = store; - Options = options.Value; - ServiceProvider = serviceProvider; - } - - public override async Task ExecuteAsync(NotificationPublishJobArgs args) - { - var providerType = Type.GetType(args.ProviderType); - - if (ServiceProvider.GetRequiredService(providerType) is INotificationPublishProvider publishProvider) - { - var notification = await Store.GetNotificationOrNullAsync(args.TenantId, args.NotificationId); - notification.Data = NotificationDataConverter.Convert(notification.Data); - - var notifacationDataMapping = Options.NotificationDataMappings - .GetMapItemOrDefault(notification.Name, publishProvider.Name); - if (notifacationDataMapping != null) - { - notification.Data = notifacationDataMapping.MappingFunc(notification.Data); - } - await publishProvider.PublishAsync(notification, args.UserIdentifiers); - } - } - } -} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJobArgs.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJobArgs.cs deleted file mode 100644 index 128019301..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJobArgs.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace LINGYUN.Abp.Notifications -{ - public class NotificationPublishJobArgs - { - public Guid? TenantId { get; set; } - public long NotificationId { get; set; } - public string ProviderType { get; set; } - public List UserIdentifiers { get; set; } - public NotificationPublishJobArgs(long id, string providerType, List userIdentifiers, Guid? tenantId = null ) - { - NotificationId = id; - ProviderType = providerType; - UserIdentifiers = userIdentifiers; - TenantId = tenantId; - } - } -} diff --git a/aspnet-core/modules/console/LINGYUN.Abp.Encryption.Console/AbpEncryptionConsoleModule.cs b/aspnet-core/modules/console/LINGYUN.Abp.Encryption.Console/AbpEncryptionConsoleModule.cs index 9f4f8ed21..4d51b8ab4 100644 --- a/aspnet-core/modules/console/LINGYUN.Abp.Encryption.Console/AbpEncryptionConsoleModule.cs +++ b/aspnet-core/modules/console/LINGYUN.Abp.Encryption.Console/AbpEncryptionConsoleModule.cs @@ -1,12 +1,22 @@ -using Volo.Abp.Modularity; +using System.Text; +using Volo.Abp.Modularity; using Volo.Abp.Security; - +using Volo.Abp.Security.Encryption; + namespace LINGYUN.Abp.Encryption.Console { [DependsOn( typeof(AbpSecurityModule))] public class AbpEncryptionConsoleModule : AbpModule { - + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; + options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); + options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); + }); + } } } diff --git a/aspnet-core/modules/message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Notifications/INotificationRepository.cs b/aspnet-core/modules/message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Notifications/INotificationRepository.cs index 074456b6a..162a20906 100644 --- a/aspnet-core/modules/message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Notifications/INotificationRepository.cs +++ b/aspnet-core/modules/message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Notifications/INotificationRepository.cs @@ -1,4 +1,5 @@ -using System.Threading; +using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -10,7 +11,7 @@ namespace LINGYUN.Abp.MessageService.Notifications long notificationId, CancellationToken cancellationToken = default); - Task DeleteExpritionAsync( + Task> GetExpritionAsync( int batchCount, CancellationToken cancellationToken = default); } diff --git a/aspnet-core/modules/message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Notifications/NotificationStore.cs b/aspnet-core/modules/message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Notifications/NotificationStore.cs index 3b28bf4fb..aacfde4b9 100644 --- a/aspnet-core/modules/message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Notifications/NotificationStore.cs +++ b/aspnet-core/modules/message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Notifications/NotificationStore.cs @@ -92,7 +92,9 @@ namespace LINGYUN.Abp.MessageService.Notifications { using (var unitOfWork = _unitOfWorkManager.Begin()) { - await _notificationRepository.DeleteExpritionAsync(batchCount, cancellationToken); + var notitications = await _notificationRepository.GetExpritionAsync(batchCount, cancellationToken); + + await _notificationRepository.DeleteManyAsync(notitications); await unitOfWork.CompleteAsync(cancellationToken); } diff --git a/aspnet-core/modules/message/LINGYUN.Abp.MessageService.EntityFrameworkCore/LINGYUN/Abp/MessageService/Notifications/EfCoreNotificationRepository.cs b/aspnet-core/modules/message/LINGYUN.Abp.MessageService.EntityFrameworkCore/LINGYUN/Abp/MessageService/Notifications/EfCoreNotificationRepository.cs index b6cc1d0ae..4abeb50c5 100644 --- a/aspnet-core/modules/message/LINGYUN.Abp.MessageService.EntityFrameworkCore/LINGYUN/Abp/MessageService/Notifications/EfCoreNotificationRepository.cs +++ b/aspnet-core/modules/message/LINGYUN.Abp.MessageService.EntityFrameworkCore/LINGYUN/Abp/MessageService/Notifications/EfCoreNotificationRepository.cs @@ -1,6 +1,7 @@ using LINGYUN.Abp.MessageService.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -19,20 +20,14 @@ namespace LINGYUN.Abp.MessageService.Notifications { } - public async Task DeleteExpritionAsync( + public async Task> GetExpritionAsync( int batchCount, CancellationToken cancellationToken = default) { - var dbSet = await GetDbSetAsync(); - var batchDeleteNoticeWithIds = await dbSet - .Where(x => x.ExpirationTime <= DateTime.Now) + return await (await GetDbSetAsync()) + .Where(x => x.ExpirationTime.Value.CompareTo(DateTime.Now) <= 0) .Take(batchCount) - .Select(x => new Notification(x.Id)) - .AsNoTracking() - .ToArrayAsync(GetCancellationToken(cancellationToken)); - - dbSet.AttachRange(batchDeleteNoticeWithIds); - dbSet.RemoveRange(batchDeleteNoticeWithIds); + .ToListAsync(GetCancellationToken(cancellationToken)); } public async Task GetByIdAsync( diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationCleanupJob.cs b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationCleanupJob.cs new file mode 100644 index 000000000..dd1fd2b6e --- /dev/null +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationCleanupJob.cs @@ -0,0 +1,22 @@ +using LINGYUN.Abp.BackgroundTasks; +using LINGYUN.Abp.Notifications; +using System.Threading.Tasks; + +namespace LY.MicroService.RealtimeMessage.BackgroundJobs; + +public class NotificationCleanupJob : IJobRunnable +{ + /// + /// 每次清除记录大小 + /// + public const string PropertyBatchCount = "BatchCount"; + + + public async virtual Task ExecuteAsync(JobRunnableContext context) + { + var count = context.GetJobData(PropertyBatchCount); + var store = context.GetRequiredService(); + + await store.DeleteNotificationAsync(count); + } +} diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationPublishJob.cs b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationPublishJob.cs new file mode 100644 index 000000000..2f447a3f4 --- /dev/null +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationPublishJob.cs @@ -0,0 +1,45 @@ +using LINGYUN.Abp.Notifications; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System; +using System.Threading.Tasks; +using Volo.Abp.BackgroundJobs; +using Volo.Abp.DependencyInjection; + +namespace LY.MicroService.RealtimeMessage.BackgroundJobs; + +public class NotificationPublishJob : AsyncBackgroundJob, ITransientDependency +{ + protected AbpNotificationOptions Options { get; } + protected INotificationStore Store { get; } + protected IServiceProvider ServiceProvider { get; } + + public NotificationPublishJob( + IOptions options, + INotificationStore store, + IServiceProvider serviceProvider) + { + Store = store; + Options = options.Value; + ServiceProvider = serviceProvider; + } + + public override async Task ExecuteAsync(NotificationPublishJobArgs args) + { + var providerType = Type.GetType(args.ProviderType); + + if (ServiceProvider.GetRequiredService(providerType) is INotificationPublishProvider publishProvider) + { + var notification = await Store.GetNotificationOrNullAsync(args.TenantId, args.NotificationId); + notification.Data = NotificationDataConverter.Convert(notification.Data); + + var notifacationDataMapping = Options.NotificationDataMappings + .GetMapItemOrDefault(notification.Name, publishProvider.Name); + if (notifacationDataMapping != null) + { + notification.Data = notifacationDataMapping.MappingFunc(notification.Data); + } + await publishProvider.PublishAsync(notification, args.UserIdentifiers); + } + } +} diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationPublishJobArgs.cs b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationPublishJobArgs.cs new file mode 100644 index 000000000..d15cec9a0 --- /dev/null +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/BackgroundJobs/NotificationPublishJobArgs.cs @@ -0,0 +1,20 @@ +using LINGYUN.Abp.Notifications; +using System; +using System.Collections.Generic; + +namespace LY.MicroService.RealtimeMessage.BackgroundJobs; + +public class NotificationPublishJobArgs +{ + public Guid? TenantId { get; set; } + public long NotificationId { get; set; } + public string ProviderType { get; set; } + public List UserIdentifiers { get; set; } + public NotificationPublishJobArgs(long id, string providerType, List userIdentifiers, Guid? tenantId = null) + { + NotificationId = id; + ProviderType = providerType; + UserIdentifiers = userIdentifiers; + TenantId = tenantId; + } +} diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs index e4da34377..32c029759 100644 --- a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs @@ -1,4 +1,5 @@ using LINGYUN.Abp.Notifications; +using LY.MicroService.RealtimeMessage.BackgroundJobs; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; @@ -121,6 +122,12 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed var providers = Enumerable .Reverse(NotificationPublishProviderManager.Providers); + // 过滤用户指定提供者 + if (notification.Providers.Any()) + { + providers = providers.Where(p => notification.Providers.Contains(p.Name)); + } + await PublishFromProvidersAsync(providers, eventData.Users, notificationInfo); } } diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/LY.MicroService.RealtimeMessage.HttpApi.Host.csproj b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/LY.MicroService.RealtimeMessage.HttpApi.Host.csproj index b31fbd568..7b3103481 100644 --- a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/LY.MicroService.RealtimeMessage.HttpApi.Host.csproj +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/LY.MicroService.RealtimeMessage.HttpApi.Host.csproj @@ -19,6 +19,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -43,16 +44,15 @@ - - - - + + + diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.Configure.cs b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.Configure.cs index 3a6bb8c74..592a73bdb 100644 --- a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.Configure.cs +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.Configure.cs @@ -1,30 +1,26 @@ using DotNetCore.CAP; -using Hangfire; -using Hangfire.Dashboard; +using LINGYUN.Abp.BackgroundTasks; using LINGYUN.Abp.ExceptionHandling; -using LINGYUN.Abp.Hangfire.Dashboard.Authorization; using LINGYUN.Abp.Localization.CultureMap; using LINGYUN.Abp.MessageService.Localization; -using LINGYUN.Abp.MessageService.Permissions; using LINGYUN.Abp.Serilog.Enrichers.Application; using LINGYUN.Abp.Serilog.Enrichers.UniqueId; +using LY.MicroService.RealtimeMessage.BackgroundJobs; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.DataProtection; -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Caching.StackExchangeRedis; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; +using Quartz; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text.Encodings.Web; using System.Text.Unicode; -using System.Threading.Tasks; using Volo.Abp; -using Volo.Abp.AspNetCore.Auditing; using Volo.Abp.Auditing; using Volo.Abp.Caching; using Volo.Abp.EntityFrameworkCore; @@ -33,16 +29,15 @@ using Volo.Abp.Json; using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Localization; using Volo.Abp.MultiTenancy; +using Volo.Abp.Quartz; using Volo.Abp.Threading; -using Volo.Abp.Timing; using Volo.Abp.VirtualFileSystem; -using HangfireDashboardOptions = Hangfire.DashboardOptions; namespace LY.MicroService.RealtimeMessage; public partial class RealtimeMessageHttpApiHostModule { - protected static string[] PrefixTokenQueryStrings = new [] { "/signalr-hubs", "/hangfire" }; + protected const string ApplicationName = "MessageService"; private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); @@ -56,7 +51,7 @@ public partial class RealtimeMessageHttpApiHostModule private void PreConfigureApp() { - AbpSerilogEnrichersConsts.ApplicationName = "MessageService"; + AbpSerilogEnrichersConsts.ApplicationName = ApplicationName; PreConfigure(options => { @@ -84,16 +79,41 @@ public partial class RealtimeMessageHttpApiHostModule }); } - private void PreCongifureHangfire() + private void PreConfigureQuartz(IConfiguration configuration) { - PreConfigure(options => + PreConfigure(options => { - options.AsyncAuthorization = new IDashboardAsyncAuthorizationFilter[] + // 如果使用持久化存储, 则配置quartz持久层 + if (configuration.GetSection("Quartz:UsePersistentStore").Get()) { - new DashboardAuthorizationFilter( - MessageServicePermissions.Hangfire.Dashboard, - MessageServicePermissions.Hangfire.ManageQueue) - }; + var settings = configuration.GetSection("Quartz:Properties").Get>(); + if (settings != null) + { + foreach (var setting in settings) + { + options.Properties[setting.Key] = setting.Value; + } + } + + options.Configurator += (config) => + { + config.UsePersistentStore(store => + { + store.UseProperties = false; + store.UseJsonSerializer(); + }); + }; + } + }); + } + + private void ConfigureBackgroundTasks() + { + Configure(options => + { + options.NodeName = ApplicationName; + + options.AddProvider("NotificationCleanupJob"); }); } @@ -139,14 +159,9 @@ public partial class RealtimeMessageHttpApiHostModule private void ConfigureAuditing(IConfiguration configuration) { - Configure(options => - { - options.IgnoredUrls.AddIfNotContains("/hangfire"); - }); - Configure(options => { - options.ApplicationName = "MessageService"; + options.ApplicationName = ApplicationName; // 是否启用实体变更记录 var entitiesChangedConfig = configuration.GetSection("App:TrackingEntitiesChanged"); if (entitiesChangedConfig.Exists() && entitiesChangedConfig.Get()) @@ -300,42 +315,6 @@ public partial class RealtimeMessageHttpApiHostModule options.Authority = configuration["AuthServer:Authority"]; options.RequireHttpsMetadata = false; options.Audience = configuration["AuthServer:ApiName"]; - options.Events = new JwtBearerEvents - { - OnMessageReceived = context => - { - var accessToken = context.Request.Query["access_token"]; - var path = context.HttpContext.Request.Path; - if (!string.IsNullOrEmpty(accessToken) && - PrefixTokenQueryStrings.Any(prefix => path.StartsWithSegments(prefix))) - { - // 解决仪表板自定义授权问题 - context.Token = accessToken; - - var clock = context.HttpContext.RequestServices.GetRequiredService(); - var protectedProvider = context.HttpContext.RequestServices.GetRequiredService(); - var protector = protectedProvider.CreateProtector("_hangfire_tk"); - var tokenCookies = protector.Protect(accessToken); - context.HttpContext.Response.Cookies.Append( - "_hangfire_tk", - tokenCookies, - new CookieOptions - { - Expires = clock.Now.AddMinutes(10) - }); - } - - if (context.Token.IsNullOrWhiteSpace() && - context.Request.Cookies.TryGetValue("_hangfire_tk", out var protectedToken)) - { - var protectedProvider = context.HttpContext.RequestServices.GetRequiredService(); - var protector = protectedProvider.CreateProtector("_hangfire_tk"); - context.Token = protector.Unprotect(protectedToken); - } - - return Task.CompletedTask; - }, - }; }); if (!isDevelopment) @@ -347,9 +326,4 @@ public partial class RealtimeMessageHttpApiHostModule .PersistKeysToStackExchangeRedis(redis, "LINGYUN.Abp.Application:DataProtection:Protection-Keys"); } } - - private void ConfigureHangfireServer(IServiceCollection services) - { - services.AddHangfireServer(); - } } diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.cs b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.cs index 3ae527f91..f1fcece17 100644 --- a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.cs +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.cs @@ -1,14 +1,12 @@ using DotNetCore.CAP; -using Hangfire; using LINGYUN.Abp.AspNetCore.HttpOverrides; using LINGYUN.Abp.AuditLogging.Elasticsearch; using LINGYUN.Abp.Authorization.OrganizationUnits; -using LINGYUN.Abp.BackgroundJobs.Hangfire; -using LINGYUN.Abp.BackgroundWorkers.Hangfire; +using LINGYUN.Abp.BackgroundTasks.ExceptionHandling; +using LINGYUN.Abp.BackgroundTasks.Quartz; using LINGYUN.Abp.Data.DbMigrator; using LINGYUN.Abp.EventBus.CAP; using LINGYUN.Abp.ExceptionHandling.Notifications; -using LINGYUN.Abp.Hangfire.Storage.MySql; using LINGYUN.Abp.Identity.WeChat; using LINGYUN.Abp.IM.SignalR; using LINGYUN.Abp.Localization.CultureMap; @@ -21,6 +19,7 @@ using LINGYUN.Abp.Notifications.WeChat.MiniProgram; using LINGYUN.Abp.Saas.EntityFrameworkCore; using LINGYUN.Abp.Serilog.Enrichers.Application; using LINGYUN.Abp.Serilog.Enrichers.UniqueId; +using LINGYUN.Abp.TaskManagement.EntityFrameworkCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -37,9 +36,9 @@ using Volo.Abp.Modularity; using Volo.Abp.PermissionManagement.EntityFrameworkCore; using Volo.Abp.SettingManagement.EntityFrameworkCore; -namespace LY.MicroService.RealtimeMessage -{ - [DependsOn( +namespace LY.MicroService.RealtimeMessage; + +[DependsOn( typeof(AbpSerilogEnrichersApplicationModule), typeof(AbpSerilogEnrichersUniqueIdModule), typeof(AbpAspNetCoreSerilogModule), @@ -48,6 +47,9 @@ namespace LY.MicroService.RealtimeMessage typeof(AbpMessageServiceApplicationModule), typeof(AbpMessageServiceHttpApiModule), typeof(AbpIdentityWeChatModule), + typeof(AbpBackgroundTasksQuartzModule), + typeof(AbpBackgroundTasksExceptionHandlingModule), + typeof(TaskManagementEntityFrameworkCoreModule), typeof(AbpMessageServiceEntityFrameworkCoreModule), typeof(AbpIdentityEntityFrameworkCoreModule), typeof(AbpSaasEntityFrameworkCoreModule), @@ -57,9 +59,6 @@ namespace LY.MicroService.RealtimeMessage typeof(AbpDataDbMigratorModule), typeof(AbpAspNetCoreAuthenticationJwtBearerModule), typeof(AbpAuthorizationOrganizationUnitsModule), - typeof(AbpHangfireMySqlStorageModule), - typeof(AbpBackgroundJobsHangfireModule), - typeof(AbpBackgroundWorkersHangfireModule), typeof(AbpBackgroundWorkersModule), typeof(AbpIMSignalRModule), typeof(AbpNotificationsSmsModule), @@ -72,79 +71,75 @@ namespace LY.MicroService.RealtimeMessage typeof(AbpLocalizationCultureMapModule), typeof(AbpAutofacModule) )] - public partial class RealtimeMessageHttpApiHostModule : AbpModule - { - private const string DefaultCorsPolicyName = "Default"; +public partial class RealtimeMessageHttpApiHostModule : AbpModule +{ + private const string DefaultCorsPolicyName = "Default"; - public override void PreConfigureServices(ServiceConfigurationContext context) - { - var configuration = context.Services.GetConfiguration(); + public override void PreConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); - PreConfigureApp(); - PreConfigureFeature(); - PreCongifureHangfire(); - PreConfigureCAP(configuration); - } + PreConfigureApp(); + PreConfigureFeature(); + PreConfigureCAP(configuration); + PreConfigureQuartz(configuration); + } - public override void ConfigureServices(ServiceConfigurationContext context) - { - var hostingEnvironment = context.Services.GetHostingEnvironment(); - var configuration = context.Services.GetConfiguration(); + public override void ConfigureServices(ServiceConfigurationContext context) + { + var hostingEnvironment = context.Services.GetHostingEnvironment(); + var configuration = context.Services.GetConfiguration(); - ConfigureDbContext(); - ConfigureLocalization(); - ConfigureJsonSerializer(); - ConfigreExceptionHandling(); - ConfigureVirtualFileSystem(); - ConfigureCaching(configuration); - ConfigureAuditing(configuration); - ConfigureSwagger(context.Services); - ConfigureMultiTenancy(configuration); - ConfigureHangfireServer(context.Services); - ConfigureCors(context.Services, configuration); - ConfigureSeedWorker(context.Services, hostingEnvironment.IsDevelopment()); - ConfigureSecurity(context.Services, configuration, hostingEnvironment.IsDevelopment()); - } + ConfigureDbContext(); + ConfigureLocalization(); + ConfigureJsonSerializer(); + ConfigureBackgroundTasks(); + ConfigreExceptionHandling(); + ConfigureVirtualFileSystem(); + ConfigureCaching(configuration); + ConfigureAuditing(configuration); + ConfigureSwagger(context.Services); + ConfigureMultiTenancy(configuration); + ConfigureCors(context.Services, configuration); + ConfigureSeedWorker(context.Services, hostingEnvironment.IsDevelopment()); + ConfigureSecurity(context.Services, configuration, hostingEnvironment.IsDevelopment()); + } - public override void OnApplicationInitialization(ApplicationInitializationContext context) + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + // http调用链 + app.UseCorrelationId(); + // 虚拟文件系统 + app.UseStaticFiles(); + // 路由 + app.UseRouting(); + // 跨域 + app.UseCors(DefaultCorsPolicyName); + // 认证 + app.UseAuthentication(); + app.UseAbpClaimsMap(); + // jwt + app.UseJwtTokenMiddleware(); + // 多租户 + app.UseMultiTenancy(); + // 本地化 + app.UseMapRequestLocalization(); + // 授权 + app.UseAuthorization(); + // Cap Dashboard + app.UseCapDashboard(); + // Swagger + app.UseSwagger(); + // Swagger可视化界面 + app.UseSwaggerUI(options => { - var app = context.GetApplicationBuilder(); - // http调用链 - app.UseCorrelationId(); - // 虚拟文件系统 - app.UseStaticFiles(); - // 路由 - app.UseRouting(); - // 跨域 - app.UseCors(DefaultCorsPolicyName); - // 认证 - app.UseAuthentication(); - app.UseAbpClaimsMap(); - // jwt - app.UseJwtTokenMiddleware(); - // 多租户 - app.UseMultiTenancy(); - // 本地化 - app.UseMapRequestLocalization(); - // 授权 - app.UseAuthorization(); - // Cap Dashboard - app.UseCapDashboard(); - // Swagger - app.UseSwagger(); - // Swagger可视化界面 - app.UseSwaggerUI(options => - { - options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support Realtime Message API"); - }); - // 审计日志 - app.UseAuditing(); - app.UseAbpSerilogEnrichers(); - // 将在 2.0.0版本移除 - // app.UseHangfireServer(); - app.UseHangfireDashboard(); - // 路由 - app.UseConfiguredEndpoints(); - } + options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support Realtime Message API"); + }); + // 审计日志 + app.UseAuditing(); + app.UseAbpSerilogEnrichers(); + // 路由 + app.UseConfiguredEndpoints(); } } diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/appsettings.Development.json b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/appsettings.Development.json index 9a0f09f66..2b3bb333c 100644 --- a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/appsettings.Development.json +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/appsettings.Development.json @@ -25,6 +25,7 @@ "ConnectionStrings": { "Default": "Server=127.0.0.1;Database=Messages;User Id=root;Password=123456", "MessageService": "Server=127.0.0.1;Database=Messages;User Id=root;Password=123456", + "TaskManagement": "Server=127.0.0.1;Database=Platform;User Id=root;Password=123456", "AbpIdentity": "Server=127.0.0.1;Database=IdentityServer;User Id=root;Password=123456", "AbpSaas": "Server=127.0.0.1;Database=Platform;User Id=root;Password=123456", "AbpSettingManagement": "Server=127.0.0.1;Database=Platform;User Id=root;Password=123456", @@ -39,13 +40,19 @@ "Authority": "http://127.0.0.1:44385/", "ApiName": "lingyun-abp-application" }, - "Hangfire": { - "MySql": { - "Connection": "Server=127.0.0.1;Database=Messages;User Id=root;Password=123456;Allow User Variables=true", - "TablePrefix": "AppHangfire" - }, - "Dashboard": { - "WhiteList": "http://127.0.0.1:30000" + "Quartz": { + "UsePersistentStore": false, + "Properties": { + "quartz.jobStore.dataSource": "tkm", + "quartz.jobStore.type": "Quartz.Impl.AdoJobStore.JobStoreTX,Quartz", + "quartz.jobStore.driverDelegateType": "Quartz.Impl.AdoJobStore.MySQLDelegate,Quartz", + "quartz.dataSource.tkm.connectionString": "Server=127.0.0.1;Database=Platform;User Id=root;Password=123456", + "quartz.dataSource.tkm.connectionStringName": "TaskManagement", + "quartz.dataSource.tkm.provider": "MySqlConnector", + "quartz.jobStore.clustered": "true", + "quartz.serializer.type": "json", + "quartz.scheduler.instanceName": "message-service", + "quartz.scheduler.instanceId": "3c36620a-d8c5-4c89-9828-78f33547751d" } }, "Notifications": { @@ -90,7 +97,6 @@ "Override": { "Microsoft.EntityFrameworkCore": "Warning", "DotNetCore.CAP": "Debug", - "Hangfire.Server": "Warning", "System": "Warning", "Microsoft": "Warning", "Microsoft.AspNetCore.SignalR": "Debug",