Exemplo n.º 1
0
 public static IQueryable<int> GetDeliveryServerIdsForMonitoredDeliveryGroups(IPmtaRepository pmtaRepository, IDeliveryGroupRepository deliveryGroupRepository)
 {
     return (from dsi in pmtaRepository.GetDeliveryServerIps()
             where dsi.deliveryserver_id.HasValue && dsi.deliverygroup_id.HasValue && dsi.enabled && dsi.DeliveryServer.enabled
             join dvg in DeliveryGroup.GetMonitored(deliveryGroupRepository).Where(d => d.CancelOnBulkingEnabled) on dsi.deliverygroup_id equals dvg.deliverygroup_id
             select dsi.deliveryserver_id.Value).Distinct();
 }
Exemplo n.º 2
0
 public static IQueryable<Pmta> GetPmtasByDeliveryGroup(IPmtaRepository pmtaRepository, DeliveryGroup deliveryGroup)
 {
     return (from dsi in pmtaRepository.GetDeliveryServerIps()
             where dsi.deliveryserver_id.HasValue && dsi.deliverygroup_id.HasValue && dsi.deliverygroup_id == deliveryGroup.deliverygroup_id
             join pmta in pmtaRepository.GetAll() on dsi.deliveryserver_id equals pmta.deliveryserver_id
             select pmta).Distinct();
 }
Exemplo n.º 3
0
        public static IQueryable<IGrouping<int, DeliveryServerIp>> GetVmtasGroupedByDeliveryServerForDeliveryGroup(IPmtaRepository pmtaRepository, DeliveryGroup deliveryGroup)
        {

            return from dsi in pmtaRepository.GetDeliveryServerIps()
                   where dsi.deliveryserver_id.HasValue && dsi.deliverygroup_id.HasValue && dsi.deliverygroup_id == deliveryGroup.deliverygroup_id && dsi.enabled && dsi.DeliveryServer.enabled
                   group dsi by dsi.deliveryserver_id.Value into g
                   select g;
        }
Exemplo n.º 4
0
        public void FindAndCancelBulkingCampaigns(
            IPmtaRepository pmtaRepository,
            IDeliveryGroupRepository deliveryGroupRepository,
            IHeatDataRepository heatDataRepository,
            int heatDataLookBackMinutes,
            int minimumRecords,
            int minimumInboxing,
            DateTime nextReset
            )
        {
            _logger.Info("Getting Heat Data");
            var inboxing = heatDataRepository.GetCampaignInboxingStatistics(heatDataLookBackMinutes).ToArray();

            _logger.Info("Getting DeliveryGroups");
            var monitoredDeliveryGroups = DeliveryGroup.GetMonitored(deliveryGroupRepository).ToArray();

            _logger.Info("Verifying Pmtas for Monitored DeliverGroups");
            var deliveryServerIds = Pmta.GetDeliveryServerIdsForMonitoredDeliveryGroups(pmtaRepository, deliveryGroupRepository);
            var pmtas = pmtaRepository.GetAll();
            var missingPmtas = deliveryServerIds.Where(d => !pmtas.Any(p => p.deliveryserver_id == d));

            if (missingPmtas.Any())
            {
                _logger.WarnFormat("Missing Pmtas for DeliveryServers {0}", string.Join(", ", missingPmtas));
                return;
            }
            
            foreach (var deliveryGroup in monitoredDeliveryGroups)
            {
                try
                {
                    var campaigns = inboxing.Where(c => c.Records >= minimumRecords && c.DeliveryGroups.Any(d => d.deliverygroup_id == deliveryGroup.deliverygroup_id)).ToArray();

                    if (campaigns.Any())
                    {
                        var bulkingCampaignsStats = campaigns.Where(c => c.InboxingRate <= minimumInboxing).ToArray();

                        if (bulkingCampaignsStats.Count() == campaigns.Count())
                        {
                            var dblogEvent = new Event()
                            {
                                EventName = Event.EventNames.AllBulked,
                                DeliveryGroup = deliveryGroup,
                                Monitor = Event.Monitors.CancelOnBulking,
                                InboxingRateSetting = minimumInboxing
                            };

                            bulkingCampaignsStats.Select(c => new EventCampaign() { Campaign = c.Campaign, InboxingRate = c.InboxingRate })
                                .ToList().ForEach(c => dblogEvent.EventCampaigns.Add(c));

                            _eventRepository.Add(dblogEvent);
                            _logger.InfoFormat("Starting AllBulked on {0}", deliveryGroup.deliverygroup_id);
                            _unitOfWork.SaveChanges();

                            PausePurgeAndCancelByDeliveryGroup(pmtaRepository, nextReset, deliveryGroup, dblogEvent);

                            _emailNotification.SendEvent(dblogEvent);
                        }
                        else if (bulkingCampaignsStats.Any())
                        {
                            var dblogEvent = new Event()
                            {
                                EventName = Event.EventNames.SomeBulked,
                                DeliveryGroup = deliveryGroup,
                                Monitor = Event.Monitors.CancelOnBulking,
                                InboxingRateSetting = minimumInboxing
                            };

                            bulkingCampaignsStats.Select(c => new EventCampaign() { Campaign = c.Campaign, InboxingRate = c.InboxingRate })
                                .ToList().ForEach(c => dblogEvent.EventCampaigns.Add(c));

                            _eventRepository.Add(dblogEvent);
                            _logger.InfoFormat("Starting SomeBulked on {0}", deliveryGroup.deliverygroup_id);
                            _unitOfWork.SaveChanges();

                            foreach (var campaignStats in bulkingCampaignsStats)
                            {
                                if (campaignStats.Campaign.Template.creative_id.HasValue)
                                {
                                    PurgeAndCancelByCampaignContent(pmtaRepository, nextReset, campaignStats, deliveryGroupRepository, dblogEvent);
                                }
                            }
                            _emailNotification.SendEvent(dblogEvent);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.ErrorFormat("DVG {0}: {1}", deliveryGroup.deliverygroup_id, ex.UnwrapForLog(true));
                }
            }
        }
Exemplo n.º 5
0
        private void PausePurgeAndCancelByDeliveryGroup(IPmtaRepository pmtaRepository, DateTime nextReset, DeliveryGroup deliveryGroup, Event dblogEvent)
        {
            foreach (var deliveryServer in Pmta.GetVmtasGroupedByDeliveryServerForDeliveryGroup(pmtaRepository, deliveryGroup))
            {
                try
                {
                    var pmta = pmtaRepository.GetAll().FirstOrDefault(p => p.deliveryserver_id == deliveryServer.Key);

                    if (pmta != null)
                    {
                        var queues = _mtaAgent.GetHotmailQueuesByVmta(pmta.ToMta(), deliveryServer.Select(d => d.VmtaUrl));

                        foreach (var queue in queues)
                        {
                            try
                            {
                                _mtaAgent.Pause(pmta.ToMta(), queue);

                                _logger.DebugFormat("Paused Queue {0} on {1}", queue, pmta.Host);

                                dblogEvent.EventActions.Add(new EventAction()
                                {
                                    Action = EventAction.Actions.MTAPause,
                                    ActedOnDeliveryGroupId = deliveryGroup.deliverygroup_id,
                                    Pmta = pmta,
                                    PmtaQueue = queue
                                });

                                _mtaAgent.Purge(pmta.ToMta(), queue);

                                _logger.DebugFormat("Deleted Queue {0} on {1}", queue, pmta.Host);

                                dblogEvent.EventActions.Add(new EventAction()
                                {
                                    Action = EventAction.Actions.MTAPurge,
                                    ActedOnDeliveryGroupId = deliveryGroup.deliverygroup_id,
                                    Pmta = pmta,
                                    PmtaQueue = queue
                                });
                            }
                            catch (Exception ex)
                            {
                                _logger.ErrorFormat("Pmta {0}: {1}", pmta.PmtaId, ex.UnwrapForLog(true));
                            }
                        }

                    }
                    else
                    {
                        _logger.ErrorFormat("Missing MTA credentials for {0}", deliveryServer.Key);
                    }
                }
                catch (Exception ex)
                {
                    _logger.ErrorFormat("DeliveryGroup {0} DeliveryServer {1}: {2}", deliveryGroup.deliverygroup_id, deliveryServer.Key, ex.UnwrapForLog(true));
                }
            }

            _unitOfWork.SaveChanges();

            DeliveryGroup.CancelHotmailJobsByDeliveryGroup(_jobRepository, _logger, deliveryGroup, nextReset, dblogEvent);

            _unitOfWork.SaveChanges();

        }
Exemplo n.º 6
0
        private void PurgeAndCancelByCampaignContent(IPmtaRepository pmtaRepository, DateTime nextReset, CampaignInboxingStatistics campaignStats, IDeliveryGroupRepository deliveryGroupRepository, Event dblogEvent)
        {
            var jobs = Job.GetHotmailRelatedJobsSent(_jobRepository, campaignStats.Campaign, nextReset).ToArray();
            var dvgs = (from d in DeliveryGroup.GetMonitored(deliveryGroupRepository).ToArray()
                        where jobs.Select(j => j.deliverygroup_id.Value)
                                  .Distinct()
                                  .Contains(d.deliverygroup_id)
                        select new { Id = d.deliverygroup_id, Pmtas = Pmta.GetPmtasByDeliveryGroup(pmtaRepository, d).ToArray() }).ToArray();
            var mtaEventDetails = new List<EventAction>();

            var mtaJobDeleteTask = new Task(() =>
                              {
                                  var commandsSent = 0;
                                  _logger.DebugFormat("Deleting Related Jobs from MTAs");
                                  foreach (var job in jobs)
                                  {
                                      try
                                      {
                                          var pmtas = dvgs.FirstOrDefault(d => d.Id == job.deliverygroup_id.Value).Pmtas;

                                          if (pmtas.Any())
                                          {
                                              foreach (var pmta in pmtas)
                                              {
                                                  _mtaAgent.DeleteJob(pmta.ToMta(), job.job_id);
                                                  commandsSent++;
                                              }
                                          }
                                          else
                                          {
                                              _logger.TraceFormat("No DeliveryServers found for DeliveryGroup {0}", job.deliverygroup_id);
                                          }

                                          mtaEventDetails.Add(new EventAction()
                                          {
                                              Action = EventAction.Actions.MTAJobDelete,
                                              ActedOnDeliveryGroupId = job.deliverygroup_id,
                                              JobId = job.job_id
                                          });
                                      }
                                      catch (Exception ex)
                                      {
                                          _logger.ErrorFormat("Job {0}: {1}", job.job_id, ex.UnwrapForLog(true));
                                      }
                                  }
                                  _logger.DebugFormat("Sent {0} delete commands for {1} Jobs to MTAs", commandsSent, jobs.Count());
                              });

            try
            {
                mtaJobDeleteTask.Start();

                try
                {
                    _logger.DebugFormat("Cancelling Related Jobs");
                    Job.CancelHotmailRelatedJobs(_jobRepository, _logger, campaignStats.Campaign, nextReset, dblogEvent);
                }
                catch (Exception ex)
                {
                    _logger.ErrorFormat("Campaign {0}: {1}", campaignStats.Campaign.campaign_id, ex.UnwrapForLog(true));
                }

                mtaJobDeleteTask.Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var innerException in ex.InnerExceptions)
                {
                    _logger.Error(innerException.UnwrapForLog(false));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex.UnwrapForLog());
            }

            mtaEventDetails.ForEach(e => dblogEvent.EventActions.Add(e));

            _unitOfWork.SaveChanges();
        }
Exemplo n.º 7
0
 public static IQueryable<Pmta> GetMonitored(IPmtaRepository pmtaRepository)
 {
     return pmtaRepository.GetAll().Where(p => p.IsActive);
 }