Exemple #1
0
        private void ReadFromQueue(object o)
        {
            _queueStats.Start();
            Thread.BeginThreadAffinity(); // ensure we are not switching between OS threads. Required at least for v8.

            const int spinmax         = 5000;
            var       iterationsCount = 0;

            while (!_stop)
            {
                Message msg = null;
                try
                {
                    if (!_queue.TryDequeue(out msg))
                    {
                        _queueStats.EnterIdle();

                        iterationsCount += 1;
                        if (iterationsCount < spinmax)
                        {
                            //do nothing... spin
                        }
                        else
                        {
                            Thread.Sleep(1);
                        }
                    }
                    else
                    {
                        _queueStats.EnterBusy();

                        var cnt = _queue.Count;
                        _queueStats.ProcessingStarted(msg.GetType(), cnt);

                        if (_watchSlowMsg)
                        {
                            var start = DateTime.UtcNow;

                            _consumer.Handle(msg);

                            var elapsed = DateTime.UtcNow - start;
                            if (elapsed > _slowMsgThreshold)
                            {
                                Log.Trace("SLOW QUEUE MSG [{0}]: {1} - {2}ms. Q: {3}/{4}.",
                                          Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count);
                                if (elapsed > QueuedHandler.VerySlowMsgThreshold)// && !(msg is SystemMessage.SystemInit))
                                {
                                    Log.Error("---!!! VERY SLOW QUEUE MSG [{0}]: {1} - {2}ms. Q: {3}/{4}.",
                                              Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count);
                                }
                            }
                        }
                        else
                        {
                            _consumer.Handle(msg);
                        }

                        _queueStats.ProcessingEnded(1);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, String.Format("Error while processing message {0} in queued handler '{1}'.", msg, Name), ex);
                }
            }
            _queueStats.Stop();

            _stopped.Set();
            _queueMonitor.Unregister(this);
            Thread.EndThreadAffinity();
        }
Exemple #2
0
        private void ReadFromQueue(object o)
        {
            _queueStats.Start();
            Thread.BeginThreadAffinity(); // ensure we are not switching between OS threads. Required at least for v8.

            while (!_stop)
            {
                Message msg = null;
                try
                {
                    lock (_locker)
                    {
                        while (!_queue.TryDequeue(out msg) && !_stop)
                        {
                            _starving = true;
                            _queueStats.EnterIdle();
                            Monitor.Wait(_locker, 100);
                        }
                        _starving = false;
                        if (_stop)
                        {
                            break;
                        }
                    }

                    _queueStats.EnterBusy();

                    var cnt = _queue.Count;
                    _queueStats.ProcessingStarted(msg.GetType(), cnt);

                    if (_watchSlowMsg)
                    {
                        var start = DateTime.UtcNow;

                        _consumer.Handle(msg);

                        var elapsed = DateTime.UtcNow - start;
                        if (elapsed > _slowMsgThreshold)
                        {
                            Log.Trace("SLOW QUEUE MSG [{0}]: {1} - {2}ms. Q: {3}/{4}.",
                                      Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count);
                            if (elapsed > QueuedHandler.VerySlowMsgThreshold)// && !(msg is SystemMessage.SystemInit))
                            {
                                Log.Error("---!!! VERY SLOW QUEUE MSG [{0}]: {1} - {2}ms. Q: {3}/{4}.",
                                          Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count);
                            }
                        }
                    }
                    else
                    {
                        _consumer.Handle(msg);
                    }

                    _queueStats.ProcessingEnded(1);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, $"Error while processing message {msg} in queued handler '{Name}'.");
                }
            }
            _queueStats.Stop();

            _stopped.Set();
            _queueMonitor.Unregister(this);
            Thread.EndThreadAffinity();
        }