/// <inheritdoc /> /// <summary> /// Execute the cli task async /// </summary> /// <returns>error code</returns> public Task <int> ExecuteAsync() { // Call WaitForExit and then the using statement will close. var startInfo = new ProcessStartInfo { CreateNoWindow = true, UseShellExecute = false, FileName = ExecutablePath, Arguments = CommandLineArguments }; var tcs = new TaskCompletionSource <int>(); try { Logger.WriteProcessToLog(Name, $"Executing '{ExecutablePath}' with '{CommandLineArguments}'"); var exeProcess = new Process { StartInfo = startInfo, EnableRaisingEvents = true }; RedirectStdOut(exeProcess.StartInfo); exeProcess.Start(); OutputRedirector stdOutToFile = null; OutputRedirector stdErrToFile = null; string timestamp = DateTime.UtcNow.ToString("yyyyMMdd-HHmmss"); if (startInfo.RedirectStandardOutput) { stdOutToFile = new OutputRedirector(OutputLogDir, $"{Name}-{exeProcess.Id}-{timestamp}.stdout"); exeProcess.OutputDataReceived += stdOutToFile.LineHandler; exeProcess.BeginOutputReadLine(); } if (startInfo.RedirectStandardError) { stdErrToFile = new OutputRedirector(ErrorLogDir, $"{Name}-{exeProcess.Id}-{timestamp}.stderr"); exeProcess.ErrorDataReceived += stdErrToFile.LineHandler; exeProcess.BeginErrorReadLine(); } exeProcess.Exited += (s, e) => { ExitCode = exeProcess.ExitCode; Logger.WriteProcessToLog(Name, $"ExitCode: {ExitCode}."); tcs.TrySetResult(ExitCode); stdOutToFile?.Dispose(); stdErrToFile?.Dispose(); exeProcess.Dispose(); }; } catch (Exception ex) { ExitCode = -1; tcs.TrySetResult(ExitCode); tcs.TrySetException(ex); Logger.WriteProcessToLog(Name, "the task is being terminated due to a failure."); Logger.WriteExceptionToLog(ex); } return(tcs.Task); }
/// <inheritdoc /> /// <summary> /// Execute the cli task /// </summary> /// <remarks>blocking call</remarks> public void Execute() { // Call WaitForExit and then the using statement will close. var startInfo = new ProcessStartInfo { CreateNoWindow = true, UseShellExecute = false, FileName = ExecutablePath, Arguments = CommandLineArguments }; Logger.WriteProcessToLog(Name, $"Executing '{ExecutablePath}' with '{CommandLineArguments}'"); using (var exeProcess = new Process { StartInfo = startInfo, EnableRaisingEvents = true }) { OutputRedirector stdOutToFile = null; OutputRedirector stdErrToFile = null; try { RedirectStdOut(exeProcess.StartInfo); exeProcess.Start(); string timestamp = DateTime.UtcNow.ToString("yyyyMMdd-HHmmss"); if (startInfo.RedirectStandardOutput) { stdOutToFile = new OutputRedirector(OutputLogDir, $"{Name}-{exeProcess.Id}-{timestamp}.stdout"); exeProcess.OutputDataReceived += stdOutToFile.LineHandler; exeProcess.BeginOutputReadLine(); } if (startInfo.RedirectStandardError) { stdErrToFile = new OutputRedirector(ErrorLogDir, $"{Name}-{exeProcess.Id}-{timestamp}.stderr"); exeProcess.ErrorDataReceived += stdErrToFile.LineHandler; exeProcess.BeginErrorReadLine(); } exeProcess.WaitForExit(); ExitCode = GetProcessExitCode(exeProcess); Logger.WriteProcessToLog(Name, $"ExitCode: {ExitCode}."); } catch (Exception ex) { ExitCode = -1; Logger.WriteProcessToLog(Name, "the task is being terminated due to a failure."); Logger.WriteExceptionToLog(ex); try { exeProcess?.Kill(); Terminated = true; } catch (Exception e) { Logger.WriteProcessToLog(Name, "failed to kill the process."); Logger.WriteExceptionToLog(e); } } finally { stdOutToFile?.Dispose(); stdErrToFile?.Dispose(); } } }