public virtual async Task PublishAsync(string notificationName, NotificationData data = null, EntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, long[] userIds = null) { if (notificationName.IsNullOrEmpty()) { throw new ArgumentException("NotificationName can not be null or whitespace!", "notificationName"); } var notificationInfo = new NotificationInfo { NotificationName = notificationName, EntityTypeName = entityIdentifier == null ? null : entityIdentifier.Type.FullName, EntityTypeAssemblyQualifiedName = entityIdentifier == null ? null : entityIdentifier.Type.AssemblyQualifiedName, EntityId = entityIdentifier == null ? null : entityIdentifier.Id.ToJsonString(), Severity = severity, UserIds = userIds.IsNullOrEmpty() ? null : userIds.JoinAsString(","), Data = data == null ? null : data.ToJsonString(), DataTypeName = data == null ? null : data.GetType().AssemblyQualifiedName }; await _store.InsertNotificationAsync(notificationInfo); await CurrentUnitOfWork.SaveChangesAsync(); //To get Id of the notification await _backgroundJobManager.EnqueueAsync <NotificationDistributionJob, NotificationDistributionJobArgs>( new NotificationDistributionJobArgs( notificationInfo.Id ) ); }
/// <summary> /// 指定提供者发布通知 /// </summary> /// <param name="providers">提供者列表</param> /// <param name="notificationInfo">通知信息</param> /// <returns></returns> protected async Task PublishFromProvidersAsync(IEnumerable <INotificationPublishProvider> providers, NotificationInfo notificationInfo) { Logger.LogDebug($"Persistent notification {notificationInfo.Name}"); // 持久化通知 await _notificationStore.InsertNotificationAsync(notificationInfo); Logger.LogDebug($"Gets a list of user subscriptions {notificationInfo.Name}"); // 获取用户订阅列表 var userSubscriptions = await _notificationStore.GetSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name); Logger.LogDebug($"Persistent user notifications {notificationInfo.Name}"); // 持久化用户通知 var subscriptionUserIdentifiers = userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName)); await _notificationStore.InsertUserNotificationsAsync(notificationInfo, subscriptionUserIdentifiers.Select(u => u.UserId)); // 发布通知 foreach (var provider in providers) { await PublishAsync(provider, notificationInfo, subscriptionUserIdentifiers); } // TODO: 需要计算队列大小,根据情况是否需要并行发布消息 //Parallel.ForEach(providers, async (provider) => //{ // await PublishAsync(provider, notificationInfo, subscriptionUserIdentifiers); //}); }
public virtual async Task PublishAsync( string notificationName, NotificationData data = null, EntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, UserIdentifier[] userIds = null, UserIdentifier[] excludedUserIds = null, int?[] tenantIds = null) { if (notificationName.IsNullOrEmpty()) { throw new ArgumentException("NotificationName can not be null or whitespace!", "notificationName"); } if (!tenantIds.IsNullOrEmpty() && !userIds.IsNullOrEmpty()) { throw new ArgumentException("tenantIds can be set only if userIds is not set!", "tenantIds"); } if (tenantIds.IsNullOrEmpty() && userIds.IsNullOrEmpty()) { tenantIds = new[] { CodeZeroSession.TenantId }; } var notificationInfo = new NotificationInfo(_guidGenerator.Create()) { NotificationName = notificationName, EntityTypeName = entityIdentifier == null ? null : entityIdentifier.Type.FullName, EntityTypeAssemblyQualifiedName = entityIdentifier == null ? null : entityIdentifier.Type.AssemblyQualifiedName, EntityId = entityIdentifier == null ? null : entityIdentifier.Id.ToJsonString(), Severity = severity, UserIds = userIds.IsNullOrEmpty() ? null : userIds.Select(uid => uid.ToUserIdentifierString()).JoinAsString(","), ExcludedUserIds = excludedUserIds.IsNullOrEmpty() ? null : excludedUserIds.Select(uid => uid.ToUserIdentifierString()).JoinAsString(","), TenantIds = tenantIds.IsNullOrEmpty() ? null : tenantIds.JoinAsString(","), Data = data == null ? null : data.ToJsonString(), DataTypeName = data == null ? null : data.GetType().AssemblyQualifiedName }; await _store.InsertNotificationAsync(notificationInfo); await CurrentUnitOfWork.SaveChangesAsync(); //To get Id of the notification if (userIds != null && userIds.Length <= MaxUserCountToDirectlyDistributeANotification) { //We can directly distribute the notification since there are not much receivers await _notificationDistributer.DistributeAsync(notificationInfo.Id); } else { //We enqueue a background job since distributing may get a long time await _backgroundJobManager.EnqueueAsync <NotificationDistributionJob, NotificationDistributionJobArgs>( new NotificationDistributionJobArgs( notificationInfo.Id ) ); } }
private async Task AddNotificationsAsync() { var notification1 = new NotificationInfo { Name = NotificationsTestsNames.Test1, Severity = NotificationSeverity.Success, CreationTime = DateTime.Now, Lifetime = NotificationLifetime.OnlyOne, Type = NotificationType.Application }; var notification2 = new NotificationInfo { Name = NotificationsTestsNames.Test2, Severity = NotificationSeverity.Success, CreationTime = DateTime.Now, Lifetime = NotificationLifetime.Persistent, Type = NotificationType.Application }; var notification3 = new NotificationInfo { Name = NotificationsTestsNames.Test3, Severity = NotificationSeverity.Success, CreationTime = DateTime.Now, Lifetime = NotificationLifetime.OnlyOne, Type = NotificationType.User }; await _notificationStore.InsertNotificationAsync(notification1); await _notificationStore.InsertNotificationAsync(notification2); await _notificationStore.InsertNotificationAsync(notification3); NotificationsTestConsts.NotificationId1 = notification1.GetId(); NotificationsTestConsts.NotificationId2 = notification2.GetId(); NotificationsTestConsts.NotificationId3 = notification3.GetId(); await AddUserNotificationsAsync(notification2, new Guid[] { NotificationsTestConsts.User1Id, NotificationsTestConsts.User2Id }); await AddUserNotificationsAsync(notification3, new Guid[] { NotificationsTestConsts.User1Id }); await _notificationStore.ChangeUserNotificationReadStateAsync(null, NotificationsTestConsts.User1Id, NotificationsTestConsts.NotificationId3, NotificationReadState.Read); }
public async Task DispatcheAsync(NotificationInfo notification) { // 持久化通知 await _notificationStore.InsertNotificationAsync(notification); // 获取用户订阅列表 var userSubscriptions = await _notificationStore.GetSubscriptionsAsync(notification.TenantId, notification.Name); // 持久化用户通知 var subscriptionUserIds = userSubscriptions.Select(us => us.UserId); await _notificationStore.InsertUserNotificationsAsync(notification, subscriptionUserIds); // 发布用户通知 await _notificationPublisher.PublishAsync(notification, subscriptionUserIds); }
public async Task SendAsync( string notificationName, INotificationData data = null, EntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, long[] userIds = null, long[] excludedUserIds = null) { Throw.IfArgumentNull(notificationName, nameof(notificationName)); var notification = new NotificationInfo(_guidGenerator.Create()) { NotificationName = notificationName, NotificationDataTypeAssemblyQualifiedName = data == null ? typeof(NotificationData).AssemblyQualifiedName : data.GetType().AssemblyQualifiedName, EntityTypeName = entityIdentifier?.Type.FullName, EntityTypeAssemblyQualifiedName = entityIdentifier?.Type.AssemblyQualifiedName, EntityId = entityIdentifier?.Id.ToJsonString(), Severity = severity, UserIds = userIds.IsNullOrEmpty() ? null : userIds.Select(uid => uid).JoinAsString(","), ExcludedUserIds = excludedUserIds.IsNullOrEmpty() ? null : excludedUserIds.Select(uid => uid).JoinAsString(","), Data = data?.ToJsonString(), DataTypeName = data?.GetType().AssemblyQualifiedName }; await _unitOfWorkManager.PerformAsyncUow(async() => { await _notificationStore.InsertNotificationAsync(notification); // To get Id of the notification await _unitOfWorkManager.Current.SaveChangesAsync(); if (userIds != null && userIds.Length <= MaxUserCountToDirectlySendANotification) { // We can process the notification since there are not much receivers await _notificationDistributer.DistributeAsync(notification.Id); } else { await _backgroundJobManager .EnqueueAsync <NotificationDistributerJob, NotificationDistributerJobArgs>( new NotificationDistributerJobArgs(notification.Id)); } }); }
public virtual async Task PublishAsync(NotificationPublishOptions options) { CheckNotificationName(options.NotificationName); var notificationInfo = new NotificationInfo { NotificationName = options.NotificationName, EntityType = options.EntityType, EntityId = options.EntityId, Severity = options.Severity, UserIds = options.UserIds.IsNullOrEmpty() ? null : options.UserIds.JoinAsString(",") }; await _store.InsertNotificationAsync(notificationInfo); await _backgroundJobManager.EnqueueAsync <NotificationDistributionJob, NotificationDistributionJobArgs>( new NotificationDistributionJobArgs( notificationInfo.Id ) ); }
public virtual async Task PublishAsync( string notificationName, NotificationData data = null, EntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, UserIdentifier[] userIds = null, UserIdentifier[] excludedUserIds = null, int?[] tenantIds = null) { Guid?notificationId = null; using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)) { if (notificationName.IsNullOrEmpty()) { throw new ArgumentException("NotificationName can not be null or whitespace!", "notificationName"); } if (!tenantIds.IsNullOrEmpty() && !userIds.IsNullOrEmpty()) { throw new ArgumentException("tenantIds can be set only if userIds is not set!", "tenantIds"); } if (tenantIds.IsNullOrEmpty() && userIds.IsNullOrEmpty()) { tenantIds = new[] { AbpSession.TenantId }; } var notificationInfo = new NotificationInfo(_guidGenerator.Create()) { NotificationName = notificationName, EntityTypeName = entityIdentifier == null ? null : entityIdentifier.Type.FullName, EntityTypeAssemblyQualifiedName = entityIdentifier == null ? null : entityIdentifier.Type.AssemblyQualifiedName, EntityId = entityIdentifier == null ? null : entityIdentifier.Id.ToJsonString(), Severity = severity, UserIds = userIds.IsNullOrEmpty() ? null : userIds.Select(uid => uid.ToUserIdentifierString()).JoinAsString(","), ExcludedUserIds = excludedUserIds.IsNullOrEmpty() ? null : excludedUserIds.Select(uid => uid.ToUserIdentifierString()).JoinAsString(","), TenantIds = tenantIds.IsNullOrEmpty() ? null : tenantIds.JoinAsString(","), Data = data == null ? null : data.ToJsonString(), DataTypeName = data == null ? null : data.GetType().AssemblyQualifiedName }; await _store.InsertNotificationAsync(notificationInfo); await uow.CompleteAsync(); //To get Id of the notification notificationId = notificationInfo.Id; } if (notificationId == null) { throw new ShaNotificationSaveFailedException(notificationName, data); } var isShaNotification = data != null && data is ShaNotificationData; if (isShaNotification || userIds != null && userIds.Length <= MaxUserCountToDirectlyDistributeANotification) { await _notificationDistributer.DistributeAsync(notificationId.Value); } else { //We enqueue a background job since distributing may get a long time await _backgroundJobManager.EnqueueAsync <NotificationDistributionJob, NotificationDistributionJobArgs>( new NotificationDistributionJobArgs( notificationId.Value ) ); } }