public override async Task StopAsync(CancellationToken cancellationToken) { if (_executingTask == null) { return; } _cancellationTokenSource.Cancel(); await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken)).ConfigureAwait(false); var channel = _messagingFactory.Configure(); channel.BasicCancel(_tag); _messagingFactory.Disconnect(); }
public AsyncEventHandler <BasicDeliverEventArgs> Dequeue(Func <string, T, Task> callback) { if (callback == null) { throw new ArgumentNullException(nameof(callback)); } var channel = _messagingFactory.Configure(); return(async(model, ea) => { _logger.LogDebug("MESSAGING | RECEIVING NEW MESSAGE"); var body = ea.Body; var raw = Encoding.UTF8.GetString(body); var retries = 0; _logger.LogDebug($"MESSAGING | RAW MESSAGE: { raw }"); try { retries = Retries(ea); _logger.LogDebug($"MESSAGING | THIS MESSAGE HAS BEEN PROCESSED { retries } TIMES"); var message = JsonConvert.DeserializeObject <T>(raw); if (_messaging.Retries > retries) { _logger.LogDebug($"MESSAGING | MESSAGE WAS SUCCESSFULLY DESERIALIZE, INVOKING ORCHESTRATION"); await callback.Invoke(raw, message); } else { _logger.LogDebug($"MESSAGING | MESSAGE WAS PROCESSED TOO MANY TIMES, PUSHING TO ERROR QUEUE"); var headers = new Dictionary <string, object> { { "queue", _messaging.Consuming.Queue } }; Queue(_messaging.Error.Exchange, _messaging.Error.Routingkey, raw, headers); } _logger.LogDebug($"MESSAGING | MESSAGE HAS BEEN ACKED"); channel.BasicAck(ea.DeliveryTag, false); } catch (Exception ex) { _logger.LogDebug($"MESSAGING | SOMETHING HAPPENED WHEN PROCESSING THE MESSAGE: { ex }"); if (_messaging.Retries > retries) { channel.BasicNack(ea.DeliveryTag, false, false); _logger.LogDebug($"MESSAGING | MESSAGE HAS BEEN NACKED"); } else { _logger.LogDebug($"MESSAGING | MESSAGE WAS PROCESSED TOO MANY TIMES, PUSHING TO ERROR QUEUE"); var headers = new Dictionary <string, object> { { "queue", _messaging.Consuming.Queue } }; Queue(_messaging.Error.Exchange, _messaging.Error.Routingkey, raw, headers); channel.BasicAck(ea.DeliveryTag, false); } await callback.Invoke(raw, default(T)); } }); }
public AsyncEventHandler <BasicDeliverEventArgs> Dequeue <T>(CancellationToken cancellationToken, Func <string, T, Task> callback) { if (callback == null) { throw new ArgumentNullException(nameof(callback)); } var channel = _messagingFactory.Configure(); return(async(model, ea) => { cancellationToken.ThrowIfCancellationRequested(); _logger.LogDebug("MESSAGING | RECEIVING NEW MESSAGE"); var raw = Encoding.UTF8.GetString(ea.Body.Span); var retries = 0; _logger.LogDebug("MESSAGING | RAW MESSAGE: { raw }"); try { retries = Retries(ea); _logger.LogDebug("MESSAGING | THIS MESSAGE HAS BEEN PROCESSED { retries } TIMES"); var message = JsonConvert.DeserializeObject <T>(raw); await callback.Invoke(raw, message).ConfigureAwait(false); _logger.LogDebug("MESSAGING | MESSAGE WILL BE ACKED"); channel.BasicAck(ea.DeliveryTag, false); _logger.LogDebug("MESSAGING | MESSAGE HAS BEEN ACKED"); } catch (Exception ex) { _logger.LogError($"MESSAGING | SOMETHING HAPPENED WHEN PROCESSING THE MESSAGE: {ex}"); if (_messaging.Retries > retries) { channel.BasicNack(ea.DeliveryTag, false, false); _logger.LogDebug("MESSAGING | MESSAGE HAS BEEN NACKED"); } else { _logger.LogDebug("MESSAGING | MESSAGE WAS PROCESSED TOO MANY TIMES, PUSHING TO ERROR QUEUE"); var headers = new Dictionary <string, object> { { "queue", _messaging.Consuming.Queue }, { "exception", JsonConvert.SerializeObject(ex) } }; Queue(_messaging.Error.Exchange.Name, _messaging.Error.Routingkey, raw, headers); channel.BasicAck(ea.DeliveryTag, false); } } }); }