private async Task <BuildContentOutput> DoBuildContentForBuildRequestAsync(BuildRequest buildRequest)
        {
            Message message = buildRequest.SharedObject.GetValue <Message>(Constants.BuildMessage);

            // add message priority into AmbientContext
            SetMessagePriorityInAmbientContext(message);

            TraceEx.TraceInformation(
                string.Format(
                    "Build content for request Action: {0}, EntityIdentifier: {1}, Message: {2}",
                    buildRequest.ActionName,
                    buildRequest.EntityIdentifier,
                    Utility.ToJsonString(message)));

            string pipelineName = BuildActionMapper.GetPipelineName(buildRequest.ActionName);

            if (string.IsNullOrEmpty(pipelineName))
            {
                string errorMessage = LogMessage.FormatMessage(LogCode.PipelineForActionNotFound, buildRequest.ActionName);
                TraceEx.TraceError(errorMessage);
                throw new ArgumentException(errorMessage);
            }

            BuildEngine  engine;
            BuildContext context;

            using (new PerformanceScopeWrapper(
                       "Setup Context",
                       PerformanceCategory.Pipeline))
            {
                string reportPipelineName = GetReportPipelineName(buildRequest.ActionName, message.MessageType);
                engine  = BuildEngineFactory.CreateEngineByPipelineName(reportPipelineName);
                context = engine.Context;

                context.XmlContext.AddVariable(Constants.ContextData.PipelineName, pipelineName);
                await SetupContextAsync(context, buildRequest);

                context.SetSharedObject(Constants.ContextData.PipelineStartTime, DateTime.UtcNow);
            }

            PipelineResult result = PipelineResult.Break;

            using (new PerformanceScopeWrapper(
                       string.Format("Setup Pipeline: {0}", buildRequest.ActionName),
                       PerformanceCategory.Pipeline))
            {
                engine.Pipeline.Setup(context);
            }

            XmlDocument document = new XmlDocument();

            result = await engine.Pipeline.ApplyAsync(document, context);

            return(new BuildContentOutput
            {
                BuildContentResult = result == PipelineResult.NeedRerun ? BuildContentResult.NeedRerun : BuildContentResult.Succeeded,
                Outputs = context.Outputs
            });
        }
 private EngineExecutor()
 {
     try
     {
         // environment variable to be used in the pipeline transform component
         Environment.SetEnvironmentVariable(Constants.BuildSetupFilePath, CommonServiceConfiguration.BuildSetupFilesUrl);
         Environment.SetEnvironmentVariable(Constants.ExecutableDir, CommonServiceConfiguration.ExecutableDir);
     }
     catch (Exception e)
     {
         TraceEx.TraceError(LogMessage.FormatMessage(LogCode.EngineExecutorInitError, e.Message));
         throw;
     }
 }
Ejemplo n.º 3
0
        public static async void RunTaskSilentlyNoWait(Task task, [CallerMemberName] string taskDescription = null)
        {
            Guard.ArgumentNotNull(task, nameof(task));

            try
            {
                await task;
                TraceEx.TraceInformation($"{taskDescription} succeeded.");
            }
            catch (Exception ex)
            {
                TraceEx.TraceError($"{taskDescription} failed: {ex}");
            }
        }
Ejemplo n.º 4
0
        public static async Task <bool> CompleteTaskWithinTimeOutAsync(Task task, int timeoutInSeconds, string taskDescription, Func <Exception> exceptionGeneratorOnTimeout)
        {
            Guard.ArgumentNotNull(task, nameof(task));
            Guard.ArgumentNotNull(exceptionGeneratorOnTimeout, nameof(exceptionGeneratorOnTimeout));

            if (timeoutInSeconds <= 0 || await RunTaskWithTimeoutAsync(task, TimeSpan.FromSeconds(timeoutInSeconds), taskDescription))
            {
                await task;
                return(true);
            }

            TraceEx.TraceError($"{taskDescription} timeout in {timeoutInSeconds} seconds.");
            throw exceptionGeneratorOnTimeout();
        }
Ejemplo n.º 5
0
        // The same behavior as "RunTaskSilentlyNoWait".
        // but it is bad because it returns Task,
        // and the caller has chance to wait "RunTaskSilentlyNoWaitBad" to complete.
        public static async Task RunTaskSilentlyNoWaitBad(Task task, string taskDescription = null)
        {
            Guard.ArgumentNotNull(task, nameof(task));

            try
            {
                Console.WriteLine(task.Status + ": RunTaskSilentlyNoWait1");
                await task;
                Console.WriteLine(task.Status + " : RunTaskSilentlyNoWait1");
            }
            catch (Exception ex)
            {
                TraceEx.TraceError(taskDescription != null
                    ? $"{taskDescription} failed: {ex}"
                    : $"Running task failed: {ex}");
            }
        }
Ejemplo n.º 6
0
        // The same as RunTaskSilentlyNoWait.
        public static async void RunTaskSilentlyNoWaitUsingFunc(Func <Task> task, string taskDescription = null)
        {
            Guard.ArgumentNotNull(task, nameof(task));

            try
            {
                Task t = task();
                Console.WriteLine(t.Status);
                await t;
                Console.WriteLine(t.Status);
            }
            catch (Exception ex)
            {
                TraceEx.TraceError(taskDescription != null
                    ? $"{taskDescription} failed: {ex}"
                    : $"Running task failed: {ex}");
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Execute exe
        /// </summary>
        /// <param name="executionInfo">execution info</param>
        /// <param name="stdoutWriter">stdout stream writer, null to ignore stdout</param>
        /// <param name="stderrWriter">stderr stream writer, null to ignore stderr</param>
        /// <param name="timeoutInMilliseconds">timeout in milliseconds, -1 to wait infinitely</param>
        /// <returns>return value of the exe</returns>
        public static int ExecuteExe(ProcessExecutionInfo executionInfo, StreamWriter stdoutWriter, StreamWriter stderrWriter, int timeoutInMilliseconds = Timeout.Infinite)
        {
            Guard.ArgumentNotNull(executionInfo, nameof(executionInfo));
            Guard.ArgumentNotNullOrEmpty(executionInfo.ExeFilePath, $"{nameof(executionInfo)}.{nameof(executionInfo.ExeFilePath)}");
            if (timeoutInMilliseconds < 0 && timeoutInMilliseconds != Timeout.Infinite)
            {
                throw new ArgumentOutOfRangeException(nameof(timeoutInMilliseconds), $"{nameof(timeoutInMilliseconds)} must be equal to or greater than 0, or equal to {Timeout.Infinite}.");
            }

            using (Process buildProcess = new Process())
            {
                buildProcess.StartInfo.FileName         = Environment.ExpandEnvironmentVariables(executionInfo.ExeFilePath);
                buildProcess.StartInfo.UseShellExecute  = false;
                buildProcess.StartInfo.CreateNoWindow   = true;
                buildProcess.StartInfo.Arguments        = executionInfo.Arguments;
                buildProcess.StartInfo.WorkingDirectory = executionInfo.WorkingDirectory == null ? null : Environment.ExpandEnvironmentVariables(executionInfo.WorkingDirectory);

                // set TEMP/TMP variables
                buildProcess.StartInfo.EnvironmentVariables["TEMP"] = Environment.GetEnvironmentVariable("TEMP");
                buildProcess.StartInfo.EnvironmentVariables["TMP"]  = Environment.GetEnvironmentVariable("TMP");
                buildProcess.StartInfo.EnvironmentVariables["PATH"] = Environment.GetEnvironmentVariable("PATH");

                buildProcess.StartInfo.RedirectStandardOutput = true;
                buildProcess.StartInfo.StandardOutputEncoding = Encoding.UTF8;
                buildProcess.StartInfo.RedirectStandardError  = true;
                buildProcess.StartInfo.StandardErrorEncoding  = Encoding.UTF8;

                buildProcess.Start();

                var outputTask = Task.Factory.StartNew(() =>
                {
                    var buffer = new char[512];
                    while (true)
                    {
                        var read = buildProcess.StandardOutput.Read(buffer, 0, 512);
                        if (read > 0)
                        {
                            try
                            {
                                stdoutWriter?.Write(buffer, 0, read);
                            }
                            catch (Exception ex)
                            {
                                TraceEx.TraceError($"Unable to write standard output, details: {ex}");
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }, TaskCreationOptions.LongRunning);

                var errorTask = Task.Factory.StartNew(() =>
                {
                    var buffer = new char[512];
                    while (true)
                    {
                        var read = buildProcess.StandardError.Read(buffer, 0, 512);
                        if (read > 0)
                        {
                            try
                            {
                                stderrWriter?.Write(buffer, 0, read);
                            }
                            catch (Exception ex)
                            {
                                TraceEx.TraceError($"Unable to write error output, details: {ex}");
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }, TaskCreationOptions.LongRunning);

                try
                {
                    if (buildProcess.WaitForExit(timeoutInMilliseconds))
                    {
                        return(buildProcess.ExitCode);
                    }
                    else
                    {
                        buildProcess.Kill();
                        buildProcess.WaitForExit();
                        throw new TimeoutException($"{executionInfo.ExeFilePath} (argument: {executionInfo.Arguments}, working directory: {executionInfo.WorkingDirectory}) didn't exit in {timeoutInMilliseconds} ms. Force to exit.");
                    }
                }
                finally
                {
                    outputTask.Wait();
                    errorTask.Wait();
                }
            }
        }