示例#1
0
        public async Task <AllBatchesResult> GenerateAll(
            GeneratorConfig config,
            CancellationToken ct = default)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            try
            {
                config.Validate();
            }
            catch (ValidationException ve)
            {
                _logger.Fatal(ve, "Cannot start generation, Config is invalid");
                throw;
            }

            config.ServicePointSettings?.ApplySettings();

            var batches = config.GetAllBatches(out var unqiueCount).ToList();

            var results = new List <AllGenerationsResult>(batches.Count);

            using (StopwatchLoggerFactory.ForceLogStartDispose(
                       _logger,
                       "Start generation all settings for all batches, " +
                       "Lunches count {count}, Config = {@config}",
                       "Generation for all batches completed",
                       Params.ToArray <object>(batches.Count, config)))
            {
                foreach (var batch in batches)
                {
                    results.Add(await Generate(config, batch, ct));
                }
            }
            return(new AllBatchesResult(results));
        }
        /// <summary>
        ///     Execute args (if any) and return value indicates to start datageneration or not (depending on args).
        /// </summary>
        /// <param name="args"></param>
        /// <returns>Specifies to start datageneration.</returns>
        public bool ExecuteArgs(IEnumerable <string> args)
        {
            if (args.IsNullOrEmpty())
            {
                return(true);
            }

            try
            {
                var result = new ArgsExecutor(args)
                             .Arg(Args.Help, ShowHelp, stopOnSuccess: true)
                             .Arg_ThrowNoValue(Args.Config, value => _config = new Lazy <GeneratorConfig>(() => GeneratorConfig.ReadConfig(value)))
                             //.Arg_ThrowNoValues(Args.Settings, values => Array.ForEach(values, v => Config.AddGenerationSettingsFromFile(v)))
                             .Arg(Args.PutEndpoint, PutEndpoint, () => PutEndpoint(EndpointDatagenFileName))
                             .Arg_ThrowNoValues(Args.GetEndpoint, GetAndSaveEndpoint)
                             .Arg(Args.Start, () => { }, res => res.StartGeneration = true)
                             .Default(WrongArgumentsSpecified)
                             .Result;

                if (result.StartGeneration.HasValue(out var startGeneration))
                {
                    return(startGeneration);
                }

                if (result.Stopped == true)
                {
                    return(false);
                }

                if (result.Executed.ContainsOnlyAnyOf(Args.Config, Args.Settings))
                {
                    return(true);
                }

                return(false);
            }
            catch (ArgsExecutionException aee)
            {
                WriteInfo(aee.Message, ConsoleColor.Red, aee);
            }
            catch (ArgsException ae)
            {
                WriteInfo(ae.Message, ConsoleColor.Red);
                ShowHelp();
            }
            catch (InvalidOperationException ioe)
            {
                WriteInfo(ioe.Message, ConsoleColor.Red, ioe);
            }
            catch (Exception e)
            {
                WriteInfo("Unexpected exception occurred.", ConsoleColor.Red, e);
            }
            return(false);
        }
        public static async Task Generate(GeneratorConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            using (var tokenSource = new CancellationTokenSource())
            {
                Console.CancelKeyPress += (s, e) =>
                {
                    if (!tokenSource.IsCancellationRequested)
                    {
                        tokenSource.Cancel();
                    }
                    e.Cancel = true;
                };
                try
                {
                    ConsoleExecutor.WriteInfo("Start all generations.", ConsoleColor.Cyan);
                    _logger.Info("Start all generations");

                    var result = await new GeneratorClient()
                                 .GenerateAll(config, tokenSource.Token)
                                 .ConfigureAwait(false);

                    LogLevel resultLevel;

                    if (result.AllSucceeded)
                    {
                        ConsoleExecutor.WriteInfo("All generations completed successfully.", ConsoleColor.Green);
                        _logger.Info("All generations completed successfully.");
                        resultLevel = LogLevel.Info;
                    }
                    else
                    {
                        resultLevel = LogLevel.Error;
                        if (result.AllFailed)
                        {
                            ConsoleExecutor.WriteInfo("All generations completed unsuccessfully.", ConsoleColor.Red);
                            _logger.Error("All generations completed unsuccessfully");
                        }
                        else
                        {
                            ConsoleExecutor.WriteInfo("Some generations completed unsuccessfully.", ConsoleColor.Yellow);
                            _logger.Warn("Some generations completed unsuccessfully");
                        }
                        var errorResults = result
                                           .GenerationResults
                                           .SelectMany(g => g.GenerationResults)
                                           .Where(g => !g.Success)
                                           .ToList();
                        foreach (var item in errorResults)
                        {
                            ConsoleExecutor.WriteInfo($"Generation {item.Id} - {item.GenerationType} failed.",
                                                      ConsoleColor.Red,
                                                      item.Exception.Message);
                        }
                        _logger.Warn("All results with errors: {@results}", errorResults);
                    }

                    var title = resultLevel == LogLevel.Info
                        ? "Data generation completed succesfully"
                        : "Data generation failed";
                    LogHelper.MailLogger.LogWithEventParams(
                        resultLevel,
                        "Config:\r\n{config}\r\n\r\nResult:\r\n{results}",
                        args: Params.ToArray(
                            JsonConvert.SerializeObject(config, Formatting.Indented),
                            JsonConvert.SerializeObject(result, Formatting.Indented)),
                        eventParams: Params.ToArray <(object, object)>(("ResultTitle", title)));
                }
                catch (ValidationException ve)
                {
                    ConsoleExecutor.WriteInfo("Validation failed.", ConsoleColor.Red, ve);
                    _logger.Fatal(ve, "Validation failed");
                }
                catch (OperationCanceledException oce)
                {
                    ConsoleExecutor.WriteInfo("Operation was canceled", ConsoleColor.Red, oce);
                    _logger.Error(oce, "Operation was canceled");
                }
            }
        }
示例#4
0
        protected async Task <AllGenerationsResult> Generate(
            GeneratorConfig config,
            BatchSettings batchSettings,
            CancellationToken ct = default)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (batchSettings == null)
            {
                throw new ArgumentNullException(nameof(batchSettings));
            }

            try
            {
                config.Validate();
                batchSettings.Validate();
            }
            catch (ValidationException ve)
            {
                _logger.Fatal(ve, "Cannot start generation, configuration is invalid");
                throw;
            }

            if (batchSettings.RestartIisBeforeBatch)
            {
                try
                {
                    using (StopwatchLoggerFactory.ForceLogDispose(_logger, LogLevel.Debug, "iisreset completed"))
                    {
                        IisManager.Instance.RestartIis();
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(e, "Cannot restart IIS. Perhaps application was batched without administrator rights");
                }
            }

            if (batchSettings.CollectGarbageBeforeBatch)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Memory before GC.Collect: {byte:N0}", GC.GetTotalMemory(false));
                    GC.Collect();
                    _logger.Debug("Memory after GC.Collect: {byte:N0}", GC.GetTotalMemory(true));
                }
                else
                {
                    GC.Collect();
                }
            }

            var generatationResults = new List <GenerationResult>();
            var batch = batchSettings.CompileBatch();

            bool stopProcessing = false;

            using (StopwatchLoggerFactory.ForceLogStartDispose(
                       _logger,
                       "Start generation all settings for batch, Settings count = {count}, Id = {id}",
                       "Generation all settings for batch completed, Settings count = {count}, Id = {id}",
                       Params.ToArray <object>(batch.AvailableCount, batchSettings.Id, batchSettings),
                       callback: time => LogHelper.ResultsLogger.Info("Batch, Time: {time} ({time-sec} sec)", time, time.TotalSeconds)))
            {
                foreach (var settings in batch)
                {
                    GenerationResult result;
                    try
                    {
                        ct.ThrowIfCancellationRequested();
                        if (settings is IBatchDependent bd)
                        {
                            bd.Inject(batch);
                        }

                        var runner = settings.GetGenerationRunner(config.ApiConnectionConfig);
                        config.SubscriptionManager?.SubscribeGenerationRunner(runner);
                        await runner.RunGeneration(ct).ConfigureAwait(false);

                        result = new GenerationResult(settings);
                    }
                    catch (OperationCanceledException oce)
                    {
                        _logger.Fatal(oce, "Operation was canceled");
                        throw;
                    }
                    catch (GenerationException ge)
                    {
                        _logger.Error(ge, "Generation failed");
                        result = new GenerationResult(settings, ge);
                        if (batchSettings.StopProcessingAtException)
                        {
                            stopProcessing = true;
                        }
                    }
                    catch (ValidationException ve)
                    {
                        _logger.Error(ve, "Generation not started because of invalid configuration");
                        result = new GenerationResult(settings, ve);
                    }
                    // this should not happen
                    catch (Exception e)
                    {
                        _logger.Fatal(e, "Generation failed with unexpected error");
                        result = new GenerationResult(settings, e);
                        if (batchSettings.StopProcessingAtException)
                        {
                            stopProcessing = true;
                        }
                    }
                    generatationResults.Add(result);
                    if (stopProcessing)
                    {
                        break;
                    }
                }
            }
            return(new AllGenerationsResult(generatationResults, stopProcessing));
        }