/// <summary>
        /// Creates the worker factory
        /// </summary>
        public ThreadPoolWorkerFactory(ITransport transport, IRebusLoggerFactory rebusLoggerFactory, IPipeline pipeline, IPipelineInvoker pipelineInvoker, Options options, Func<RebusBus> busGetter, BusLifetimeEvents busLifetimeEvents, ISyncBackoffStrategy backoffStrategy)
        {
            if (transport == null) throw new ArgumentNullException(nameof(transport));
            if (rebusLoggerFactory == null) throw new ArgumentNullException(nameof(rebusLoggerFactory));
            if (pipeline == null) throw new ArgumentNullException(nameof(pipeline));
            if (pipelineInvoker == null) throw new ArgumentNullException(nameof(pipelineInvoker));
            if (options == null) throw new ArgumentNullException(nameof(options));
            if (busGetter == null) throw new ArgumentNullException(nameof(busGetter));
            if (busLifetimeEvents == null) throw new ArgumentNullException(nameof(busLifetimeEvents));
            if (backoffStrategy == null) throw new ArgumentNullException(nameof(backoffStrategy));
            _transport = transport;
            _rebusLoggerFactory = rebusLoggerFactory;
            _pipeline = pipeline;
            _pipelineInvoker = pipelineInvoker;
            _options = options;
            _busGetter = busGetter;
            _backoffStrategy = backoffStrategy;
            _parallelOperationsManager = new ParallelOperationsManager(options.MaxParallelism);
            _log = _rebusLoggerFactory.GetCurrentClassLogger();

            if (_options.MaxParallelism < 1)
            {
                throw new ArgumentException($"Max parallelism is {_options.MaxParallelism} which is an invalid value");
            }

            if (options.WorkerShutdownTimeout < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException($"Cannot use '{options.WorkerShutdownTimeout}' as worker shutdown timeout as it");
            }

            busLifetimeEvents.WorkersStopped += WaitForContinuationsToFinish;
        }
            internal ParallelOperation(bool canContinue, ParallelOperationsManager parallelOperationsManager)
            {
                _canContinue = canContinue;
                _parallelOperationsManager = parallelOperationsManager;

                if (!_canContinue) return;

                _parallelOperationsManager.OperationStarted();
            }
 /// <summary>
 /// Constructs the worker factory
 /// </summary>
 public ThreadWorkerFactory(ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, int maxParallelism)
 {
     if (transport == null) throw new ArgumentNullException("transport");
     if (pipeline == null) throw new ArgumentNullException("pipeline");
     if (pipelineInvoker == null) throw new ArgumentNullException("pipelineInvoker");
     if (maxParallelism <= 0) throw new ArgumentOutOfRangeException(string.Format("Cannot use value '{0}' as max parallelism!", maxParallelism));
     _transport = transport;
     _pipeline = pipeline;
     _pipelineInvoker = pipelineInvoker;
     _parallelOperationsManager = new ParallelOperationsManager(maxParallelism);
 }
        public void DoesNotAllowMoreThanMaxParallelismToContinue()
        {
            var manager = new ParallelOperationsManager(3);

            var operation1 = manager.TryBegin();
            var operation2 = manager.TryBegin();
            var operation3 = manager.TryBegin();
            var operation4 = manager.TryBegin();

            Assert.That(operation1.CanContinue(), Is.True);
            Assert.That(operation2.CanContinue(), Is.True);
            Assert.That(operation3.CanContinue(), Is.True);
            Assert.That(operation4.CanContinue(), Is.False);
        }
Exemple #5
0
        internal ThreadWorker(ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, string workerName, ThreadWorkerSynchronizationContext threadWorkerSynchronizationContext, ParallelOperationsManager parallelOperationsManager)
        {
            Name = workerName;

            _transport = transport;
            _pipeline = pipeline;
            _pipelineInvoker = pipelineInvoker;
            _threadWorkerSynchronizationContext = threadWorkerSynchronizationContext;
            _parallelOperationsManager = parallelOperationsManager;
            _workerThread = new Thread(ThreadStart)
            {
                Name = workerName,
                IsBackground = true
            };
            _workerThread.Start();
        }
Exemple #6
0
        internal ThreadWorker(ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, string workerName, ThreadWorkerSynchronizationContext threadWorkerSynchronizationContext, ParallelOperationsManager parallelOperationsManager, IBackoffStrategy backoffStrategy, IRebusLoggerFactory rebusLoggerFactory)
        {
            Name = workerName;

            _log = rebusLoggerFactory.GetCurrentClassLogger();
            _transport = transport;
            _pipeline = pipeline;
            _pipelineInvoker = pipelineInvoker;
            _threadWorkerSynchronizationContext = threadWorkerSynchronizationContext;
            _parallelOperationsManager = parallelOperationsManager;
            _backoffStrategy = backoffStrategy;
            _workerThread = new Thread(ThreadStart)
            {
                Name = workerName,
                IsBackground = true
            };
            _workerThread.Start();
        }
Exemple #7
0
 internal ThreadPoolWorker(string name, ITransport transport, IRebusLoggerFactory rebusLoggerFactory, IPipeline pipeline, IPipelineInvoker pipelineInvoker, ParallelOperationsManager parallelOperationsManager, RebusBus owningBus, Options options, ISyncBackoffStrategy backoffStrategy)
 {
     Name = name;
     _log = rebusLoggerFactory.GetCurrentClassLogger();
     _transport = transport;
     _pipeline = pipeline;
     _pipelineInvoker = pipelineInvoker;
     _parallelOperationsManager = parallelOperationsManager;
     _owningBus = owningBus;
     _options = options;
     _backoffStrategy = backoffStrategy;
     _workerThread = new Thread(Run)
     {
         Name = name,
         IsBackground = true
     };
     _workerThread.Start();
 }
        /// <summary>
        /// Constructs the worker factory
        /// </summary>
        public ThreadWorkerFactory(ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, int maxParallelism, IBackoffStrategy backoffStrategy, IRebusLoggerFactory rebusLoggerFactory, TimeSpan workerShutdownTimeout)
        {
            if (transport == null) throw new ArgumentNullException(nameof(transport));
            if (pipeline == null) throw new ArgumentNullException(nameof(pipeline));
            if (pipelineInvoker == null) throw new ArgumentNullException(nameof(pipelineInvoker));
            if (backoffStrategy == null) throw new ArgumentNullException(nameof(backoffStrategy));
            if (rebusLoggerFactory == null) throw new ArgumentNullException(nameof(rebusLoggerFactory));
            if (maxParallelism <= 0) throw new ArgumentOutOfRangeException($"Cannot use value '{maxParallelism}' as max parallelism!");
            if (workerShutdownTimeout == null) throw new ArgumentNullException(nameof(workerShutdownTimeout));

            _transport = transport;
            _pipeline = pipeline;
            _pipelineInvoker = pipelineInvoker;
            _backoffStrategy = backoffStrategy;
            _rebusLoggerFactory = rebusLoggerFactory;
            _parallelOperationsManager = new ParallelOperationsManager(maxParallelism);
            _workerShutdownTimeout = workerShutdownTimeout;
        }
        public void ReleasesOperationAsExpected()
        {
            var manager = new ParallelOperationsManager(3);

            var operation1 = manager.TryBegin();
            var operation2 = manager.TryBegin();
            var operation3 = manager.TryBegin();

            operation1.Dispose();

            var operation4 = manager.TryBegin();
            var operation5 = manager.TryBegin();

            Assert.That(operation1.CanContinue(), Is.True);
            Assert.That(operation2.CanContinue(), Is.True);
            Assert.That(operation3.CanContinue(), Is.True);
            Assert.That(operation4.CanContinue(), Is.True);
            Assert.That(operation5.CanContinue(), Is.False);
        }
        public void ReleasingOperationThatCouldNotContinueDoesNotAffectAnything()
        {
            var manager = new ParallelOperationsManager(2);

            var op1 = manager.TryBegin();
            var op2 = manager.TryBegin();

            var op3 = manager.TryBegin();
            var op4 = manager.TryBegin();

            op1.Dispose();
            op2.Dispose();
            op3.Dispose();
            op4.Dispose();

            var op5 = manager.TryBegin();
            var op6 = manager.TryBegin();
            var op7 = manager.TryBegin();

            Assert.That(op5.CanContinue(), Is.True);
            Assert.That(op6.CanContinue(), Is.True);
            Assert.That(op7.CanContinue(), Is.False);
        }
 internal ParallelOperation(bool canContinue, ParallelOperationsManager parallelOperationsManager)
 {
     _canContinue = canContinue;
     _parallelOperationsManager = parallelOperationsManager;
 }
            public AsyncTaskWorker(string name, ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, int maxParallelismPerWorker, IRebusLoggerFactory rebusLoggerFactory)
            {
                _transport = transport;
                _pipeline = pipeline;
                _pipelineInvoker = pipelineInvoker;
                _parallelOperationsManager = new ParallelOperationsManager(maxParallelismPerWorker);
                _log = rebusLoggerFactory.GetCurrentClassLogger();

                Name = name;

                _workerTask = new AsyncTask(name, DoWork, new ConsoleLoggerFactory(false), prettyInsignificant: true)
                {
                    Interval = TimeSpan.FromMilliseconds(1)
                };
                _log.Debug("Starting (task-based) worker {0}", Name);
                _workerTask.Start();
            }
Exemple #13
0
 internal ParallelOperation(bool canContinue, ParallelOperationsManager parallelOperationsManager)
 {
     _canContinue = canContinue;
     _parallelOperationsManager = parallelOperationsManager;
 }