Esempio n. 1
0
        /// <summary>
        /// Each time we enqueue a job we need to check:
        /// 1) if the queue has enough jobs to create a batch
        /// 2) or shutdown method was called and we need to send all the jobs we have as a batch.
        /// </summary>
        private void ScheduleBatch(bool forced = false)
        {
            if (forced || _processorJobs.Count >= _maxBatchSize)
            {
                lock (batchLock)
                {
                    // create and send a batch of batchSize jobs
                    if (_processorJobs.Count >= _maxBatchSize)
                    {
                        var batch = new List <ProcessorJob>();
                        for (int i = 1; i <= _maxBatchSize; i++)
                        {
                            if (_processorJobs.TryDequeue(out ProcessorJob job))
                            {
                                batch.Add(job);
                            }
                            else
                            {
                                break;
                            }
                        }

                        _batchScheduler.ScheduleBatch(batch);
                    }
                    //if we shutdown we need to put jobs we have in queue
                    if (forced)
                    {
                        var batch = new List <ProcessorJob>();
                        for (int i = 1; i <= _maxBatchSize; i++)
                        {
                            if (_processorJobs.TryDequeue(out ProcessorJob job))
                            {
                                batch.Add(job);
                            }
                            else
                            {
                                break;
                            }
                        }

                        _batchScheduler.ScheduleBatch(batch);
                    }
                }
            }
        }