private Envelope[] filterExpired(IDocumentSession session, IEnumerable <Envelope> outgoing) { var expiredMessages = outgoing.Where(x => x.IsExpired()).ToArray(); _logger.DiscardedExpired(expiredMessages); session.DeleteEnvelopes(_marker.Outgoing, expiredMessages); return(outgoing.Where(x => !x.IsExpired()).ToArray()); }
public override async Task EnqueueForRetry(OutgoingMessageBatch batch) { var expiredInQueue = Queued.Where(x => x.IsExpired()).ToArray(); var expiredInBatch = batch.Messages.Where(x => x.IsExpired()).ToArray(); try { using (var session = _store.LightweightSession()) { var expired = expiredInBatch.Concat(expiredInQueue).ToArray(); session.DeleteEnvelopes(_marker.Incoming, expired); var all = Queued.Where(x => !expiredInQueue.Contains(x)) .Concat(batch.Messages.Where(x => !expiredInBatch.Contains(x))) .ToList(); if (all.Count > _settings.MaximumEnvelopeRetryStorage) { var reassigned = all.Skip(_settings.MaximumEnvelopeRetryStorage).ToArray(); session.MarkOwnership(_marker.Incoming, TransportConstants.AnyNode, reassigned); } await session.SaveChangesAsync(); _logger.DiscardedExpired(expired); Queued = all.Take(_settings.MaximumEnvelopeRetryStorage).ToList(); } } catch (Exception e) { _logger.LogException(e, message: "Failed while trying to enqueue a message batch for retries"); #pragma warning disable 4014 Task.Delay(100).ContinueWith(async _ => await EnqueueForRetry(batch)); #pragma warning restore 4014 } }