//-----------------------------------------------------------------------------------------------------------------------------------------------------

        private void ValidateStartedOrThrow()
        {
            try
            {
                _stdErr = new OutputReader(_process.StandardError);

                if (!WaitUntilMicroserviceIsUp(10000, out _stdOut))
                {
                    throw new TimeoutException("Microservice didn't start withen allotted timeout (10 sec).");
                }

                if (_process.HasExited)
                {
                    _process.ExitCode.Should().Be(0, "microservice exit code");
                }
            }
            catch (Exception e)
            {
                _exceptions = _exceptions.Add(e);

                try
                {
                    StopOrThrow(10000);
                }
                catch (Exception e2)
                {
                    _exceptions = _exceptions.Add(e2);
                }

                AssertNoErrors();
            }
        }
Example #2
0
			public static OutputReader Launch (StreamReader input, TextWriter output) {
				OutputReader reader = new OutputReader (StreamReader.Synchronized (input), 
									TextWriter.Synchronized (output));

				reader.thread.Start ();

				return reader;
			}
Example #3
0
            public static OutputReader Launch(StreamReader input, TextWriter output)
            {
                OutputReader reader = new OutputReader(StreamReader.Synchronized(input),
                                                       TextWriter.Synchronized(output));

                reader.thread.Start();

                return(reader);
            }
        /// <summary>
        /// Handles the DoWork event of the outputWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DoWorkEventArgs" /> instance containing the event data.</param>
        void OutputWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!OutputWorker.CancellationPending && OutputReader is not null)
            {
                //  Any lines to read?
                int count;
                var buffer = new char[1024];
                do
                {
                    var builder = new StringBuilder();
                    count = OutputReader.Read(buffer, 0, 1024);
                    builder.Append(buffer, 0, count);
                    OutputWorker.ReportProgress(0, builder.ToString());
                } while (count > 0);

                System.Threading.Thread.Sleep(200);
            }
        }
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private bool WaitUntilMicroserviceIsUp(int millisecondsTimeout, out OutputReader stdOut)
        {
            using (var signal = new ManualResetEvent(initialState: false))
            {
                stdOut = new OutputReader(_process.StandardOutput, lineCallback: signalOnMicroserviceUp);

                return(signal.WaitOne(millisecondsTimeout));

                bool signalOnMicroserviceUp(string line)
                {
                    if (line.ToLower().Contains("microservice is up"))
                    {
                        signal.Set();
                        return(false);
                    }
                    return(true);
                }
            }
        }
        public CommandOutput Decode(AnalyticsData analysisdata)
        {
            CommandOutput op = new CommandOutput();

            Paragraph Title = new Paragraph();

            OutputHelper.AnalyticsData = analysisdata;
            OutputReader reader = new OutputReader();
            if (analysisdata.Result.CommandString != null && analysisdata.Result.CommandString.Contains("UAloadDataset"))
            {
                reader.Hdr = "Open Dataset"; //21Oct2013
            }
            else
            {
                reader.Hdr = analysisdata.Result.CommandString;
            }
            List<DependencyObject> objs = reader.GetOutput(analysisdata.OutputTemplate);
            op.AddRange(objs);
            return op;
        }
Example #7
0
        public CommandOutput Decode(AnalyticsData analysisdata)
        {
            CommandOutput op = new CommandOutput();

            Paragraph Title = new Paragraph();

            OutputHelper.AnalyticsData = analysisdata;
            OutputReader reader = new OutputReader();

            if (analysisdata.Result.CommandString != null && analysisdata.Result.CommandString.Contains("UAloadDataset"))
            {
                reader.Hdr = "Open Dataset"; //21Oct2013
            }
            else
            {
                reader.Hdr = analysisdata.Result.CommandString;
            }
            List <DependencyObject> objs = reader.GetOutput(analysisdata.OutputTemplate);

            op.AddRange(objs);
            return(op);
        }
Example #8
0
        public static int Start(BinaryInfo info, string extra_args, TextReader stdin,
                                TextWriter stdout, TextWriter stderr, IBuildContext ctxt)
        {
            ProcessStartInfo si = PrepareInfo(info, extra_args, ctxt);

            string command = si.FileName + " " + si.Arguments;

            if (command.Length > 511)
            {
                ctxt.Logger.Warning(3002, "Command line is too long for Windows shell", command);
            }

            ctxt.Logger.Log("launcher.launch", command);
            Process ps = Process.Start(si);

            bool capture_stdout = (stdout == null);
            bool capture_stderr = (stderr == null);

            // Transfer data from our TextReader/Writers to the process
            // FIXME: Do we really have to spawn new threads? That totally bites!
            // (Before I had a loop that would read/write from std{in,out,err}, but I
            // would get locks as mbuild would want to write to one pipe while
            // the child (jay) wanted to write to another. Sigh.

            OutputReader stdout_reader = null;
            OutputReader stderr_reader = null;

            if (stdout != null)
            {
                stdout_reader = OutputReader.Launch(ps.StandardOutput, stdout);
            }
            if (stderr != null)
            {
                stderr_reader = OutputReader.Launch(ps.StandardError, stderr);
            }

            if (stdin == null)
            {
                ps.StandardInput.Close();
            }
            else
            {
                char[] buf = new char[512];
                int    read;

                do
                {
                    //Console.WriteLine ("* before stdin read");
                    read = stdin.Read(buf, 0, buf.Length);
                    //Console.WriteLine ("* transferring {0} chars to process stdin", read);

                    //System.Text.StringBuilder sb = new System.Text.StringBuilder ();
                    //sb.Append (buf, 0, read);
                    //Console.WriteLine ("[{0}]", sb.ToString ());

                    if (read > 0)
                    {
                        //Console.WriteLine ("* before stdin write");
                        ps.StandardInput.Write(buf, 0, read);
                        //Console.WriteLine ("* after stdin write");
                    }
                    else
                    {
                        ps.StandardInput.Close();
                        stdin = null;
                    }
                } while (read > 0);
            }

            ps.WaitForExit();

            if (stdout_reader != null)
            {
                stdout_reader.Join();
            }
            if (stderr_reader != null)
            {
                stderr_reader.Join();
            }

            // Cleanup -- if not dumping stdout and stderr to a file,
            // then log them

            string data;

            if (capture_stdout)
            {
                data = ps.StandardOutput.ReadToEnd().Trim();
                if (data.Length > 0)
                {
                    ctxt.Logger.Log("launcher.stdout", data);
                }
            }

            if (capture_stderr)
            {
                data = ps.StandardError.ReadToEnd().Trim();
                if (data.Length > 0)
                {
                    ctxt.Logger.Log("launcher.stderr", data);
                }
            }

            ctxt.Logger.Log("launcher.exit", ps.ExitCode.ToString());
            return(ps.ExitCode);
        }
Example #9
0
        public virtual void ToolThread()
        {
            Thread orThread = null;
            Thread or2Thread = null;
            ok = false;
            successfull = false;
            exitCode = -1;
            try
            {
                StartInfo();
                sb.Remove(0, sb.Length);
                
                pc = new Process();
                pc.StartInfo.FileName = Path;
                pc.StartInfo.Arguments = Parameter;

                Info("Command: " + Path + " " + Parameter);

                pc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                pc.StartInfo.UseShellExecute = false;
                pc.StartInfo.CreateNoWindow = true;
                pc.StartInfo.RedirectStandardOutput = true;
                pc.StartInfo.RedirectStandardError = true;

                if (!pc.Start())
                {
                    Info("Error starting " + Path);
                    return;
                }

                pc.PriorityClass = Priority;

                OutputReader or = new OutputReader(pc.StandardOutput, Log, sb);
                orThread = new Thread(or.Start);

                OutputReader or2 = new OutputReader(pc.StandardError, Log, sb);
                or2Thread = new Thread(or2.Start);

                orThread.Start();
                or2Thread.Start();

                orThread.Join();
                or2Thread.Join();

                pc.WaitForExit();
                Info("Return code: " + pc.ExitCode.ToString());
                exitCode = pc.ExitCode;
                pc.Close();
                pc = null;
                EndInfo();
                ok = true;
                if (StartProcessing())
                {                    
                    Process();
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                try
                {
                    if (orThread != null)
                    {
                        orThread.Abort();
                        orThread = null;
                    }
                }
                catch (Exception)
                {
                }
                try
                {
                    if (or2Thread != null)
                    {
                        or2Thread.Abort();
                        or2Thread = null;
                    }
                }
                catch (Exception)
                {
                }
            }
        }
Example #10
0
        static void Main(string[] args)
        {
            processArgs(args);
            if (veryVerbose)
            {
                Console.WriteLine("   Parsed command line:");
                foreach (string option in linkerOptions)
                {
                    Console.WriteLine("   - " + option);
                }
                Console.WriteLine("   Objects to link:");
                foreach (string obj in linkerObjects)
                {
                    Console.WriteLine("   - " + obj);
                }
            }

            // Find ilasm
            String ilasm = findIlasm();

            // Construct the command line
            String optionString = "";
            String objsString   = "";
            String responseFile = null;

            foreach (string option in linkerOptions)
            {
                optionString += option + " ";
            }
            foreach (string obj in linkerObjects)
            {
                objsString += "\"" + obj + "\" ";
            }

            if (ilasm.Length + optionString.Length + objsString.Length > 2080)
            {
                // The command line is too large. Let's concatenate the object files.
                // Note that we need to make sure that this response file needs to be created
                //  in the current directory, so that external resources are correctly fetched
                //  by ilasm.
                do
                {
                    responseFile = System.IO.Path.Combine(System.Environment.CurrentDirectory,
                                                          System.IO.Path.GetRandomFileName());
                    // Make sure that the extension has no 'meaning' for ilasm, such as .com or whatever
                    // windows meaningful extensions.
                    responseFile = System.IO.Path.ChangeExtension(responseFile, ".tmp");
                } while (File.Exists(responseFile));

                using (StreamWriter sw = new StreamWriter(responseFile))
                {
                    foreach (string obj in linkerObjects)
                    {
                        using (StreamReader sr = new StreamReader(obj))
                        {
                            String line;
                            while ((line = sr.ReadLine()) != null)
                            {
                                sw.WriteLine(line);
                            }
                        }
                    }
                }

                objsString = "\"" + responseFile + "\"";
            }

            if (verbose)
            {
                Console.WriteLine("Current directory: " + Environment.CurrentDirectory);
                Console.WriteLine(ilasm + " " + optionString + objsString);
            }

            ProcessStartInfo startInfo = new ProcessStartInfo(ilasm, optionString + objsString);

            startInfo.CreateNoWindow         = true;
            startInfo.ErrorDialog            = false;
            startInfo.LoadUserProfile        = false;
            startInfo.UseShellExecute        = false;
            startInfo.WorkingDirectory       = Environment.CurrentDirectory;
            startInfo.RedirectStandardError  = true;
            startInfo.RedirectStandardOutput = true;

            Process      proc   = new Process();
            OutputReader output = new OutputReader();

            proc.StartInfo = startInfo;

            proc.OutputDataReceived += new DataReceivedEventHandler(output.outputHandler);
            proc.ErrorDataReceived  += new DataReceivedEventHandler(output.outputHandler);

            proc.Start();
            proc.BeginOutputReadLine();
            proc.BeginErrorReadLine();
            proc.WaitForExit();

            if (responseFile != null)
            {
                File.Delete(responseFile);
            }

            if (proc.ExitCode != 0)
            {
                Console.Error.WriteLine("error when calling ilasm");
                Console.Error.WriteLine(output.output);
                Environment.Exit(1);
            }
        }
Example #11
0
        public virtual void ToolThread()
        {
            Thread orThread  = null;
            Thread or2Thread = null;

            ok          = false;
            successfull = false;
            exitCode    = -1;
            try
            {
                StartInfo();
                sb.Remove(0, sb.Length);

                pc = new Process();
                pc.StartInfo.FileName  = Path;
                pc.StartInfo.Arguments = Parameter;

                Info("Command: " + Path + " " + Parameter);

                pc.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                pc.StartInfo.UseShellExecute        = false;
                pc.StartInfo.CreateNoWindow         = true;
                pc.StartInfo.RedirectStandardOutput = true;
                pc.StartInfo.RedirectStandardError  = true;

                if (!pc.Start())
                {
                    Info("Error starting " + Path);
                    return;
                }

                pc.PriorityClass = Priority;

                OutputReader or = new OutputReader(pc.StandardOutput, Log, sb);
                orThread = new Thread(or.Start);

                OutputReader or2 = new OutputReader(pc.StandardError, Log, sb);
                or2Thread = new Thread(or2.Start);

                orThread.Start();
                or2Thread.Start();

                orThread.Join();
                or2Thread.Join();

                pc.WaitForExit();
                Info("Return code: " + pc.ExitCode.ToString());
                exitCode = pc.ExitCode;
                pc.Close();
                pc = null;
                EndInfo();
                ok = true;
                if (StartProcessing())
                {
                    Process();
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                try
                {
                    if (orThread != null)
                    {
                        orThread.Abort();
                        orThread = null;
                    }
                }
                catch (Exception)
                {
                }
                try
                {
                    if (or2Thread != null)
                    {
                        or2Thread.Abort();
                        or2Thread = null;
                    }
                }
                catch (Exception)
                {
                }
            }
        }