示例#1
0
        public virtual async Task PublishAsync(
            string pushRequestName,
            PushRequestData data = null,
            EntityIdentifier entityIdentifier = null,
            PushRequestPriority priority      = PushRequestPriority.Normal,
            IUserIdentifier[] userIds         = null,
            IUserIdentifier[] excludedUserIds = null,
            int?[] tenantIds = null)
        {
            if (pushRequestName.IsNullOrEmpty())
            {
                throw new ArgumentException("PushRequestName can not be null or whitespace!", nameof(pushRequestName));
            }

            if (!tenantIds.IsNullOrEmpty() && !userIds.IsNullOrEmpty())
            {
                throw new ArgumentException("tenantIds can be set only if userIds is not set!", nameof(tenantIds));
            }

            if (tenantIds.IsNullOrEmpty() && userIds.IsNullOrEmpty())
            {
                tenantIds = new[] { AbpSession.TenantId };
            }

            var pushRequest = new PushRequest(GuidGenerator.Create())
            {
                Name           = pushRequestName,
                EntityTypeName = entityIdentifier?.Type.FullName,
                EntityTypeAssemblyQualifiedName = entityIdentifier?.Type.AssemblyQualifiedName,
                EntityId        = entityIdentifier?.Id.ToJsonString(),
                Priority        = priority,
                UserIds         = userIds.IsNullOrEmpty() ? null : userIds.Select(uid => uid.ToUserIdentifier().ToUserIdentifierString()).JoinAsString(","),
                ExcludedUserIds = excludedUserIds.IsNullOrEmpty() ? null : excludedUserIds.Select(uid => uid.ToUserIdentifier().ToUserIdentifierString()).JoinAsString(","),
                TenantIds       = PushRequest.ToTenantIds(tenantIds),
                Data            = data?.ToJsonString(),
                DataTypeName    = data?.GetType().AssemblyQualifiedName
            };

            await RequestStore.InsertRequestAsync(pushRequest);

            await CurrentUnitOfWork.SaveChangesAsync(); //To get Id of the push request

            if (userIds != null && userIds.Length <= Configuration.MaxUserCountForForegroundDistribution)
            {
                //We can directly distribute the push request since there are not much receivers
                await RequestDistributor.DistributeAsync(pushRequest.Id);
            }
            else
            {
                //We enqueue a background job since distributing may get a long time
                await BackgroundJobManager.EnqueueAsync <PushRequestDistributionJob, PushRequestDistributionJobArgs>(
                    new PushRequestDistributionJobArgs(
                        pushRequest.Id
                        )
                    );
            }
        }
 public override void Execute(PushRequestDistributionJobArgs args)
 {
     AsyncHelper.RunSync(() => _pushRequestDistributor.DistributeAsync(args.PushRequestId));
 }