Example #1
0
        public async Task <string> ExecuteAsync(string executionId, RecipeDescriptor recipeDescriptor)
        {
            await _eventBus.NotifyAsync <IRecipeEventHandler>(x => x.RecipeExecutingAsync(executionId, recipeDescriptor));

            try
            {
                using (var stream = _fileSystem.GetFileInfo(recipeDescriptor.Location).CreateReadStream())
                {
                    RecipeResult result = new RecipeResult {
                        ExecutionId = executionId
                    };
                    List <RecipeStepResult> stepResults = new List <RecipeStepResult>();

                    await _recipeParser.ProcessRecipeAsync(stream, (recipe, recipeStep) =>
                    {
                        // TODO, create Result prior to run
                        stepResults.Add(new RecipeStepResult
                        {
                            ExecutionId = executionId,
                            RecipeName  = recipeDescriptor.Name,
                            StepId      = recipeStep.Id,
                            StepName    = recipeStep.Name
                        });
                        return(Task.CompletedTask);
                    });

                    result.Steps = stepResults;
                    _session.Save(result);
                }

                using (var stream = _fileSystem.GetFileInfo(recipeDescriptor.Location).CreateReadStream())
                {
                    await _recipeParser.ProcessRecipeAsync(stream, async (recipe, recipeStep) =>
                    {
                        var shellContext = _orchardHost.GetOrCreateShellContext(_shellSettings);
                        using (var scope = shellContext.CreateServiceScope())
                        {
                            if (!shellContext.IsActivated)
                            {
                                var eventBus = scope.ServiceProvider.GetService <IEventBus>();
                                await eventBus.NotifyAsync <IOrchardShellEvents>(x => x.ActivatingAsync());
                                await eventBus.NotifyAsync <IOrchardShellEvents>(x => x.ActivatedAsync());

                                shellContext.IsActivated = true;
                            }

                            var recipeStepExecutor = scope.ServiceProvider.GetRequiredService <IRecipeStepExecutor>();

                            if (_applicationLifetime.ApplicationStopping.IsCancellationRequested)
                            {
                                throw new OrchardException(T["Recipe cancelled, application is restarting"]);
                            }

                            await recipeStepExecutor.ExecuteAsync(executionId, recipeStep);
                        }

                        // The recipe execution might have invalidated the shell by enabling new features,
                        // so the deferred tasks need to run on an updated shell context if necessary.
                        shellContext = _orchardHost.GetOrCreateShellContext(_shellSettings);
                        using (var scope = shellContext.CreateServiceScope())
                        {
                            var deferredTaskEngine = scope.ServiceProvider.GetService <IDeferredTaskEngine>();

                            // The recipe might have added some deferred tasks to process
                            if (deferredTaskEngine != null && deferredTaskEngine.HasPendingTasks)
                            {
                                var taskContext = new DeferredTaskContext(scope.ServiceProvider);
                                await deferredTaskEngine.ExecuteTasksAsync(taskContext);
                            }
                        }
                    });
                }

                await _eventBus.NotifyAsync <IRecipeEventHandler>(x => x.RecipeExecutedAsync(executionId, recipeDescriptor));

                return(executionId);
            }
            catch (Exception)
            {
                await _eventBus.NotifyAsync <IRecipeEventHandler>(x => x.ExecutionFailedAsync(executionId, recipeDescriptor));

                throw;
            }
        }