void RestartProcess(Process proc) { Logger.Log(string.Format("Trying to restart application {0}", proc.StartInfo.FileName)); BackOffHandler.WaitAppropriateAmountOfTime(proc.StartInfo); if (ShouldProcessBeRestarted(proc)) { // It seems like we cannot capture output to the stdout or stderr when we restart the process (we get no events), // and calling BeginOutputReadLine fails, so... we create a new process object altogether. processes.Remove(proc); var app = GetApplication(proc); proc = new Process { StartInfo = proc.StartInfo }; ConfigureProcess(proc, app); processes.Add(proc); proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); BackOffHandler.RegisterProcessStart(proc.StartInfo); Logger.Log(string.Format("Application {0} restarted with PID {1}", proc.StartInfo.FileName, proc.Id)); } }
void StartApplication(Application app) { var proc = new Process(); processes.Add(proc); proc.StartInfo.FileName = app.FileName; proc.StartInfo.Arguments = string.Join(" ", app.Arguments.ToArray()); //proc.StartInfo.CreateNoWindow = true; foreach (var env in app.EnvironmentVariables) { Logger.Log(String.Format("Adding environment variable {0} with value {1}", env.Key, env.Value)); proc.StartInfo.EnvironmentVariables[env.Key] = env.Value; } proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; ConfigureProcess(proc, app); Logger.Log(string.Format("Starting application {0} with arguments {1}", app.FileName, string.Join(" ", app.Arguments.ToArray()))); try { proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); BackOffHandler.RegisterProcessStart(proc.StartInfo); Logger.Log(string.Format("Application {0} started with PID {1}", app.FileName, proc.Id)); } catch (Win32Exception e) { // FIXME: What is the correct action to take here? Logger.Log(string.Format("Application {0} failed to start: {1}. Terminating service.", app.FileName, e.Message)); throw new Exception("One or more applications failed to start. Service terminating.", e); } }