Пример #1
0
        public async Task RunAsync(Stock stock)
        {
            _reportingTimer?.Start();

            var prepareStep  = new TransformBlock <Recipe, Cake>(async recipe => await PrepareCakeAsync(recipe), _prepareOptions);
            var cookStep     = new TransformBlock <Cake, Cake>(async cake => await CookCakeAsync(cake), _cookOptions);
            var packageStep  = new TransformBlock <Cake, Cake>(async cake => await PackageCakeAsync(cake), _packageOptions);
            var deliveryStep = new ActionBlock <Cake>(async cake => await DeliveryCakeAsync(cake));

            prepareStep.LinkTo(cookStep, _linkOptions);
            cookStep.LinkTo(packageStep, _linkOptions);
            packageStep.LinkTo(deliveryStep, _linkOptions);

            while (true)
            {
                var recipe = await stock.GetNextRecipeAsync();

                if (recipe == null)
                {
                    break;
                }

                await prepareStep.SendAsync(recipe);
            }

            prepareStep.Complete();
            await prepareStep.Completion;
            await cookStep.Completion;
            await packageStep.Completion;

            _reportingTimer?.Stop();
        }
Пример #2
0
        public async Task RunAsync(Stock stock)
        {
            _reportingTimer?.Start();

            //Step 1: Get the list of Ingredients to prepare the cake
            var prepareStep = new TransformBlock <Recipe, Cake>(async recipe =>
            {
                var creationDate = DateTime.Now;
                await Task.Delay(_durationSettingsModel.PrepareDuration);
                var cake = new Cake(recipe)
                {
                    CreationDate = creationDate
                };
                _cakes.Add(cake);
                return(cake);
            }, _prepareOptions);

            //Step 2: Cook the prepared Cake
            var cookStep = new TransformBlock <Cake, Cake>(async cake =>
            {
                await Task.Delay(_durationSettingsModel.CookDuration);
                cake.Status = CakeStatus.Cooked;
                return(cake);
            }, _cookOptions);

            //Step 3: package the cooked Cake
            var packageStep = new TransformBlock <Cake, Cake>(async cake =>
            {
                await Task.Delay(_durationSettingsModel.PackageDuration);
                cake.Status = CakeStatus.Packaged;
                return(cake);
            }, _packageOptions);

            //Step 4: End of pipeline
            var deliveryStep = new ActionBlock <Cake>(async cake =>
            {
                await Task.Delay(_durationSettingsModel.DeliveryDuration);
                cake.Status       = CakeStatus.Delivered;
                cake.DeliveryDate = DateTime.Now;
            });

            var _linkOptions = new DataflowLinkOptions {
                PropagateCompletion = true
            };

            prepareStep.LinkTo(cookStep, _linkOptions);
            cookStep.LinkTo(packageStep, _linkOptions);
            packageStep.LinkTo(deliveryStep, _linkOptions);

            while (true)
            {
                var recipe = await stock.GetNextRecipeAsync();

                if (recipe == null)
                {
                    break;
                }

                await prepareStep.SendAsync(recipe);
            }

            // Mark the head of the pipeline as complete.
            prepareStep.Complete();

            // Wait for the last block in the pipeline to process all messages.
            deliveryStep.Completion.Wait();
            _reportingTimer?.Stop();
        }