private QueueInfo CreateQueueInfo() { QueueInfo queueInfo = null; using (IModel model = this.ConnectionPool.GetConnection().CreateModel()) { queueInfo = new QueueInfo() { QueueName = this.QueueName, ConsumerCount = this.GetConsumerCount(model), MessageCount = this.GetMessageCount(model) }; } return(queueInfo); }
protected virtual void ManageConsumersLoop(CancellationToken token) { while (!token.IsCancellationRequested) { if (!this._isStopped) { QueueInfo queueInfo = this.CreateQueueInfo(); this._scalingAmount = this._consumerCountManager.GetScalingAmount(queueInfo, this._consumerWorkersCount); int scalingAmount = this._scalingAmount; for (var i = 1; i <= scalingAmount; i++) { this._scalingAmount--; this._consumerWorkersCount++; Task.Factory.StartNew(() => { try { using (IQueueConsumerWorker consumerWorker = this.CreateNewConsumerWorker(token)) { consumerWorker.DoConsume(); } } catch (Exception exception) { System.Diagnostics.Trace.TraceError("DevWeek.Architecture.MessageQueuing.RabbitMQConsumer", exception.ToString(), "QueueName", this.QueueName); } finally { this._consumerWorkersCount--; this._scalingAmount++; } } , token); } } Thread.Sleep(this._consumerCountManager.AutoscaleFrequency); } }
public int GetScalingAmount(QueueInfo queueInfo, int consumersRunningCount) { uint consumersByRatio = queueInfo.MessageCount / MessagesPerConsumerWorkerRatio; int idealConsumerCount; if (consumersByRatio < MinConcurrentConsumers) { idealConsumerCount = MinConcurrentConsumers.ToInt(); } else if (consumersByRatio > MaxConcurrentConsumers) { idealConsumerCount = MaxConcurrentConsumers.ToInt(); } else { idealConsumerCount = consumersByRatio.ToInt(); } int scalingAmount = idealConsumerCount - consumersRunningCount; return(scalingAmount); }
public int GetScalingAmount(QueueInfo queueInfo, int consumersRunningCount) => this.MaxConcurrentConsumers - consumersRunningCount;