private void ProcessQueue(IModel model, CancellationToken cancellation) { while (!cancellation.IsCancellationRequested) { LinkProducerMessage message; try { message = _messageQueue.Wait(cancellation); } catch (Exception ex) { if (cancellation.IsCancellationRequested) { continue; } _logger.Error($"Cannot read message from queue: {ex}"); return; } var seq = model.NextPublishSeqNo; var corellationId = _ackQueue.Add(message, seq); var properties = model.CreateBasicProperties(); properties.Extend(message.Properties); if (_configuration.SetUserId) { properties.UserId = _channel.Connection.UserId; } if (properties.Headers == null) { properties.Headers = new Dictionary <string, object>(); } properties.Headers[CorrelationHeader] = Encoding.UTF8.GetBytes(corellationId); try { model.BasicPublish( _exchange.Name, message.PublishProperties.RoutingKey ?? "", message.PublishProperties.Mandatory ?? false, properties, message.Body ); } catch (Exception ex) { _logger.Error($"Cannot publish message: {ex.Message}"); return; } if (!ConfirmsMode) { _ackQueue.Ack(seq, false); } } }
private async Task <LinkTopologyState> OnConfigureAsync(IModel model, bool retry, CancellationToken cancellation) { if (retry) { try { _logger.Debug($"Retrying in {_configuration.RecoveryInterval.TotalSeconds:0.###}s"); await Task.Delay(_configuration.RecoveryInterval, cancellation) .ConfigureAwait(false); } catch { return(LinkTopologyState.Reconfiguring); } } _logger.Debug("Configuring topology"); try { await _topologyRunner .RunAsync(model, cancellation) .ConfigureAwait(false); } catch (Exception ex) { _logger.Warning($"Exception on configuration: {ex}"); try { await _configuration.TopologyHandler.ConfigurationError(ex) .ConfigureAwait(false); } catch (Exception handlerException) { _logger.Error($"Error in error handler: {handlerException}"); } return(LinkTopologyState.Reconfiguring); } try { await _configuration.TopologyHandler.Ready() .ConfigureAwait(false); } catch (Exception ex) { _logger.Error($"Error in ready handler: {ex}"); } _logger.Debug("Topology configured"); return(LinkTopologyState.Ready); }
private async Task Loop() { var newState = LinkConnectionState.Opening; while (true) { if (_disposeCancellation.IsCancellationRequested) { newState = LinkConnectionState.Stopping; } ChangeState(newState); try { switch (State) { case LinkConnectionState.Opening: case LinkConnectionState.Reopening: newState = await OpenReopenAsync(State == LinkConnectionState.Reopening) .ConfigureAwait(false) ? LinkConnectionState.Active : LinkConnectionState.Stopping; break; case LinkConnectionState.Active: await AsyncHelper.RunAsync(Active) .ConfigureAwait(false); newState = LinkConnectionState.Stopping; break; case LinkConnectionState.Stopping: await AsyncHelper.RunAsync(Stop) .ConfigureAwait(false); if (_disposeCancellation.IsCancellationRequested) { return; } newState = LinkConnectionState.Reopening; break; default: throw new NotImplementedException($"Handler for state ${State} not implemeted"); } } catch (Exception ex) { _logger.Error($"Unhandled exception: {ex}"); } } }