Example #1
0
        private void StartCore()
        {
            this.consumersFaulted = false;

            var cancelToken   = this.configuration.TokenSource.Token;
            var consumerTasks = new List <Task>();

            this.configuration.Logger.Info("Starting...");
            this.InitTransport();

            // Start the work queue consumers
            this.configuration.Logger.DebugFormat("Starting {0} work queue consumer{1}", this.configuration.MaxWorkers, this.configuration.MaxWorkers == 1 ? string.Empty : "s");
            for (var i = 0; i < this.configuration.MaxWorkers; i++)
            {
                var consumer = new PrimaryConsumer(this.configuration);
                consumerTasks.Add(this.CreateConsumerTask(consumer));
            }

            // Start the delay consumer
            var delayConsumer = new DelayConsumer(this.configuration);

            cancelToken.Register(() => delayConsumer.Cancel());
            consumerTasks.Add(this.CreateConsumerTask(delayConsumer));

            try
            {
                Task.WaitAll(consumerTasks.ToArray());
            }
            catch (AggregateException ex)
            {
                // Ignore if it is just OperationCancelledException
                // That will be thrown when cancellation is requested
                if (!ex.InnerExceptions.All(e => e is OperationCanceledException))
                {
                    this.configuration.Logger.FatalFormat("Unhandled exception inside of consumer: {0}", ex.ToString());
                    this.consumersFaulted = true;
                    throw;
                }
            }

            this.configuration.Transport.Shutdown();
        }
Example #2
0
        public virtual void Start()
        {
            var cancelToken = this.configuration.TokenSource.Token;
            var consumerTasks = new List<Task>();

            this.configuration.Logger.Info("Starting...");
            this.InitTransport();

            // Start the work queue consumers
            this.configuration.Logger.DebugFormat("Starting {0} work queue consumer{1}", this.configuration.MaxWorkers, this.configuration.MaxWorkers == 1 ? string.Empty : "s");
            for (var i = 0; i < this.configuration.MaxWorkers; i++)
            {
                var consumer = new PrimaryConsumer(this.configuration);
                consumerTasks.Add(this.CreateConsumerTask(consumer));
            }

            // Start the delay consumer
            var delayConsumer = new DelayConsumer(this.configuration);
            cancelToken.Register(() => delayConsumer.Cancel());
            consumerTasks.Add(this.CreateConsumerTask(delayConsumer));

            try
            {
                Task.WaitAll(consumerTasks.ToArray());
            }
            catch (AggregateException ex)
            {
                // Ignore if it is just OperationCancelledException
                // That will be thrown when cancellation is requested
                if (!ex.InnerExceptions.All(e => e is OperationCanceledException))
                {
                    this.configuration.Logger.FatalFormat("Unhandled exception inside of consumer: {0}", ex.ToString());
                    this.consumersFaulted = true;
                    throw;
                }
            }

            this.configuration.Transport.Shutdown();
        }