示例#1
0
        /// <summary>
        /// This method should never be called by a sub-class.
        /// This method is protected only to allow inheritors to create additional workers by overriding this method.
        /// </summary>
        protected virtual void CreateWorkers()
        {
            // ReSharper disable InconsistentlySynchronizedField
            MessageConsumer.Connect();

            var errorWorker = new WorkerScheduled(_settings.ErrorQueueName, _settings.ScheduledWorkersBatchSize,
                                                  ServiceScopeFactory, ServiceScope.GetRequiredService <IJobLogger <WorkerScheduled> >());

            errorWorker.Error += OnWorkerError;

            Workers.Add(errorWorker);

            for (var i = 0; i < _settings.ScheduledWorkersCount; i++)
            {
                var queueName = string.Format(_settings.ScheduledQueueNameTemplate, i);

                var worker = new WorkerScheduled(queueName, _settings.ScheduledWorkersBatchSize,
                                                 ServiceScopeFactory, ServiceScope.GetRequiredService <IJobLogger <WorkerScheduled> >());
                worker.Error += OnWorkerError;

                Workers.Add(worker);
            }

            for (var i = 0; i < _settings.PeriodicWorkersCount; i++)
            {
                var queueName = string.Format(_settings.PeriodicQueueNameTemplate, i);

                var worker = new WorkerScheduled(queueName, _settings.PeriodicWorkersBatchSize,
                                                 ServiceScopeFactory, ServiceScope.GetRequiredService <IJobLogger <WorkerScheduled> >());
                worker.Error += OnWorkerError;

                Workers.Add(worker);
            }

            for (var i = 0; i < _settings.ImmediateWorkersCount; i++)
            {
                var queueName = string.Format(_settings.ImmediateQueueNameTemplate, i);

                var worker = new WorkerImmediate(queueName, _settings.ImmediateWorkersBatchSize, _settings.MaxDegreeOfParallelismPerWorker,
                                                 ServiceScopeFactory, ServiceScope.GetRequiredService <IJobLogger <WorkerImmediate> >());
                worker.Error += OnWorkerError;

                Workers.Add(worker);
            }

            for (var i = 0; i < _settings.LongRunningWorkersCount; i++)
            {
                var queueName = string.Format(_settings.LongRunningQueueNameTemplate, i);

                var worker = new WorkerImmediate(queueName, _settings.LongRunningWorkersBatchSize, _settings.MaxDegreeOfParallelismPerWorker,
                                                 ServiceScopeFactory, ServiceScope.GetRequiredService <IJobLogger <WorkerImmediate> >());
                worker.Error += OnWorkerError;

                Workers.Add(worker);
            }

            // ReSharper restore InconsistentlySynchronizedField
        }