public void Stop() { _stop = true; if (!_stopped.Wait(_threadStopWaitTimeout)) { throw new TimeoutException(string.Format("Unable to stop thread '{0}'.", Name)); } _queueMonitor.Unregister(this); }
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; const int sleepmax = 500; 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 if (iterationsCount < sleepmax) { Thread.Sleep(1); } else { _starving = true; _msgAddEvent.WaitOne(100); _starving = false; } } else { _queueStats.EnterBusy(); #if DEBUG _queueStats.Dequeued(msg); #endif iterationsCount = 0; 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.ErrorException(ex, "Error while processing message {0} in queued handler '{1}'.", msg, Name); } } _queueStats.Stop(); _stopped.Set(); _queueMonitor.Unregister(this); Thread.EndThreadAffinity(); }
private void ReadFromQueue(object o) { try{ _queueStats.Start(); Thread.BeginThreadAffinity(); // ensure we are not switching between OS threads. Required at least for v8. var batch = new Message[128]; while (!_stop) { Message msg = null; try { QueueBatchDequeueResult dequeueResult; if (_queue.TryDequeue(batch, out dequeueResult) == false) { _starving = true; _queueStats.EnterIdle(); _msgAddEvent.WaitOne(100); _msgAddEvent.Reset(); _starving = false; } else { var estimatedQueueCount = dequeueResult.EstimateCurrentQueueCount; for (var i = 0; i < dequeueResult.DequeueCount; i++) { try { msg = batch[i]; _queueStats.EnterBusy(); #if DEBUG _queueStats.Dequeued(msg); #endif _queueStats.ProcessingStarted(msg.GetType(), estimatedQueueCount); if (_watchSlowMsg) { var start = DateTime.UtcNow; _consumer.Handle(msg); var elapsed = DateTime.UtcNow - start; if (elapsed > _slowMsgThreshold) { Log.Trace("SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevEstimatedQueueCount}/{curEstimatedQueueCount}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, estimatedQueueCount, _queue.EstimageCurrentQueueCount()); if (elapsed > QueuedHandler.VerySlowMsgThreshold && !(msg is SystemMessage.SystemInit)) { Log.Error("---!!! VERY SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevEstimatedQueueCount}/{curEstimatedQueueCount}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, estimatedQueueCount, _queue.EstimageCurrentQueueCount()); } } } else { _consumer.Handle(msg); } } catch (Exception ex) { Log.ErrorException(ex, "Error while processing message {message} in queued handler '{queue}'.", msg, Name); #if DEBUG throw; #endif } estimatedQueueCount -= 1; _queueStats.ProcessingEnded(1); } } } catch (Exception ex) { Log.ErrorException(ex, "Error while processing message {message} in queued handler '{queue}'.", msg, Name); #if DEBUG throw; #endif } } } catch (Exception ex) { _tcs.TrySetException(ex); throw; } finally{ _queueStats.Stop(); _stopped.Set(); _queueMonitor.Unregister(this); Thread.EndThreadAffinity(); } }
private void ReadFromQueue(object o) { try { _queueStats.Start(); Thread.BeginThreadAffinity(); // ensure we are not switching between OS threads. Required at least for v8. while (!_stop) { Message msg = null; try { if (!_queue.TryDequeue(out msg)) { _starving = true; _queueStats.EnterIdle(); _msgAddEvent.Wait(100); _msgAddEvent.Reset(); _starving = false; } else { _queueStats.EnterBusy(); #if DEBUG _queueStats.Dequeued(msg); #endif 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.Verbose( "SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevQueueCount}/{curQueueCount}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count); if (elapsed > QueuedHandler.VerySlowMsgThreshold && !(msg is SystemMessage.SystemInit)) { Log.Error( "---!!! VERY SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevQueueCount}/{curQueueCount}.", 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 {message} in queued handler '{queue}'.", msg, Name); #if DEBUG throw; #endif } } } catch (Exception ex) { _tcs.TrySetException(ex); throw; } finally { _queueStats.Stop(); _stopped.Set(); _queueMonitor.Unregister(this); Thread.EndThreadAffinity(); } }
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.ErrorException(ex, "Error while processing message {0} in queued handler '{1}'.", msg, Name); } } _queueStats.Stop(); _stopped.Set(); _queueMonitor.Unregister(this); Thread.EndThreadAffinity(); }