Exemple #1
0
        public void HandleWithoutLibraryScenario(IBulkCampaignCreatedOrUpdated command)
        {
            var validateContext = CreateValidateContext(command);

            var campaignExternalIds = command.Data.Select(c => c.ExternalId).ToList();

            _campaignCleaner.ExecuteAsync(campaignExternalIds).GetAwaiter().GetResult();

            var campaigns = new List <CampaignDbObject>();

            foreach (var item in command.Data)
            {
                Validator.ValidateCampaign(item, validateContext);

                var newCampaign = _mapper.Map <CampaignDbObject>(item, opts => opts.UseEntityCache(_salesAreaByIdCache));
                AdjustCampaignPassPriority(newCampaign);
                newCampaign.UpdateDerivedKPIs();

                campaigns.Add(newCampaign);
            }

            using (var dbContext = _dbContextFactory.Create())
            {
                dbContext.AddRange(_mapper.Map <GamePlan.Persistence.SqlServer.Entities.Tenant.Campaigns.Campaign[]>(campaigns, opts => opts.UseEntityCache(_salesAreaByNameCache)));
                dbContext.SaveChanges();
            }
        }
        public async Task <IHttpActionResult> DeleteAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                return(BadRequest("Invalid Campaign Id"));
            }

            var campaign = _campaignRepository.Get(id);

            if (campaign == null)
            {
                return(NotFound());
            }

            await _campaignCleaner.ExecuteAsync(campaign.Id).ConfigureAwait(false);

            return(this.NoContent());
        }
        public override void Handle(IBulkCampaignDeleted command)
        {
            var allActiveExternalIds = _campaignRepository.GetAllActiveExternalIds();
            var externalIdsToDelete  = command.Data.Select(x => x.ExternalId).ToList();
            var inactiveExternalIds  = externalIdsToDelete
                                       .Except(allActiveExternalIds, StringComparer.OrdinalIgnoreCase).ToList();

            if (inactiveExternalIds.Count > 0)
            {
                _logger.Warn($"Active campaigns not found: {String.Join(", ", inactiveExternalIds)}");
            }
            else
            {
                Task.Run(async() => await _campaignCleaner.ExecuteAsync(externalIdsToDelete).ConfigureAwait(false))
                .GetAwaiter().GetResult();
            }
        }
Exemple #4
0
        public override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            if (Options.DaysAfter == 0)
            {
                _logger.LogDebug(
                    $"'{nameof(Options.DaysAfter)}' configuration parameters hasn't been specified, execution skipped.");
                return;
            }

            var thresholdDate = _clock.GetCurrentInstant().ToDateTimeUtc().AddDays(-Options.DaysAfter);

            var campaignIds = await _dbContext.Query <Campaign>().AsNoTracking()
                              .Where(x => x.EndDateTime < thresholdDate)
                              .Select(x => x.Id).ToArrayAsync(cancellationToken).ConfigureAwait(false);

            _logger.LogDebug(
                $"'{nameof(Options.DaysAfter)}' parameter is set to {Options.DaysAfter} days. {campaignIds.Length} campaign(s) returned which end date less than {thresholdDate}.");

            if (campaignIds.Length > 0)
            {
                var targetBlock = new BatchExecuteTargetBlock <Guid>(
                    ids => _campaignCleaner.ExecuteAsync(ids, cancellationToken), Options.Concurrency,
                    cancellationToken);

                foreach (var campaignId in campaignIds)
                {
                    _ = await targetBlock.SendAsync(campaignId, cancellationToken).ConfigureAwait(false);
                }

                targetBlock.Complete();

                await targetBlock.Completion.AggregateExceptions <DataPurgingException>(
                    "Purging of Campaign data has been failed. See inner exception for more details.");

                _logger.LogDebug(
                    $"Purging of {campaignIds.Length} campaign(s) has been completed successfully according to the '{nameof(Options.DaysAfter)}' parameter.");
            }
        }