예제 #1
0
 public void Execute(Action handler, IMessageAcceptor ma, CancellationToken cancellationToken)
 {
     try
     {
         handler();
         ma.Accept();
     }
     catch (Exception ex)
     {
         // ReSharper disable once MethodSupportsCancellation
         _log.WriteWarningAsync(
             nameof(ResilientErrorHandlingStrategy),
             _settings.GetSubscriberName(),
             "Message handling",
             $"Failed to handle the message. Send it to poison queue {_settings.QueueName}-poison. Exception {ex}")
         .GetAwaiter().GetResult();
         ma.Reject();
     }
 }
 public void Execute(Action handler, IMessageAcceptor ma, CancellationToken cancellationToken)
 {
     try
     {
         handler();
         ma.Accept();
     }
     catch (Exception ex)
     {
         // ReSharper disable once MethodSupportsCancellation
         _log.WriteErrorAsync(_settings.GetSubscriberName(), "Message handling", _settings.GetSubscriberName(), ex).GetAwaiter().GetResult();
         if (_next == null)
         {
             ma.Accept();
         }
         else
         {
             _next.Execute(handler, ma, cancellationToken);
         }
     }
 }
        public void Execute(Action handler, IMessageAcceptor ma, CancellationToken cancellationToken)
        {
            var isAllAttemptsFailed = false;

            try
            {
                handler();
            }
            catch (Exception ex)
            {
                // Message processing is failed, entering to the retries

                // ReSharper disable once MethodSupportsCancellation
                _log.WriteWarningAsync(
                    nameof(ResilientErrorHandlingStrategy),
                    _settings.GetSubscriberName(),
                    "Message handling",
                    $"Failed to handle the message for the first time. Retry in {_retryTimeout.Seconds} sec. Exception {ex}")
                .GetAwaiter().GetResult();

                // Retries loop

                for (int i = 0; i < _retryNum; i++)
                {
                    // Adding delay between attempts

                    // ReSharper disable once MethodSupportsCancellation
                    Task.Delay(_retryTimeout, cancellationToken).GetAwaiter().GetResult();

                    try
                    {
                        handler();

                        // The message was processed after all, so no more retries needed

                        return;
                    }
                    catch (Exception ex2)
                    {
                        // ReSharper disable once MethodSupportsCancellation
                        _log.WriteWarningAsync(
                            nameof(ResilientErrorHandlingStrategy),
                            _settings.GetSubscriberName(),
                            "Message handling",
                            $"Failed to handle the message for the {i + 1} time. Retry in {_retryTimeout.Seconds} sec. Exception {ex2}")
                        .GetAwaiter().GetResult();
                    }
                }

                // All attempts is failed, need to call next strategy in the chain

                isAllAttemptsFailed = true;

                if (_next == null)
                {
                    // Swallow the message if no more strategy is chained

                    ma.Accept();
                }
                else
                {
                    _next.Execute(handler, ma, cancellationToken);
                }
            }
            finally
            {
                // Finally, the message should be accepted, if it was successfully processed

                if (!isAllAttemptsFailed)
                {
                    ma.Accept();
                }
            }
        }