Beispiel #1
0
        /// <summary>
        /// Invoked when the timer used to check the status of trackers is invoked
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void OnTimerElapsed(object source, EventArgs e)
        {
            if (!IsOpened)
            {
                return;
            }

            Metric.Context("FEED").Meter("FeedRecoveryManager->OnTimerElapsed", Unit.Calls).Mark();

            foreach (var recoveryTracker in _producerRecoveryManagers.Values)
            {
                recoveryTracker.CheckStatus();
            }

            try
            {
                if (_isOpened != 0)
                {
                    _inactivityTimer.FireOnce(TimeSpan.FromSeconds(TrackersCheckPeriodSeconds));
                }
            }
            catch (ObjectDisposedException ex)
            {
                _executionLog.Info($"Error happened during invoking timer, because the instance {ex.ObjectName} is being disposed.");
            }
        }
        /// <summary>
        /// Invoked when the internally used timer elapses
        /// </summary>
        /// <param name="sender">A <see cref="object" /> representation of the <see cref="ITimer" /> raising the event</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data</param>
        private void OnTimerElapsed(object sender, EventArgs e)
        {
            try
            {
                CreateAndOpenConsumerChannel();
            }
            catch (Exception)
            {
                // ignored
            }

            if (_shouldBeOpened == 1)
            {
                _healthTimer.FireOnce(TimeSpan.FromSeconds(_timerInterval));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RabbitMqConsumerChannel" /> class
        /// </summary>
        /// <param name="channelFactory">A <see cref="IChannelFactory" /> used to construct the <see cref="IModel" /> representing Rabbit MQ channel</param>
        /// <param name="mtsChannelSettings">The mts channel settings</param>
        /// <param name="channelSettings">The channel settings</param>
        /// <param name="connectionStatus">The connection status</param>
        public RabbitMqPublisherChannel(IChannelFactory channelFactory, IMtsChannelSettings mtsChannelSettings, IRabbitMqChannelSettings channelSettings, IConnectionStatus connectionStatus)
        {
            Guard.Argument(channelFactory, nameof(channelFactory)).NotNull();
            Guard.Argument(mtsChannelSettings, nameof(mtsChannelSettings)).NotNull();
            Guard.Argument(channelSettings, nameof(channelSettings)).NotNull();
            Guard.Argument(connectionStatus, nameof(connectionStatus)).NotNull();

            _channelFactory = channelFactory;

            _mtsChannelSettings = mtsChannelSettings;

            _channelSettings = channelSettings;

            _useQueue = false;
            if (channelSettings.MaxPublishQueueTimeoutInMs > 0 || channelSettings.PublishQueueLimit > 0)
            {
                _useQueue            = true;
                _msgQueue            = new ConcurrentQueue <PublishQueueItem>();
                _queueLimit          = channelSettings.PublishQueueLimit > 1 ? _channelSettings.PublishQueueLimit : -1;
                _queueTimeout        = channelSettings.MaxPublishQueueTimeoutInMs >= 10000 ? _channelSettings.MaxPublishQueueTimeoutInMs : 15000;
                _queueTimer          = new SdkTimer(new TimeSpan(0, 0, 5), new TimeSpan(0, 0, 1));
                _queueTimer.Elapsed += OnTimerElapsed;
                _queueTimer.FireOnce(new TimeSpan(0, 0, 5));
            }
            _shouldBeOpened = 0;

            UniqueId = _channelFactory.GetUniqueId();

            _connectionStatus = (ConnectionStatus)connectionStatus;
        }
Beispiel #4
0
        /// <summary>
        /// Invoked when the internally used timer elapses
        /// </summary>
        /// <param name="sender">A <see cref="object" /> representation of the <see cref="ITimer" /> raising the event</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data</param>
        private void OnTimerElapsed(object sender, EventArgs e)
        {
            while (!_msgQueue.IsEmpty)
            {
                if (!_connectionStatus.IsConnected)
                {
                    break;
                }
                PublishQueueItem pqi = null;
                try
                {
                    if (_msgQueue.TryDequeue(out pqi))
                    {
                        //check if expired
                        if (pqi.Timestamp < DateTime.Now.AddMilliseconds(-_queueTimeout))
                        {
                            var msg = $"At {DateTime.Now} publishing queue item is expired. CorrelationId={pqi.CorrelationId}, RoutingKey={pqi.RoutingKey}, Added={pqi.Timestamp}.";
                            ExecutionLog.LogError(msg);
                            FeedLog.LogError(msg);
                            RaiseMessagePublishFailedEvent(pqi.Message, pqi.CorrelationId, pqi.RoutingKey, "Queue item is expired.");
                            continue;
                        }

                        //publish
                        var publishResult = PublishMsg(pqi.TicketId, (byte[])pqi.Message, pqi.RoutingKey, pqi.CorrelationId, pqi.ReplyRoutingKey);

                        if (publishResult.IsSuccess)
                        {
                            if (FeedLog.IsEnabled(LogLevel.Debug))
                            {
                                FeedLog.LogDebug($"Publish succeeded. CorrelationId={pqi.CorrelationId}, RoutingKey={pqi.RoutingKey}, ReplyRoutingKey={pqi.ReplyRoutingKey}, Added={pqi.Timestamp}.");
                            }
                            else
                            {
                                FeedLog.LogInformation($"Publish succeeded. CorrelationId={pqi.CorrelationId}, RoutingKey={pqi.RoutingKey}, Added={pqi.Timestamp}.");
                            }
                        }
                        else
                        {
                            FeedLog.LogWarning($"Publish failed. CorrelationId={pqi.CorrelationId}, RoutingKey={pqi.RoutingKey}, Added={pqi.Timestamp}. Reason={publishResult.Message}");
                            RaiseMessagePublishFailedEvent(pqi.Message, pqi.CorrelationId, pqi.RoutingKey, publishResult.Message);
                        }
                    }
                }
                catch (Exception exception)
                {
                    FeedLog.LogError($"Error during publishing queue item. CorrelationId={pqi?.CorrelationId}, RoutingKey={pqi?.RoutingKey}, Added={pqi?.Timestamp}.", exception);
                    if (pqi != null)
                    {
                        RaiseMessagePublishFailedEvent(pqi.Message, pqi.CorrelationId, pqi.RoutingKey, "Error during publishing queue item: " + exception);
                    }
                }
            }

            if (_useQueue)
            {
                _queueTimer.FireOnce(TimeSpan.FromMilliseconds(200)); // recheck after X milliseconds
            }
        }
 /// <summary>
 /// Handles the <see cref="E:TimerElapsed" /> event
 /// </summary>
 /// <param name="sender">The sender</param>
 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data</param>
 private void OnTimerElapsed(object sender, EventArgs e)
 {
     try
     {
         DeleteExpiredCacheItems();
     }
     catch (Exception exception)
     {
         _executionLog.Warn("Error during cleaning TicketCache.", exception);
     }
     _timer.FireOnce(new TimeSpan(0, 0, 10));
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="TicketSenderBase"/> class
        /// </summary>
        /// <param name="publisherChannel">The publisher channel</param>
        /// <param name="ticketCache">The ticket cache</param>
        /// <param name="mtsChannelSettings">The MTS channel settings</param>
        /// <param name="rabbitMqChannelSettings">Rabbit channel settings</param>
        internal TicketSenderBase(IRabbitMqPublisherChannel publisherChannel,
                                  ConcurrentDictionary <string, TicketCacheItem> ticketCache,
                                  IMtsChannelSettings mtsChannelSettings,
                                  IRabbitMqChannelSettings rabbitMqChannelSettings)
        {
            Guard.Argument(publisherChannel, nameof(publisherChannel)).NotNull();
            Guard.Argument(ticketCache, nameof(ticketCache)).NotNull();
            Guard.Argument(mtsChannelSettings, nameof(mtsChannelSettings)).NotNull();
            Guard.Argument(rabbitMqChannelSettings, nameof(rabbitMqChannelSettings)).NotNull();

            _publisherChannel        = publisherChannel;
            _ticketCache             = ticketCache;
            _mtsChannelSettings      = mtsChannelSettings;
            _rabbitMqChannelSettings = rabbitMqChannelSettings;
            _publisherChannel.MqMessagePublishFailed += PublisherChannelOnMqMessagePublishFailed;

            _timer          = new SdkTimer(new TimeSpan(0, 0, 0, 0, GetCacheTimeout(null)), new TimeSpan(0, 0, 10));
            _timer.Elapsed += OnTimerElapsed;
            _timer.FireOnce(new TimeSpan(0, 0, 0, 0, GetCacheTimeout(null)));
        }