public Task Execute(IJobExecutionContext context) { Guid processId = Guid.Empty; try { processId = Guid.Parse(context.JobDetail.Key.Name); if (processManagerService.IsRunning(processId)) { bool result = processManagerService.Restart(processId); if (result) { processOutputService.AddInternalOutput(processId, $"Process restarted with schedule."); } } } catch (Exception ex) { processOutputService.AddInternalOutput(processId, $"Failed to restart process with schedule. ({ex.GetType().Name}: {ex.Message})"); } return(Task.CompletedTask); }
public bool Start(Guid processId) { if (!managedProcesses.ContainsKey(processId)) { return(false); } ProcessListItem item = managedProcesses[processId]; ManagedProcess configuration = item.Configuration; if (item.Process != null) { if (!item.Process.HasExited) { return(false); } } else { ProcessStartInfo startInfo = new ProcessStartInfo(configuration.FileName); if (!string.IsNullOrWhiteSpace(configuration.Args)) { startInfo.Arguments = configuration.Args; } if (!string.IsNullOrWhiteSpace(configuration.WorkingDirectory)) { startInfo.WorkingDirectory = configuration.WorkingDirectory; } if (!string.IsNullOrWhiteSpace(configuration.Username) && !string.IsNullOrWhiteSpace(configuration.Password)) { startInfo.UserName = configuration.Username; startInfo.PasswordInClearText = configuration.Password; } startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.StandardErrorEncoding = Encoding.UTF8; startInfo.StandardInputEncoding = Encoding.UTF8; startInfo.StandardOutputEncoding = Encoding.UTF8; startInfo.RedirectStandardError = true; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; Process startedProcess = new Process(); startedProcess.StartInfo = startInfo; startedProcess.OutputDataReceived += (source, ea) => processOutputService.AddStandardOutput(configuration.ID, ea.Data); startedProcess.ErrorDataReceived += (source, ea) => processOutputService.AddErrorOutput(configuration.ID, ea.Data); startedProcess.EnableRaisingEvents = true; startedProcess.Exited += (source, ea) => StartedProcess_Exited(item, ea); item.Process = startedProcess; } bool result; try { result = item.Process.Start(); if (result) { item.Process.StandardInput.AutoFlush = true; item.Process.BeginErrorReadLine(); item.Process.BeginOutputReadLine(); } } catch (Exception ex) { result = false; processOutputService.AddInternalOutput(configuration.ID, $"Process failed to start with exception: {ex.Message} ({ex.GetType().FullName})"); } return(result); }