Exemplo n.º 1
0
        /// <summary>
        /// Writes all messages currently in the queue to MongoDb and acknowledges
        /// </summary>
        protected override void ProcessQueue()
        {
            // Will happen when ProcessQueue is called due to the timer, before we receive our first message
            if (Model == null)
            {
                return;
            }

            lock (LockObj)
            {
                if (ToProcess.Count == 0)
                {
                    return;
                }

                Logger.Info($"Queue contains {ToProcess.Count} message to write");

                foreach ((string modality, List <BsonDocument> modalityDocs) in
                         MongoModalityGroups.GetModalityChunks(ToProcess.Select(x => x.Item1).ToList()))
                {
                    Logger.Debug($"Attempting to write {modalityDocs.Count} documents of modality {modality}");

                    while (FailedWriteAttempts < FailedWriteLimit)
                    {
                        WriteResult imageWriteResult = MongoDbAdapter.WriteMany(modalityDocs, modality);

                        if (imageWriteResult == WriteResult.Success)
                        {
                            Logger.Debug($"Wrote {modalityDocs.Count} documents successfully, sending ACKs");

                            // Hopefully this uses ReferenceEquals, otherwise will be slow...
                            foreach (ulong deliveryTag in ToProcess
                                     .Where(x => modalityDocs.Contains(x.Item1))
                                     .Select(x => x.Item2))
                            {
                                Model.BasicAck(deliveryTag, false);
                            }

                            AckCount           += modalityDocs.Count;
                            FailedWriteAttempts = 0;
                            break;
                        }

                        Logger.Warn($"Failed to write {FailedWriteAttempts + 1} time(s) in a row");

                        if (++FailedWriteAttempts < FailedWriteLimit)
                        {
                            continue;
                        }

                        throw new ApplicationException("Failed write attempts exceeded");
                    }
                }

                Logger.Debug("Wrote and acknowledged all documents in queue. Clearing and continutig");
                ToProcess.Clear();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes all messages currently in the queue to MongoDb and acknowledges
        /// </summary>
        protected override void ProcessQueue()
        {
            // Will happen when ProcessQueue is called before we receive our first message
            if (Model == null)
            {
                return;
            }

            lock (LockObj)
            {
                if (ToProcess.Count == 0)
                {
                    return;
                }

                Logger.Debug("SeriesMessageProcessor: Queue contains " + ToProcess.Count + " message to write");

                IEnumerable <string> batchDirectories = ToProcess.Select(t => t.Item1.GetValue("header")["DirectoryPath"].AsString).Distinct();
                Logger.Trace($"Writing series from directories: {string.Join(", ", batchDirectories)}");

                WriteResult seriesWriteResult = MongoDbAdapter.WriteMany(ToProcess.Select(t => t.Item1).ToList());

                // Result => Need to differentiate between connection loss and error in the data to be written
                // As well as making sure either all are written or none

                if (seriesWriteResult == WriteResult.Success)
                {
                    Logger.Debug("SeriesMessageProcessor: Wrote " + ToProcess.Count + " messages successfully, sending ACKs");

                    foreach (ulong deliveryTag in ToProcess.Select(t => t.Item2))
                    {
                        Model.BasicAck(deliveryTag, false);
                    }

                    AckCount += ToProcess.Count;
                    ToProcess.Clear();
                    FailedWriteAttempts = 0;
                }
                else
                {
                    Logger.Warn($"SeriesMessageProcessor: Failed to write {FailedWriteAttempts + 1} time(s) in a row");

                    if (++FailedWriteAttempts < FailedWriteLimit)
                    {
                        return;
                    }

                    throw new ApplicationException("Failed write attempts exceeded");
                }
            }
        }