protected override void afterRestarting(ISender sender) { var toRetry = Queued.Where(x => !x.IsExpired()).ToArray(); Queued.Clear(); foreach (var envelope in toRetry) { // It's perfectly okay to not wait on the task here _sender.Enqueue(envelope); } }
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 } }