private void AsyncFailure(IMessage request, RC.IModel channel, Exception exception) { _logger?.LogError(exception, "Async method was completed with an exception for {request} ", request); try { channel.BasicNack(request.Headers.DeliveryTag().Value, false, ContainerUtils.ShouldRequeue(DefaultRequeueRejected, exception, _logger)); } catch (Exception e) { _logger?.LogError(e, "Failed to nack message"); } }
public void ProcessMessage(object sender, BasicDeliverEventArgs e) { var mi = MessageInfo(e); try { _logger.LogInformation($@"Got message {mi}"); TSrc src; using (var ms = new MemoryStream(e.Body)) { src = ProtoBuf.Serializer.Deserialize <TSrc>(ms); _logger.LogInformation($@"Message {mi} JSON view: {JsonConvert.SerializeObject(src, Formatting.Indented)}"); } var dst = _transformer.Transform(src); _writer.WriteAsync(dst).Wait(); _channel.BasicAck(e.DeliveryTag, false); OnProcessed?.Invoke(this, new MessageProcessedEventArgs <TSrc>(src)); _logger.LogInformation($"Message processed {mi}"); } catch (Exception ex) { _logger.LogError($@"Error message process {mi} Requeue after {_rabbitConfig.RequeueOnErrorAfter} Error {ex.Message}. {ex}"); OnProcessError?.Invoke(this, new MessageProcessErrorEventArgs(ex)); Task.Run(async() => { await Task.Delay(_rabbitConfig.RequeueOnErrorAfter); _channel?.BasicNack(e.DeliveryTag, false, true); _logger.LogInformation($@"Reqeue message {mi}"); }); } }