Ejemplo n.º 1
0
        private void ProcessorFunction()
        {
            Exception exceptionRaised = null;

            try
            {
                do
                {
                    _messageAddedSignal.WaitOne(1000);

                    while (!_jobsCancellationTokenSource.IsCancellationRequested && _messages.Count > 0)
                    {
                        var batch = new List <TMessage>();

                        while (!_jobsCancellationTokenSource.IsCancellationRequested && batch.Count < _batchSize && _messages.TryDequeue(out var message))
                        {
                            batch.Add(message);
                        }

                        if (batch.Count > 0)
                        {
                            ProcessMessageBatch(batch, _jobsCancellationTokenSource.Token, out var pauseSec);

                            if (pauseSec > 0)
                            {
                                OnPause();
                                Task.Delay(pauseSec * 1000, _jobsCancellationTokenSource.Token).Wait();
                                OnResume();
                            }
                        }
                    }
                }while (!_cancellationTokenSource.IsCancellationRequested);
            }
            catch (Exception ex)
            {
                Logger.LogCritical(ex, "Unhandled exception in batch processor function");
                exceptionRaised = ex;
            }

            _jobsCancellationTokenSource.SafeDispose();
            _jobsCancellationTokenSource = null;

            _cancellationTokenSource.SafeDispose();
            _cancellationTokenSource = null;

            _processorThread = null;

            TryOnStop();

            _stoppingSignal.Set();

            if (exceptionRaised != null)
            {
                OnError(exceptionRaised);
            }
        }