Example #1
0
        private void Distribute(BLQueue.Queue queue, List <Envelope> envelopes)
        {
            ConnectionFactory factory = new ConnectionFactory();

            factory.Uri = appsettings.CloudQueue.MqUrl;

            using (IConnection conn = factory.CreateConnection())
            {
                try
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        try
                        {
                            channel.ExchangeDeclare(appsettings.CloudQueue.MqExchange, ExchangeType.Direct, true);
                            var deliveryDetails = new DeliveryDetails
                            {
                                RabbitMqChannel = channel,
                                ExchangeName    = appsettings.CloudQueue.MqExchange
                            };

                            envelopeDistributor.Distribute(envelopes, queue, deliveryDetails, jobLogs);
                        }
                        finally
                        {
                            channel.Close();
                        }
                    }
                }
                finally
                {
                    conn.Close();
                }
            }
        }
Example #2
0
        private List <BLAiring.Airing> ValidateDeletedAirings(BLQueue.Queue queue, List <BLAiring.Airing> airings)
        {
            var validAirings = new List <BLAiring.Airing>();

            if (!airings.Any())
            {
                return(validAirings);
            }

            var invalidAirings = new List <string>();

            foreach (var airing in airings)
            {
                //Delete message will be delivered only when there is any modified message delivered for the same queue.
                if (messageDeliveryValidator.Validate(airing, queue.Name))
                {
                    validAirings.Add(airing);
                }
                else
                {
                    LogInformation(string.Format("Validation error. {0} - No 'Modify' action message delivered to the queue", airing.AssetId));
                    invalidAirings.Add(airing.AssetId);
                }
            }


            if (invalidAirings.Any())
            {
                UpdateIgnoreQueues(queue.Name, invalidAirings, true);
            }

            return(validAirings);
        }
Example #3
0
        public List <Envelope> Generate(IList <BLAiring.Airing> airings, BLQueue.Queue queue, Action action)
        {
            var packager = new QueuePackager();

            var envelopes = new List <Envelope>();

            foreach (var airing in airings)
            {
                var notifications = airing.ChangeNotifications.Where(e => e.QueueName == queue.Name).ToList();

                var envelope = new Envelope
                {
                    AiringId           = airing.AssetId,
                    PostMarkedDateTime = airing.ReleaseOn,
                    MessagePriority    = _priorityCalculator.Calculate(queue, airing),
                    Message            = packager.Package(airing, action, notifications),
                    MediaId            = airing.MediaId
                };



                envelopes.Add(envelope);
            }

            return(envelopes);
        }
Example #4
0
        private List <BLAiring.Airing> GetCurrentAirings(BLQueue.Queue queue, int limit)
        {
            var currentAirings = new List <BLAiring.Airing>();

            currentAirings.AddRange(airingService.GetBy(queue.Query, queue.HoursOut, new[] { queue.Name }).ToList());
            currentAirings.AddRange(airingService.GetDeliverToBy(queue.Name, limit).ToList());
            return(currentAirings.Distinct(new AiringComparer()).ToList());
        }
Example #5
0
        private List <BLAiring.Airing> GetDeletedAirings(BLQueue.Queue queue, int limit)
        {
            var deletedAirings = new List <BLAiring.Airing>();

            deletedAirings.AddRange(airingService.GetBy(queue.Query, queue.HoursOut, new[] { queue.Name }, true, AiringCollection.DeletedCollection).ToList());
            deletedAirings.AddRange(airingService.GetDeliverToBy(queue.Name, limit, AiringCollection.DeletedCollection).ToList());
            return(deletedAirings.Distinct(new AiringComparer()).ToList());
        }
Example #6
0
 public void BimReport(Model.Queue queue, string airingId, bool isActiveAiringStatus, string message, int statusEnum)
 {
     // If the queue requested to see statuses in digital fulfillment, proceed with
     // reporting to digital fulfillment
     if (queue.Report)
     {
         // Airing destination is defaulted to 18 (NONE) as defined in digital fulfillment
         _command.BimReport(airingId, isActiveAiringStatus, statusEnum, 18, message);
     }
 }
Example #7
0
        /// <summary>
        /// Defaults RoutingKey and Queue Name if it not exists
        /// </summary>
        /// <param name="queue">the queue to reset</param>
        private void DefaultQueueKeyFields(Model.Queue queue)
        {
            if (string.IsNullOrWhiteSpace(queue.RoutingKey))
            {
                queue.RoutingKey = Guid.NewGuid().ToString();
            }

            if (string.IsNullOrWhiteSpace(queue.Name))
            {
                queue.Name = Guid.NewGuid().ToString();
            }
        }
Example #8
0
        public void Create(BLQueue.Queue queue, bool prioritySelectionChanged = false)
        {
            if (prioritySelectionChanged)
            {
                Delete(queue.Name);
            }

            using (var advancedBus = RabbitHutch.CreateBus(_connectionString).Advanced)
            {
                var newQueue = queue.IsPriorityQueue ? advancedBus.QueueDeclare(queue.Name, maxPriority: 10) : advancedBus.QueueDeclare(queue.Name);

                var exchange = advancedBus.ExchangeDeclare(_exchangeName, ExchangeType.Topic, true);

                advancedBus.Bind(exchange, newQueue, queue.RoutingKey);
            }
        }
Example #9
0
        private List <BLAiring.Airing> ValidateAirings(BLQueue.Queue queue, IEnumerable <BLAiring.Airing> airings)
        {
            var validators = LoadValidators(queue);

            var ignoreAirings = new List <string>();

            if (!validators.Any())
            {
                return(airings.ToList());
            }

            var validator    = new Validating.AiringValidator(validators);
            var validAirings = new List <BLAiring.Airing>();

            foreach (var airing in airings)
            {
                var results = validator.Validate(airing, queue.Name);
                if (results.All(r => r.Valid))
                {
                    validAirings.Add(airing);
                }
                else if (results.Any(r => r.IgnoreQueue))
                {
                    ignoreAirings.Add(airing.AssetId);
                }

                foreach (var result in results.Where(r => !r.Valid && (r.StatusEnum != BIMNOTFOUND && r.StatusEnum != BIMMISMATCH)))
                {
                    // Append additional useful information
                    string message = "Queue validation error. ";
                    message += result.Message;
                    LogInformation(string.Format("Validation error. {0} - {1}", airing.AssetId, result.Message));
                    reportStatusCommand.Report(queue, airing.AssetId, true, message, result.StatusEnum, true);
                }
                if (queue.BimRequired)
                {
                    SendBIMStatus(results, airing, queue);
                }
            }

            if (ignoreAirings.Any())
            {
                UpdateIgnoreQueues(queue.Name, ignoreAirings);
            }

            return(validAirings);
        }
Example #10
0
        public byte?Calculate(BLQueue.Queue queue, BLAiring.Airing airing)
        {
            if (!queue.IsPriorityQueue)
            {
                return(null);
            }

            if (airing.Flights.All(e => e.End < DateTime.UtcNow))
            {
                return(0);
            }

            var firstAiringStartDate = airing.Flights.Select(e => e.Start).OrderBy(date => date).First();

            var differenceBetweenDates = (int)(firstAiringStartDate.Date - DateTime.UtcNow.Date).TotalDays;

            //Flight window already started
            if (differenceBetweenDates < 0)
            {
                return(7);
            }
            if (differenceBetweenDates == 0) //Flight windows started today
            {
                return(6);
            }
            if (differenceBetweenDates == 1) //Flight Starts tomorrow
            {
                return(5);
            }
            if (differenceBetweenDates <= 3) //Flight starts with in next two days
            {
                return(4);
            }
            if (differenceBetweenDates <= 7) //Flight starts with in a week
            {
                return(3);
            }

            if (differenceBetweenDates <= 14)
            {
                return(2);
            }

            return(1);
        }
Example #11
0
        /// <summary>
        /// Save's the queue
        /// </summary>
        /// <param name="queue">queue model to save</param>
        /// <returns>Returns saved queue</returns>
        public Model.Queue SaveQueue(Model.Queue queue)
        {
            DefaultQueueKeyFields(queue);

            DLModel.Queue dataModel = queue.ToDataModel <Model.Queue, DLModel.Queue>();

            var prioritySelectionChanged     = false;
            var versionSelectionChanged      = false;
            var prohibitResendMediaIdChanged = false;

            if (!string.IsNullOrEmpty(dataModel.Name))
            {
                var existingQueueSettings = queueQueryHelper.GetByApiKey(dataModel.Name);

                if (existingQueueSettings != null)
                {
                    prioritySelectionChanged     = existingQueueSettings.IsPriorityQueue != dataModel.IsPriorityQueue;
                    versionSelectionChanged      = !existingQueueSettings.AllowAiringsWithNoVersion && dataModel.AllowAiringsWithNoVersion;
                    prohibitResendMediaIdChanged = existingQueueSettings.IsProhibitResendMediaId &&
                                                   !dataModel.IsProhibitResendMediaId;
                }
            }

            dataModel = queueSaveCommand.Save(dataModel);

            remoteQueueHandler.Create(queue, prioritySelectionChanged);

            if (dataModel.Active)
            {
                hangfireCommand.CreatePublisherJob(dataModel.Name);
            }
            else
            {
                hangfireCommand.DeletePublisherJob(dataModel.Name);
            }

            if (versionSelectionChanged || prohibitResendMediaIdChanged)
            {
                currentAiringQuery.DeleteIgnoredQueue(dataModel.Name);
            }

            return(dataModel.ToBusinessModel <DLModel.Queue, Model.Queue>());
        }
Example #12
0
        /// <summary>
        /// Processes the queue.
        /// </summary>
        /// <param name="queue">The queue.</param>
        private void ProcessQueue(BLQueue.Queue queue)
        {
            LogInformation("Retrieving current airings that should be send to queue");
            var currentAirings = GetCurrentAirings(queue, Limit);

            LogInformation(string.Format("Successfully retrieved {0} current airings", currentAirings.Count));


            LogInformation("Retrieving deleted airings that should be send to queue");
            var deletedAirings = GetDeletedAirings(queue, Limit);

            LogInformation(string.Format("Successfully retrieved {0} deleted airings", deletedAirings.Count));


            LogInformation("Applying validation on all current airings based on queue settings");
            var validAirings = ValidateAirings(queue, currentAirings);

            LogInformation(string.Format("Completed validation on all {0} current airings, resulting in a total of {1} valid current airings", currentAirings.Count, validAirings.Count));


            LogInformation("Applying validation on all deleted airings");

            var validDeletedAirings = ValidateDeletedAirings(queue, deletedAirings);

            LogInformation(string.Format("Completed validation on all {0} deleted airings, resulting in a total of {1} valid deleted airings", deletedAirings.Count, validDeletedAirings.Count));


            if (!(validAirings.Any() || validDeletedAirings.Any()))
            {
                LogInformation("No Airings found to distribute");
                return;
            }

            LogInformation("Preparing to distribute current and deleted airings to queue");

            var envelopes = envelopeStuffer.Generate(validAirings, queue, Workflow.Action.Modify);

            envelopes.AddRange(envelopeStuffer.Generate(validDeletedAirings, queue, Workflow.Action.Delete));
            Distribute(queue, envelopes);
            LogInformation("Successfully distributed current and deleted airings to queue");
        }
Example #13
0
        public void Distribute(IList <Envelope> envelopes, BLQueue.Queue deliveryQueue, DeliveryDetails details, StringBuilder logger)
        {
            foreach (var envelope in envelopes.OrderBy(e => e.PostMarkedDateTime))
            {
                if (IsAiringDistributed(envelope, deliveryQueue.Name))
                {
                    logger.AppendWithTime(string.Format("Airing {0} already delivered to the queue", envelope.AiringId));
                    continue;
                }

                var message = JsonConvert.SerializeObject(envelope.Message, Formatting.None,
                                                          new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });

                try
                {
                    Deliver(envelope, message, deliveryQueue.RoutingKey, details);

                    _queueService.AddHistoricalMessage(envelope.AiringId, envelope.MediaId, message, deliveryQueue.Name, envelope.MessagePriority);

                    _reporter.Report(deliveryQueue, envelope.AiringId, envelope.Message.Action != "Delete",
                                     string.Format("Sent successfully to {0} Message: {1}", deliveryQueue.FriendlyName, message),
                                     12);

                    UpdateDeliveredTo(envelope, deliveryQueue.Name);

                    logger.AppendWithTime(string.Format("Airing {0} successfully delivered to the queue", envelope.AiringId));
                }
                catch
                {
                    _reporter.Report(deliveryQueue, envelope.AiringId, envelope.Message.Action != "Delete",
                                     string.Format("Failed to send to {0} Message: {1}", deliveryQueue.FriendlyName, message), 4,
                                     true);

                    throw;
                }
            }
        }
Example #14
0
        private List <IAiringValidatorStep> LoadValidators(BLQueue.Queue queue)
        {
            var validators = new List <IAiringValidatorStep>();

            if (queue.BimRequired)
            {
                validators.Add(bimContentValidator);
            }

            // Verify if queue is configured to receive
            // airings without versions. If not, apply validation.
            if (!queue.AllowAiringsWithNoVersion)
            {
                validators.Add(new VersionValidator());
            }

            if (queue.IsProhibitResendMediaId)
            {
                validators.Add(mediaIdValidator);
            }

            return(validators);
        }
Example #15
0
        private void SendBIMStatus(IList <ValidationResult> results, BLAiring.Airing airing, BLQueue.Queue queue)
        {
            var bimFoundResult    = results.Where(r => r.Valid && r.StatusEnum == BIMFOUND).FirstOrDefault();
            var bimNotFoundResult = results.Where(r => !r.Valid && r.StatusEnum == BIMNOTFOUND).FirstOrDefault();
            var bimisMatch        = results.Where(r => !r.Valid && r.StatusEnum == BIMMISMATCH).FirstOrDefault();

            if (bimFoundResult != null)
            {
                // LogInformation(string.Format("BIM  Found. {0} - {1}", airing.AssetId, bimFoundResult.Message));
                reportStatusCommand.BimReport(queue, airing.AssetId, true, bimFoundResult.Message, bimFoundResult.StatusEnum);
            }
            if (bimNotFoundResult != null)
            {
                //  LogInformation(string.Format("BIM  Not Found. {0} - {1}", airing.AssetId, bimNotFoundResult.Message));
                reportStatusCommand.BimReport(queue, airing.AssetId, true, bimNotFoundResult.Message, bimNotFoundResult.StatusEnum);
            }

            if (bimisMatch != null)
            {
                //  LogInformation(string.Format("BIM  Mismatch. {0} - {1}", airing.AssetId, bimisMatch.Message));
                reportStatusCommand.BimReport(queue, airing.AssetId, true, bimisMatch.Message, bimisMatch.StatusEnum);
            }
        }
Example #16
0
 private void LogError(Exception exception, string message, BLQueue.Queue queue)
 {
     logger.Error(exception, string.Format("{0}. QueueName: {1}  Queue: {2}, Process Id: {3}", message, queue.Name, queue.FriendlyName, processId));
 }