Rhino.Queues.QueueManager.PurgeOutgoingHistory C# (CSharp) Method

PurgeOutgoingHistory() private method

private PurgeOutgoingHistory ( ) : void
return void
        private void PurgeOutgoingHistory()
        {
            // Outgoing messages are still stored in the history in case the sender 
            // needs to revert, so there will still be messages to purge even when
            // the QueueManagerConfiguration has disabled outgoing history.
            //
            // To make this batchable:
            // 1: Move to the end of the history (to the newest messages) and seek 
            //    backword by NumberOfMessagesToKeepInOutgoingHistory.
            // 2: Save a bookmark of the current position.
            // 3: Delete from the beginning of the table (oldest messages) in batches until 
            //    a) we reach the bookmark or b) we hit OldestMessageInOutgoingHistory.

            MessageBookmark purgeLimit = null;
            int numberOfMessagesToKeep = Configuration.NumberOfMessagesToKeepInOutgoingHistory;
            if (numberOfMessagesToKeep > 0 && Configuration.EnableOutgoingMessageHistory)
            {
                queueStorage.Global(actions =>
                {
                    purgeLimit = actions.GetSentMessageBookmarkAtPosition(numberOfMessagesToKeep);
                    actions.Commit();
                });

                if (purgeLimit == null)
                    return;
            }

            bool foundMessages = false;
            do
            {
                foundMessages = false;
                queueStorage.Global(actions =>
                {
                    IEnumerable<PersistentMessageToSend> sentMessages = actions.GetSentMessages(batchSize: 250)
                        .TakeWhile(x => (purgeLimit == null || !x.Bookmark.Equals(purgeLimit))
                            && (!Configuration.EnableOutgoingMessageHistory || (DateTime.Now - x.SentAt) > Configuration.OldestMessageInOutgoingHistory));

                    foreach (var sentMessage in sentMessages)
                    {
                        foundMessages = true;
                        logger.DebugFormat("Purging sent message {0} to {1}/{2}/{3}", sentMessage.Id, sentMessage.Endpoint,
                                           sentMessage.Queue, sentMessage.SubQueue);
                        actions.DeleteMessageToSendHistoric(sentMessage.Bookmark);
                    }

                    actions.Commit();
                });
            } while (foundMessages);
        }