protected override void OnExecute(ExecutableContext context, IFile file) { context.Log($"Executing SQL script at '{file.FullPath}'..."); string sql = FileStorage.Read(file.FullPath); using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { command.CommandType = _commandType; command.CommandTimeout = context.Timeout; command.AddParameters(context.Parameters); context.Log($"Parameter Arguments: {context.Parameters}"); var result = command.ExecuteNonQuery(); context.Log($"SQL command executed. Number of records affected: {result}."); } conn.Close(); } }
public static void LogStartupHeading(this ExecutableContext context, string executableName) { string headingMessage = $"EXECUTABLE - {executableName}"; context.Log(LogFormatter.GenerateHeadingLineSeparator(headingMessage)); context.Log(headingMessage); context.Log(LogFormatter.GenerateHeadingLineSeparator(headingMessage)); }
private void LogOutput(string output, ExecutableContext context) { context.Log($"Plugin output:"); string divider = $"----- PLUGIN {_typeName} -----"; context.Log(divider); context.Log(output?.Trim()); context.Log(new string('-', divider.Length)); }
public void Log_ThrowsException_WhenSuppliedLogActionIsNull() { var context = new ExecutableContext("integrationDirectory", "executablesDirectory", parameters: null, timeout: 30, logAction: null); var exception = Record.Exception(() => context.Log("my_message")); Assert.NotNull(exception); Assert.IsType <ArgumentNullException>(exception); }
protected override void OnExecute(ExecutableContext context, IFile file) { context.Log($"Executing Powershell script at '{file.FullPath}'..."); using (PowerShell instance = PowerShell.Create()) { instance.AddCommand(file.FullPath); instance.AddParameters(context.Parameters); context.Log($"Parameter Arguments: {context.Parameters}"); var success = instance.Execute(out string result); context.Log(result); if (!success) { throw new ApplicationException($"PowerShell script '{File}' failed execution. See output in logs for details."); } } }
protected override void OnExecute(ExecutableContext context, IFile file) { context.Log($"Executing batch file at '{file.FullPath}'..."); var processInfo = new ProcessStartInfo("cmd.exe") { CreateNoWindow = true, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, Arguments = BuildArguments(file.FullPath, context) }; ExecuteProcess(processInfo, context); }
public void Log_CallsLogAction_WhenSuppliedLogActionIsValid() { string testingChangeValue = "not_my_message"; Action <string> logAction = (msg) => { testingChangeValue = msg; return; }; var context = new ExecutableContext("integrationDirectory", "executablesDirectory", parameters: null, timeout: 30, logAction: logAction); context.Log("my_message"); Assert.Equal("my_message", testingChangeValue); }
public void Execute(ExecutableContext context) { Guard.IsNotNull(context, nameof(context)); context.LogStartupHeading(Name); if (context.Timeout > 0) { var task = Task.Run(() => OnExecute(context)); if (!task.Wait(TimeSpan.FromSeconds(context.Timeout))) { throw new TimeoutException($"Executable {Name} failed to finish executing within allotted timeout of {context.Timeout} seconds."); } } else { OnExecute(context); } context.Log($"** Executable {Name} completed. **"); }
private void ExecuteProcess(ProcessStartInfo processStartInfo, ExecutableContext context) { context.Log("Starting new process for Plugin Executor..."); context.Log($"Arguments: {processStartInfo.Arguments}"); using (var process = System.Diagnostics.Process.Start(processStartInfo)) { string output = process.StandardOutput.ReadToEnd(); if (!process.WaitForExit(context.Timeout)) { try { process.Kill(); } catch (Exception ex) { context.Log($"Killing process '{process?.Id}' FAILED. Exception: {ex}"); } finally { throw new TimeoutException($"Plugin Executor process failed to process plugin '{TypeFullName}' in allotted timeout of {context.Timeout} seconds."); } } else { if (process.HasExited) { context.Log($"Plugin Executor process finished. ExitCode: {process.ExitCode}"); } else { context.Log($"Plugin Executor process finished. NOT EXITED."); } process.Close(); } LogOutput(output, context); if (!string.IsNullOrWhiteSpace(output) && output.StartsWith(LogFormatter.FailedResultCode)) { throw new ApplicationException($"CSharp Plugin '{TypeFullName}' failed execution. See plugin output in logs for details."); } } context.Log("Closed Plugin Executor process."); }
private void ExecuteProcess(ProcessStartInfo processStartInfo, ExecutableContext context) { context.Log($"Starting new process..."); context.Log($"Arguments: {processStartInfo.Arguments}"); using (var process = System.Diagnostics.Process.Start(processStartInfo)) { process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => context.Log($"Batch file '{File}' OUTPUT => {e.Data}"); process.BeginOutputReadLine(); process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => context.Log($"Batch file '{File}' ERROR => {e.Data}"); process.BeginErrorReadLine(); if (!process.WaitForExit(context.Timeout)) { try { process.Kill(); } catch (Exception ex) { context.Log($"Killing process '{process?.Id}' FAILED. Exception: {ex}"); } finally { throw new TimeoutException($"Batch file '{File}' failed to execute in allotted timeout of {context.Timeout} seconds."); } } else { if (process.HasExited) { context.Log($"Batch file '{File}' ExitCode: {process.ExitCode}"); } else { context.Log($"Batch file '{File}' NOT EXITED."); } process.Close(); } context.Log($"Windows process for batch file '{File}' complete."); } }