public void Handle()
{
try
{
try
{
TryProcessIncomingMessage();
}
catch (MessageException exception)
{
UserException?.Invoke(this, new MessageErrorEventArgs(exception));
}
catch (CommitException exception)
{
UserException?.Invoke(this, new MessageErrorEventArgs(exception));
}
catch (Exception e)
{
SystemException?.Invoke(this, new MessageErrorEventArgs(e));
//generic exceptions tend to indicate a serious problem - lets start delaying processing
//this counter will reset once a message has been processed by this thread
_seriousExceptionProcessBackOffHelper.Value.Wait();
}
}
catch (Exception ex) //not cool - one of the exception events threw an exception
{
//there isn't a whole lot we can do here
_log.ErrorException("An error has occurred while trying to handle an exception", ex, null);
}
}
/// <summary>
/// <summary> /// Main processing loop for internal thread. /// </summary> private void MainLoop() { while (!ShouldExit) { //if: //1) Single worker is allowed when idle //2) This worker is idle //3) All other workers are idle // Then: pause all other workers, and switch to single worker mode if (_workerCollection.Configuration.SingleWorkerWhenNoWorkFound && IdleStatus == WorkerIdleStatus.Idle && _workerCollection.AllWorkersAreIdle) { _workerCollection.PauseWorkers(); } if (ShouldExit) { return; } MessageProcessing.Handle(); } if (MessageProcessing != null && MessageProcessing.AsyncTaskCount > 0) { WaitOnAsyncTask.Wait(() => MessageProcessing.AsyncTaskCount > 0, () => _log.LogWarning( $"Unable to terminate because async requests have not finished. Current task count is {MessageProcessing.AsyncTaskCount}")); } }