Beispiel #1
0
        public RecipeResult GetResult(string executionId) {
            var query =
                from record in _recipeStepResultRecordRepository.Table
                where record.ExecutionId == executionId
                select record;

            var records = query.ToArray();

            if (!records.Any())
                throw new Exception(String.Format("No records were found in the database for recipe execution ID {0}.", executionId));

            var result = new RecipeResult() {
                ExecutionId = executionId,
                Steps =
                    from record in records
                    select new RecipeStepResult {
                        RecipeName = record.RecipeName,
                        StepName = record.StepName,
                        IsCompleted = record.IsCompleted,
                        IsSuccessful = record.IsSuccessful,
                        ErrorMessage = record.ErrorMessage
                    }
            };

            return result;
        }
Beispiel #2
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);
                        }
                    });
                }

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

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

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

            try
            {
                var fileInfo = _fileSystem.GetFileInfo(recipeDescriptor.Location);

                var parsersForFileExtension = _recipeOptions
                    .RecipeFileExtensions
                    .Where(rfx => Path.GetExtension(rfx.Key) == Path.GetExtension(fileInfo.PhysicalPath));

                using (var stream = fileInfo.CreateReadStream())
                {
                    RecipeResult result = new RecipeResult { ExecutionId = executionId };
                    List<RecipeStepResult> stepResults = new List<RecipeStepResult>();

                    foreach (var parserForFileExtension in parsersForFileExtension)
                    {
                        var recipeParser = _recipeParsers.First(x => x.GetType() == parserForFileExtension.Value);

                        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 = fileInfo.CreateReadStream())
                {
                    foreach (var parserForFileExtension in parsersForFileExtension)
                    {
                        var recipeParser = _recipeParsers.First(x => x.GetType() == parserForFileExtension.Value);

                        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;
            }
        }