protected override void OnExit(IProducerConsumerContext <TItem> context)
        {
            var c = GetContext(context);

            base.OnExit(c);
            onExitCallback?.Invoke(this, c);
        }
Exemple #2
0
 /// <summary>
 /// Occurs when an item is being consumed.
 /// </summary>
 /// <param name="context">The contextual information about what was produced.</param>
 protected virtual void OnConsume(IProducerConsumerContext <TItem> context)
 {
     using (var task = runner.ExecuteAsync(context))
     {
         Task.WaitAll(task);
     }
 }
        /// <summary>
        /// Creates a new worker.
        /// </summary>
        /// <param name="context">The contextual information about what was produced.</param>
        /// <returns>The worker used to work the produced item.</returns>
        protected virtual IWorker CreateWorker(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            lock (syncRoot)
            {
                IWorker worker = null;

                try
                {
                    worker = pool.Get(() => OnConsume(context), null);
                    workers.Add(worker);

                    return(worker);
                }
                catch (Exception)
                {
                    worker?.Dispose();
                    throw;
                }
            }
        }
        protected override async Task ConsumeAsync(IProducerConsumerContext <TItem> context)
        {
            var c = GetContext(context);

            await base.ConsumeAsync(c);

            onConsumeCallback?.Invoke(this, c);
        }
        private IProducerConsumerContext <TItem> GetContext(IProducerConsumerContext <TItem> context)
        {
            if (isContextOverridden)
            {
                return(overrideContext);
            }

            return(context);
        }
Exemple #6
0
        /// <summary>
        /// Determines whether the callback should be executed.
        /// </summary>
        /// <param name="context">The contextual information for the item which was produced.</param>
        /// <returns>true if the callback should be executed, otherwise false.</returns>
        protected virtual bool ShouldExecuteCallback(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(alwaysExecuteOnDefaultValue || !Equals(context.Item, default(TItem)));
        }
Exemple #7
0
        /// <inheritdoc />
        public IProducer <TItem> Resolve(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(callback(context));
        }
Exemple #8
0
        /// <summary>
        /// Acquires a synchronization lock for the item to be produced.
        /// </summary>
        /// <param name="context">The contextual information for the item which was produced.</param>
        /// <returns>The synchronization lock.</returns>
        protected virtual Task AcquireSynchronizationLockAsync(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(AcquireSynchronizationLockAsyncImpl(context));
        }
Exemple #9
0
        /// <summary>
        /// Occurs when the strategy is exiting.
        /// </summary>
        /// <param name="context">The contextual information about the item produced.</param>
        protected virtual void OnExit(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Dispose();
        }
Exemple #10
0
        /// <inheritdoc />
        public Task ExecuteAsync(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(ExecuteAsyncImpl(context));
        }
Exemple #11
0
        /// <summary>
        /// Produces an item.
        /// </summary>
        /// <param name="context">The contextual information for the item which was produced.</param>
        /// <returns>The task to await.</returns>
        protected virtual Task ProduceAsync(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(ProduceAsyncImpl(context));
        }
Exemple #12
0
        private async Task AcquireSynchronizationLockAsyncImpl(IProducerConsumerContext <TItem> context)
        {
            if (synchronizationPolicy == null)
            {
                return;
            }

            context.SynchronizationLock = await synchronizationPolicy.AcquireLockAsync(context.CancellationToken);
        }
Exemple #13
0
        /// <summary>
        /// Occurs when the strategy is starting to execute.
        /// </summary>
        /// <param name="context">The contextual information about the item produced.</param>
        protected virtual void OnStarted(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.ConsumptionContext.ExecutionStrategy = this;
            ProcessingContext.SetCurrent(context);
        }
Exemple #14
0
        /// <summary>
        /// Consumes the item.
        /// </summary>
        /// <param name="context">The contextual information about the item being consumed.</param>
        protected virtual async Task ConsumeAsync(IProducerConsumerContext <TItem> context)
        {
            context.ConsumptionContext.ConsumedOn = DateTime.Now;

            var stopwatch = Stopwatch.StartNew();
            await context.ConsumptionContext.Consumer.ConsumeAsync(context);

            stopwatch.Stop();

            context.ConsumptionContext.Duration = stopwatch.Elapsed;
        }
Exemple #15
0
        /// <inheritdoc />
        public void Consume(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            using (var worker = CreateWorker(context))
            {
                worker.Run();
            }
        }
Exemple #16
0
        private async Task ProduceAsyncImpl(IProducerConsumerContext <TItem> context)
        {
            context.ProductionContext.ProducedOn = DateTime.Now;

            var stopwatch = Stopwatch.StartNew();

            context.Item = await context.ProductionContext.Producer.ProduceAsync(context.CancellationToken);

            stopwatch.Stop();

            context.ProductionContext.Duration = stopwatch.Elapsed;
        }
        /// <inheritdoc />
        public void Consume(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            GuardMustNotBeDisposed();

            using (var task = ConsumeAsyncImpl(context))
            {
                task.Start();
                task.Wait();
            }
        }
Exemple #18
0
        /// <summary>
        /// Creates the consumer which will consume the object.
        /// </summary>
        /// <param name="context">The contextual information about the item being consumed.</param>
        protected virtual void CreateConsumer(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var consumer = consumerFactory.Resolve(context);

            if (consumer == null)
            {
                throw new InvalidOperationException("The consumer was not created.");
            }

            context.ConsumptionContext.Consumer = consumer;
        }
        private async Task ConsumeAsyncImpl(IProducerConsumerContext <TItem> context)
        {
            IWorker worker = null;

            try
            {
                worker = CreateWorker(context);
                await worker.RunAsync();
            }
            catch (Exception ex)
            {
                errorHandler.Handle(ex, ErrorSeverityLevel.NonFatal);
            }
            finally
            {
                ReleaseWorker(worker);
            }
        }
Exemple #20
0
        /// <summary>
        /// Creates a producer.
        /// </summary>
        /// <param name="context">The contextual information for the item which was produced.</param>
        protected virtual Task CreateProducerAsync(IProducerConsumerContext <TItem> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var producer = producerFactory.Resolve(context);

            if (producer == null)
            {
                throw new RuntimeException("The producer was not created.");
            }

            context.ProductionContext.Producer = producer;

            return(Task.CompletedTask);
        }
Exemple #21
0
        private async Task ExecuteAsyncImpl(IProducerConsumerContext <TItem> context)
        {
            try
            {
                OnStarted(context);

                try
                {
                    CreateConsumer(context);
                    await ConsumeAsync(context);
                }
                finally
                {
                    OnCompleted(context);
                }
            }
            finally
            {
                OnExit(context);
            }
        }
Exemple #22
0
 public Task ConsumeAsync(IProducerConsumerContext <object> context)
 {
     throw new System.NotImplementedException();
 }
 public void SetOverrideContext(IProducerConsumerContext <TItem> context)
 {
     overrideContext     = context;
     isContextOverridden = true;
 }
Exemple #24
0
 /// <summary>
 /// Creates a new worker.
 /// </summary>
 /// <param name="context">The contextual information about what was produced.</param>
 /// <returns>The worker used to work the produced item.</returns>
 protected virtual IWorker CreateWorker(IProducerConsumerContext <TItem> context)
 {
     return(pool.Get(() => OnConsume(context), null));
 }
 private void AssertContext(object item, DefaultProducerExecutionStrategy <object> target, IProducerConsumerContext <object> context)
 {
     Assert.AreEqual(item, context.Item);
     Assert.AreEqual(producer.Object, context.ProductionContext.Producer);
     Assert.AreEqual(scope.Object, context.LifetimeScope);
     Assert.AreEqual(target, context.ProductionContext.ExecutionStrategy);
     Assert.AreEqual(synchronizationLock.Object, context.SynchronizationLock);
 }
        protected override void CreateConsumer(IProducerConsumerContext <TItem> context)
        {
            var c = GetContext(context);

            base.CreateConsumer(c);
        }
Exemple #27
0
 /// <summary>
 /// Occurs when the strategy has completed execution.
 /// </summary>
 /// <param name="context">The contextual information about the item produced.</param>
 protected virtual void OnCompleted(IProducerConsumerContext <TItem> context)
 {
     ProcessingContext.Clear();
 }
Exemple #28
0
        public Task ConsumeAsync(IProducerConsumerContext <int> context)
        {
            Monitor.Increment();

            return(Task.CompletedTask);
        }
Exemple #29
0
 public Task ConsumeAsync(IProducerConsumerContext <TItem> context)
 {
     return(Task.CompletedTask);
 }
Exemple #30
0
        private void OnProducedCallback(IProducerConsumerContext <TItem> context)
        {
            context.Processor = this;

            consumerEngine.Consume(context);
        }