public ExecuteConsoleCommand(ConsoleCommandDto cmdDto, ConsoleCommandLogMgr logMgr, ConsoleCommandLogMgr errLogMgr, WorkingDirectoryMgr wrkDirectoryMgr) { if (cmdDto == null) { var msg = "Console Command Dto is NULL!"; var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 1, ErrorMessage = msg, ErrSourceMethod = "Constructor", LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw new ArgumentException(msg); } _executeCommand = cmdDto; _logMgr = logMgr; _errLogMgr = errLogMgr; _wrkDirectoryMgr = wrkDirectoryMgr; }
public void ExtractElementInfo(XmlTextReader reader, ref ConsoleCommandDto consoleCommand, ref ConsoleExecutorDto cmdExeDto) { if (reader.Name == "DefaultLogFileRetentionInDays") { cmdExeDto.AppLogRetentionInDays = _xmlHlpr.ExtractIntValue(reader, -1); return; } if (reader.Name == "DefaultCommandExeDirectory") { var dir = _xmlHlpr.ExtractStringValue(reader); cmdExeDto.SetDefaultCommandExeDirectory(dir); return; } if (reader.Name == "DefaultCommandOutputLogFilePathName") { var filePath = _xmlHlpr.ExtractStringValue(reader); cmdExeDto.SetDefaultCommandOutputLogFilePathName(filePath); } }
public static ConsoleCommandDto GetCopy1Job() { var cmdExeDto = GetConsoleExecutorDto(); var cmdDto = new ConsoleCommandDto(cmdExeDto) { CommandDisplayName = "Copy1", CommandType = ConsoleCommandType.Console, CommandOutputLogFilePathBaseName = cmdExeDto.DefaultCommandOutputLogFilePathName, ConsoleCommandExecutor = cmdExeDto.DefaultConsoleCommandExecutor, ConsoleCommandExeArguments = cmdExeDto.DefaultConsoleCommandExeArgs, CommandTimeOutInMinutes = 5.0M, ExecuteInDir = string.Empty, ExecutableTarget = "Copy", CommandToExecute = string.Empty, CommandModifier = string.Empty }; var arg1 = GetTestDirectory001().DirInfo.FullName + "\\*.*"; var arg2 = GetTestDirectory003().DirInfo.FullName + "\\"; cmdDto.CommandArguments = arg1 + " " + arg2; cmdDto.NormalizeCommandParameters(); return cmdDto; }
public void ExtractElementInfo(XmlTextReader reader, ref ConsoleCommandDto consoleCommand, ref ConsoleExecutorDto cmdExeDto) { var parmHlpr = new XmlParameterConverter(cmdExeDto); if (reader.Name == "CommandDisplayName") { consoleCommand.CommandDisplayName = _xmlHlpr.ExtractStringValue(reader); return; } if (reader.Name == "ConsoleCommandType") { consoleCommand.CommandType = _xmlHlpr.ExtractConsoleCommandType(reader); return; } if (reader.Name == "KillJobsRunOnExitCodeGreaterThan") { var userEntry = _xmlHlpr.ExtractStringValue(reader); int exitCodeLimit; if (string.IsNullOrWhiteSpace(userEntry) || !int.TryParse(userEntry, out exitCodeLimit)) { return; } consoleCommand.KillJobsRunOnExitCodeGreaterThan = exitCodeLimit; return; } if (reader.Name == "KillJobsRunOnExitCodeLessThan") { var userEntry = _xmlHlpr.ExtractStringValue(reader); int exitCodeLimit; if (string.IsNullOrWhiteSpace(userEntry) || !int.TryParse(userEntry, out exitCodeLimit)) { return; } consoleCommand.KillJobsRunOnExitCodeLessThan = exitCodeLimit; return; } if (reader.Name == "CommandOutputLogFilePathBaseName") { consoleCommand.CommandOutputLogFilePathBaseName = _xmlHlpr.ExtractStringValue(reader); return; } if (reader.Name == "CommandTimeOutInMinutes") { consoleCommand.CommandTimeOutInMinutes = _xmlHlpr.ExtractDecimalValue(reader, 0.0M); return; } if (reader.Name == "DefaultConsoleCommandExecutor") { consoleCommand.ConsoleCommandExecutor = parmHlpr.RunConversion(_xmlHlpr.ExtractStringValue(reader)); return; } if (reader.Name == "ConsoleCommandExeArguments") { consoleCommand.ConsoleCommandExeArguments = parmHlpr.RunConversion(_xmlHlpr.ExtractStringValue(reader)); return; } if (reader.Name == "ExecuteInDir") { consoleCommand.ExecuteInDir = _xmlHlpr.ExtractStringValue(reader); return; } if (reader.Name == "ExecutableTarget") { consoleCommand.ExecutableTarget = parmHlpr.RunConversion(_xmlHlpr.ExtractStringValue(reader)); return; } if (reader.Name == "CommandToExecute") { consoleCommand.CommandToExecute = parmHlpr.RunConversion(_xmlHlpr.ExtractStringValue(reader)); return; } if (reader.Name == "CommandModifier") { consoleCommand.CommandModifier = parmHlpr.RunConversion(_xmlHlpr.ExtractStringValue(reader)); return; } if (reader.Name == "CommandArguments") { consoleCommand.CommandArguments = parmHlpr.RunConversion(_xmlHlpr.ExtractStringValue(reader)); } }
private void ExecuteCommand(ConsoleCommandDto cmdDto) { var thisMethod = "ExecuteCommand()"; bool procStatus = false; var proc = new Process(); try { // No window will be displayed to the user proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // Commands are set on the following two lines proc.StartInfo.FileName = _executeCommand.ProcFileNameCommand; proc.StartInfo.Arguments = _executeCommand.ProcFileArguments; // Shell Execute must be false in order to // set the Working directory below proc.StartInfo.UseShellExecute = false; // Do not create the black window. proc.StartInfo.CreateNoWindow = true; /* startInfo.WorkingDirectory When the UseShellExecute property is false, gets or sets the working directory for the process to be started. When UseShellExecute is true, gets or sets the directory that contains the process to be started. */ proc.StartInfo.WorkingDirectory = _wrkDirectoryMgr.TargetWorkingDirectory.DirInfo.FullName; // The following commands are needed to redirect the standard output. // This means that it will be redirected to the Process.StandardOutput StreamReader. proc.StartInfo.RedirectStandardOutput = true; proc.OutputDataReceived += CmdOutputDataHandler; // The following commands are needed to redirect standard error output. proc.StartInfo.RedirectStandardError = true; proc.ErrorDataReceived += CmdErrorDataHandler; // Start Process proc.Start(); // Start the asynchronous read of the standard output stream. proc.BeginOutputReadLine(); // Start the asynchronous read of the standard // error stream. proc.BeginErrorReadLine(); procStatus = proc.WaitForExit(_executeCommand.CommandTimeOutInMiliseconds); _executeCommand.CommandExitCode = proc.ExitCode; _executeCommand.CommandExitTime = DateTime.Now; } catch (Exception ex) { var msg = "Console Command Execution Failed!"; var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 10, ErrorMessage = msg, ErrSourceMethod = thisMethod, ErrException = ex, CommandName = cmdDto.CommandDisplayName, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw new ArgumentException(msg); } finally { try { if (!procStatus) { proc.Kill(); } } catch { // ReSharper disable once EmptyStatement ; } proc.Close(); proc.Dispose(); // ReSharper disable once RedundantAssignment proc = null; } }
public static void WriteLogJobStartUpMessage(ConsoleCommandDto job, ConsoleExecutorDto consoleExecutor) { if (!IsLoggerConfigured) { return; } var jobName = job.CommandDisplayName; JobNumber = job.JobNumber; CurrentJobNo = "Starting Job No: " + JobNumber; if(!string.IsNullOrEmpty(jobName)) { CurrentJobName = "Job Name: " + jobName; } else { CurrentJobName = string.Empty; } IsFirstJobLogMsg = false; IsAnyLoggingActive = true; var banner = StringHelper.MakeSingleCharString('=', MaxBannerLen); var subbanner = StringHelper.MakeSingleCharString('-', MaxBannerLen); var now = job.CommandStartTime; var sb = new StringBuilder(); sb.Append(NewLine); sb.Append(banner + NewLine); sb.Append(banner + NewLine); var s = "CmdrX Assembly Version " + ExeAssemblyVersionNo; var cStr = StringHelper.CenterString(s, banner.Length); sb.Append(cStr); sb.Append(NewLine); cStr = StringHelper.CenterString(CurrentJobNo, banner.Length); sb.Append(cStr); sb.Append(NewLine); sb.Append(subbanner); sb.Append(NewLine); if(!string.IsNullOrWhiteSpace(CurrentJobName)) { cStr = StringHelper.CenterString(CurrentJobName, banner.Length); sb.Append(cStr); sb.Append(NewLine); } sb.Append($"Started Job: {now.ToLongDateString()} {now.ToLongTimeString()}" + NewLine); sb.Append(subbanner); sb.Append(NewLine); sb.Append("Process StartInfo FileName: " + job.ProcFileNameCommand); sb.Append(NewLine); var args = StringHelper.BreakLineAtIndex(StringHelper.RemoveCarriageReturns(job.ProcFileArguments), 65); sb.Append("Process StartInfo Arguments: "); if (args.Length > 0) { sb.Append(NewLine); for (int i = 0; i < args.Length; i++) { sb.Append(" " + args[i] + NewLine); } } else { sb.Append("<NO ARGUMENTS>" + NewLine); } sb.Append(subbanner + NewLine); WriteLog(LogLevel.MESSAGE, sb.ToString()); }
public static void WriteLogJobEndMessage(ConsoleCommandDto job, ConsoleExecutorDto consoleExecutor) { if (!IsLoggerConfigured) { return; } var banner = StringHelper.MakeSingleCharString('=', MaxBannerLen); var subbanner = StringHelper.MakeSingleCharString('-', MaxBannerLen); var startTime = job.CommandStartTime; var endTime = job.CommandExitTime; var ts = job.CommandElapsedTime; var sb = new StringBuilder(); sb.Append(NewLine); sb.Append(NewLine); sb.Append(subbanner + NewLine); var cStr = StringHelper.CenterString("Job Completed: " + job.CommandDisplayName, banner.Length); sb.Append(cStr); sb.Append(NewLine); sb.Append(subbanner + NewLine); sb.Append("Job Number: " + job.JobNumber + NewLine); sb.Append("Job Start Time: " + DateHelper.DateTimeToDayMilliseconds(startTime) + NewLine); sb.Append("Job End Time: " + DateHelper.DateTimeToDayMilliseconds(endTime) + NewLine); sb.Append("Job Elapsed Time: " + DateHelper.TimeSpanDetailToMiliseconds(ts) + NewLine); sb.Append("Job Exit Code: " + job.CommandExitCode + NewLine); sb.Append(banner); sb.Append(NewLine); sb.Append(banner); sb.Append(NewLine); WriteLog(LogLevel.MESSAGE, sb.ToString()); _logger.LogFlushStreamWriter(); }
private void ProcessElement(XmlTextReader reader) { if (reader.Name == "Commands") { return; } if (reader.Name == "CommandFileHeader") { _elementParser = new ParseCmdFileHeaderElements(XmlElementType.CommandsHeader); } if (reader.Name == "ExectuteCommand") { _currentConsoleCommand = new ConsoleCommandDto(_cmdExeDto); _elementParser = new ParseExecuteElements(XmlElementType.ExecuteCommand); return; } _elementParser?.ExtractElementInfo(reader, ref _currentConsoleCommand, ref _cmdExeDto); }