예제 #1
0
 /// <summary>
 ///将要执行的作业排队。
 /// </summary>
 /// <typeparam name="TJob">作业的类型。</typeparam>
 /// <typeparam name="TArgs">作业参数的类型。</typeparam>
 /// <param name="backgroundJobManager">后台作业管理器参考</param>
 /// <param name="args">工作参数。</param>
 /// <param name="priority">工作优先级。</param>
 /// <param name="delay">作业延迟(第一次尝试前的等待时间)。</param>
 public static void Enqueue <TJob, TArgs>(this IBackgroundJobManager backgroundJobManager, TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan?delay = null)
     where TJob : IBackgroundJob <TArgs>
 {
     AsyncHelper.RunSync(() => backgroundJobManager.EnqueueAsync <TJob, TArgs>(args, priority, delay));
 }
예제 #2
0
        public async Task <CampaignDto> CreateAsync(CreateUpdateCampaignDto input)
        {
            List <Group> groups = new List <Group>();

            //var group = _groupRepository.GetAsync();
            if (input.GroupId.Count > 0)
            {
                foreach (Guid groupDto in input.GroupId)
                {
                    var group = await _groupRepository.GetAsync(groupDto);

                    groups.Add(group);
                }
            }
            var campaign = await _campaignManager.CreateAsync(
                input.Name,
                input.Description,
                input.Schedule,
                input.Title,
                input.Content,
                groups
                );

            await _campaignRepository.InsertAsync(campaign);

            var emails = await _emailRepository.GetListAsync();


            foreach (Group group in campaign.Groups)
            {
                var contacts = await _contactRepository.GetListAsync();

                var listContacts = new List <Contact>();

                /*contacts.ForEach( async (c) =>
                 * {
                 *  await _contactRepository.EnsureCollectionLoadedAsync(c, x => x.ContactGroups);
                 * });*/
                await _groupRepository.EnsureCollectionLoadedAsync(group, x => x.ContactGroups);

                foreach (var cId in group.ContactGroups)
                {
                    listContacts.Add(cId.Contact);
                }
                //contacts = contacts.Where(c => c.Id.Equals(group.ContactGroups.)).ToList();
                foreach (Contact c in listContacts)
                {
                    var email = emails.OrderBy(e => e.Order).FirstOrDefault();
                    //AnotherEmailService service = new AnotherEmailService();
                    //await _emailService.SendEmailAsync(email.EmailString, c.Email, input.Content, input.Title);
                    //await _anotherEmailService.SendEmailAsync(campaign.Name, email.EmailString, c.Email, campaign.Title, campaign.Content, email.EmailString, email.Password);
                    await _backgroundJobManager.EnqueueAsync(
                        new EmailSetting
                    {
                        Host        = "smtp.gmail.com",
                        Port        = 587,
                        Mail        = email.EmailString,
                        Password    = email.Password,
                        To          = c.Email,
                        Subject     = campaign.Title,
                        Body        = campaign.Content,
                        DisplayName = campaign.Name
                    });

                    email.Order++;
                }
            }
            await _emailRepository.UpdateManyAsync(emails);

            return(ObjectMapper.Map <Campaign, CampaignDto>(campaign));
        }
예제 #3
0
 public async Task PrepareCollectedData()
 {
     await _backgroundJobManager.EnqueueAsync <UserCollectedDataPrepareJob, UserIdentifier>(AbpSession.ToUserIdentifier());
 }
예제 #4
0
 public async Task UpdateFans()
 {
     await _backgroundJobManager.EnqueueAsync <WechatGroupRefreshNowJob, bool>(false);
 }
예제 #5
0
        public async Task Test()
        {
            await _backgroundJobManager.EnqueueAsync(new JobQueue1Args());

            await _backgroundJobManager.EnqueueAsync(new JobQueue2Args());
        }
예제 #6
0
        public async Task Should_Store_Jobs()
        {
            var jobIdAsString = await _backgroundJobManager.EnqueueAsync(new MyJobArgs("42")).ConfigureAwait(false);

            jobIdAsString.ShouldNotBe(default);
예제 #7
0
 /// <summary>
 /// Enqueues a job to be executed.
 /// </summary>
 /// <typeparam name="TArgs">Type of the arguments of job.</typeparam>
 /// <param name="backgroundJobManager">Background job manager reference</param>
 /// <param name="args">Job arguments.</param>
 /// <param name="delay">Job delay (wait duration before first try).</param>
 public static string Enqueue <TArgs>(this IBackgroundJobManager backgroundJobManager, TArgs args, TimeSpan?delay = null)
 {
     return(AsyncHelper.RunSync(() => backgroundJobManager.EnqueueAsync(args, delay)));
 }
 public static Task EnqueueEventAsync <TEvent>(this IBackgroundJobManager backgroundJobManager,
                                               TEvent e, BackgroundJobPriority priority = BackgroundJobPriority.Normal,
                                               TimeSpan?delay = null) where TEvent : EventData
 {
     return(backgroundJobManager.EnqueueAsync <EventTriggerAsyncBackgroundJob <TEvent>, TEvent>(e, priority, delay));
 }
        public virtual async Task PublishAsync(
            string notificationName,
            NotificationData data             = null,
            EntityIdentifier entityIdentifier = null,
            NotificationSeverity severity     = NotificationSeverity.Info,
            UserIdentifier[] userIds          = null,
            UserIdentifier[] excludedUserIds  = null,
            long?[] tenantIds = null)
        {
            if (notificationName.IsNullOrEmpty())
            {
                throw new ArgumentException("NotificationName can not be null or whitespace!", nameof(notificationName));
            }

            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 notificationInfo = new NotificationInfo(_guidGenerator.Create())
            {
                NotificationName = notificationName,
                EntityTypeName   = entityIdentifier?.Type.FullName,
                EntityTypeAssemblyQualifiedName = entityIdentifier?.Type.AssemblyQualifiedName,
                EntityId        = 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       = GetTenantIdsAsStr(tenantIds),
                Data            = data?.ToJsonString(),
                DataTypeName    = 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
                foreach (var notificationDistributorType in _notificationConfiguration.Distributers)
                {
                    using (var notificationDistributer = _iocResolver.ResolveAsDisposable <INotificationDistributer>(notificationDistributorType))
                    {
                        await notificationDistributer.Object.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
                        )
                    );
            }
        }
        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
                        )
                    );
            }
        }