Exemplo n.º 1
0
 /// <summary>
 /// Constructs the worker factory
 /// </summary>
 public ThreadWorkerFactory(ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, int maxParallelism, IBackoffStrategy backoffStrategy, IRebusLoggerFactory rebusLoggerFactory)
 {
     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!");
     }
     _transport                 = transport;
     _pipeline                  = pipeline;
     _pipelineInvoker           = pipelineInvoker;
     _backoffStrategy           = backoffStrategy;
     _rebusLoggerFactory        = rebusLoggerFactory;
     _parallelOperationsManager = new ParallelOperationsManager(maxParallelism);
 }
Exemplo n.º 2
0
    /// <summary>
    /// Creates the worker factory
    /// </summary>
    public ThreadPoolWorkerFactory(ITransport transport, IRebusLoggerFactory rebusLoggerFactory,
                                   IPipelineInvoker pipelineInvoker, Options options, Func <RebusBus> busGetter,
                                   BusLifetimeEvents busLifetimeEvents, IBackoffStrategy backoffStrategy, CancellationToken busDisposalCancellationToken)
    {
        if (busLifetimeEvents == null)
        {
            throw new ArgumentNullException(nameof(busLifetimeEvents));
        }
        _transport                    = transport ?? throw new ArgumentNullException(nameof(transport));
        _rebusLoggerFactory           = rebusLoggerFactory ?? throw new ArgumentNullException(nameof(rebusLoggerFactory));
        _pipelineInvoker              = pipelineInvoker ?? throw new ArgumentNullException(nameof(pipelineInvoker));
        _options                      = options ?? throw new ArgumentNullException(nameof(options));
        _busGetter                    = busGetter ?? throw new ArgumentNullException(nameof(busGetter));
        _backoffStrategy              = backoffStrategy ?? throw new ArgumentNullException(nameof(backoffStrategy));
        _busDisposalCancellationToken = busDisposalCancellationToken;
        _parallelOperationsManager    = new ParallelOperationsManager(options.MaxParallelism);
        _log = _rebusLoggerFactory.GetLogger <ThreadPoolWorkerFactory>();

        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;
    }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructs the TPL worker factory
        /// </summary>
        public TplWorkerFactory(ITransport transport, IRebusLoggerFactory rebusLoggerFactory, IPipelineInvoker pipelineInvoker, Options options, Func <RebusBus> busGetter, BusLifetimeEvents busLifetimeEvents, IBackoffStrategy backoffStrategy, CancellationToken busDisposalCancellationToken)
        {
            _transport                    = transport;
            _rebusLoggerFactory           = rebusLoggerFactory;
            _pipelineInvoker              = pipelineInvoker;
            _options                      = options;
            _busGetter                    = busGetter;
            _backoffStrategy              = backoffStrategy;
            _busDisposalCancellationToken = busDisposalCancellationToken;
            _parallelOperationsManager    = new ParallelOperationsManager(options.MaxParallelism);
            _log = _rebusLoggerFactory.GetLogger <TplWorkerFactory>();

            busLifetimeEvents.WorkersStopped += WaitForContinuationsToFinish;
        }
Exemplo n.º 5
0
        public TplWorker(string workerName, RebusBus owningBus, ITransport transport, IRebusLoggerFactory rebusLoggerFactory, IPipelineInvoker pipelineInvoker, ParallelOperationsManager parallelOperationsManager, Options options, IBackoffStrategy backoffStrategy)
        {
            _owningBus                 = owningBus;
            _transport                 = transport;
            _pipelineInvoker           = pipelineInvoker;
            _parallelOperationsManager = parallelOperationsManager;
            _options         = options;
            _backoffStrategy = backoffStrategy;
            Name             = workerName;

            _cancellationToken = _cancellationTokenSource.Token;
            _log = rebusLoggerFactory.GetLogger <TplWorker>();

            Task.Run(Run);
        }
Exemplo n.º 6
0
            public AsyncTaskWorker(string name, ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, int maxParallelismPerWorker)
            {
                _transport                 = transport;
                _pipeline                  = pipeline;
                _pipelineInvoker           = pipelineInvoker;
                _parallelOperationsManager = new ParallelOperationsManager(maxParallelismPerWorker);

                Name = name;

                _workerTask = new AsyncTask(name, DoWork, prettyInsignificant: true)
                {
                    Interval = TimeSpan.FromMilliseconds(1)
                };
                _log.Debug("Starting (task-based) worker {0}", Name);
                _workerTask.Start();
            }
Exemplo n.º 7
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();
        }
Exemplo n.º 8
0
            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 TplAsyncTask(name, DoWork, new ConsoleLoggerFactory(false), prettyInsignificant: true)
                {
                    Interval = TimeSpan.FromMilliseconds(1)
                };
                _log.Debug("Starting (task-based) worker {0}", Name);
                _workerTask.Start();
            }
Exemplo n.º 9
0
 internal ThreadPoolWorker(string name, ITransport transport, IRebusLoggerFactory rebusLoggerFactory, IPipelineInvoker pipelineInvoker, ParallelOperationsManager parallelOperationsManager, RebusBus owningBus, Options options, IBackoffStrategy backoffStrategy)
 {
     Name                       = name;
     _log                       = rebusLoggerFactory.GetLogger <ThreadPoolWorker>();
     _transport                 = transport;
     _pipelineInvoker           = pipelineInvoker;
     _parallelOperationsManager = parallelOperationsManager;
     _owningBus                 = owningBus;
     _options                   = options;
     _backoffStrategy           = backoffStrategy;
     _workerThread              = new Thread(Run)
     {
         Name         = name,
         IsBackground = true
     };
     _workerThread.Start();
 }
Exemplo n.º 10
0
        /// <summary>
        /// Constructs the worker factory
        /// </summary>
        public ThreadWorkerFactory(ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, IBackoffStrategy backoffStrategy, IRebusLoggerFactory rebusLoggerFactory, Options options, Func <RebusBus> busGetter)
        {
            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));
            }

            var maxParallelism = options.MaxParallelism;

            if (maxParallelism <= 0)
            {
                throw new ArgumentOutOfRangeException($"Cannot use value '{maxParallelism}' as setting for max parallelism as it must be a positive number");
            }

            var workerShutdownTimeout = options.WorkerShutdownTimeout;

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

            _transport                 = transport;
            _pipeline                  = pipeline;
            _pipelineInvoker           = pipelineInvoker;
            _backoffStrategy           = backoffStrategy;
            _rebusLoggerFactory        = rebusLoggerFactory;
            _busGetter                 = busGetter;
            _parallelOperationsManager = new ParallelOperationsManager(maxParallelism);
            _workerShutdownTimeout     = workerShutdownTimeout;
        }
Exemplo n.º 11
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();
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
 /// <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);
 }
Exemplo n.º 15
0
 internal ParallelOperation(bool canContinue, ParallelOperationsManager parallelOperationsManager)
 {
     _canContinue = canContinue;
     _parallelOperationsManager = parallelOperationsManager;
 }