public void Resume() { if (this.State == RunState.Paused) { this.State = RunState.Starting; this.logger.Log(0, "Resume: Called"); this.running = false; //tells the monitor to stop while (this.monitoring) { Thread.Sleep(1); //wait for the monitor to stop } foreach (KeyValuePair <string, HostCommunication> name in this.comms) { HostCommunication comm = name.Value; HostMessage response = this.SendMessage(this.procs[name.Key], comm, HostCommand.Resume, null); if (response != null && response.Command == HostCommand.Resume) { if (response.Message.StartsWith("success")) { this.logger.Log(0, "Resume: started " + name.Key); } else { this.logger.Log(100, "Resume: failed resuming " + name.Key); } } } this.State = RunState.Running; } }
public void Pause() { if (this.State == RunState.Running) { this.State = RunState.Stopping; this.logger.Log(0, "Pause: Called"); foreach (KeyValuePair <string, HostCommunication> name in this.comms) { HostCommunication comm = name.Value; HostMessage response = this.SendMessage(this.procs[name.Key], comm, HostCommand.Pause, null); if (response != null && response.Command == HostCommand.Pause) { if (response.Message.StartsWith("success")) { this.logger.Log(0, "Pause: started " + name.Key); } else { this.logger.Log(100, "Pause: failed pausing " + name.Key); } } } this.State = RunState.Paused; } }
public void Send(HostMessage message) { if (message == null) { return; } this.outStream.WriteLine(message.Format()); }
public HostMessage SendReceive(HostMessage message) { if (message == null) { return(null); } this.Send(message); return(this.Receive()); }
private void MonitorServices() { this.monitoring = true; this.running = true; string success = Osrs.Runtime.RunState.Running.ToString(); string failed = Osrs.Runtime.RunState.FailedRunning.ToString(); string starting = Osrs.Runtime.RunState.Starting.ToString(); string stopping = Osrs.Runtime.RunState.Stopping.ToString(); while (this.running) { //make a copy of the list so we're sure we can mutate the lists for restart foreach (string nam in this.processNames) { HostCommunication comm = this.comms[nam]; HostMessage response = this.SendMessage(this.procs[nam], comm, HostCommand.HeartBeat, null); if (response != null && response.Command == HostCommand.HeartBeat) { if (!response.Message.StartsWith(Osrs.Runtime.RunState.Running.ToString())) { if (response.Message.StartsWith(starting) || response.Message.StartsWith(stopping)) { continue; //may need to deal with hangs later } if (response.Message.StartsWith(failed)) { this.logger.Log(0, "Monitor: problem " + response.Message + ": " + nam); if (this.RestartProcess(nam)) { this.logger.Log(0, "Monitor: successfully restarted: " + nam); } else { this.logger.Log(0, "Monitor: failed to restart: " + nam); } } else { this.logger.Log(0, "Monitor: problem (ignored)" + response.Message + ": " + nam); } } } if (!this.running) //get us out ASAP { break; } } for (int i = 0; i < 10; i++) { if (this.running) //get us out ASAP { Thread.Sleep(500); //5 seconds in total } } } this.monitoring = false; }
private bool RestartProcess(string fileName) { try { Process p = this.procs[fileName]; if (p != null && !p.HasExited) { p.Kill(); } p = CreateProcess(fileName); if (p.Start()) { HostCommunication comm = new HostCommunication(p.StandardOutput, p.StandardInput); //we need to also init this thing HostMessage response = this.SendMessage(p, comm, HostCommand.Init, null); if (response != null && response.Command == HostCommand.Init) { if (response.Message.StartsWith("success")) { response = this.SendMessage(p, comm, HostCommand.Start, null); if (response != null && response.Command == HostCommand.Start) { if (response.Message.StartsWith("success")) { this.comms[fileName] = comm; this.procs[fileName] = p; return(true); } else { p.Kill(); } } else { p.Kill(); } } else { p.Kill(); } } else { p.Kill(); } } } catch { } return(false); }
private HostMessage SendMessage(Process p, HostCommunication comm, HostCommand cmd, string msg) { try { if (!p.HasExited) { HostMessage mess = new HostMessage(); mess.Command = cmd; if (msg != null) { mess.Message = msg; } return(comm.SendReceive(mess)); } } catch { } return(new HostMessage()); }
public HostMessage Receive() { return(HostMessage.Parse(this.inStream.ReadLine())); }
internal static HostMessage Parse(string message) { HostMessage result = new HostMessage(); if (!string.IsNullOrEmpty(message)) { string[] words = message.Split(sep, StringSplitOptions.RemoveEmptyEntries); if (words != null && words.Length > 0) { string cmd = words[0]; if (cmd == "heartbeat") { result.Command = HostCommand.HeartBeat; if (words.Length > 1) { result.Message = message.Substring(10); } } else if (cmd == "init") { result.Command = HostCommand.Init; if (words.Length > 1) { result.Message = message.Substring(5); } } else if (cmd == "start") { result.Command = HostCommand.Start; if (words.Length > 1) { result.Message = message.Substring(6); } } else if (cmd == "stop") { result.Command = HostCommand.Stop; if (words.Length > 1) { result.Message = message.Substring(5); } } else if (cmd == "pause") { result.Command = HostCommand.Pause; if (words.Length > 1) { result.Message = message.Substring(6); } } else if (cmd == "resume") { result.Command = HostCommand.Resume; if (words.Length > 1) { result.Message = message.Substring(7); } } else if (cmd == "shutdown") { result.Command = HostCommand.Shutdown; if (words.Length > 1) { result.Message = message.Substring(9); } } else { result.Command = HostCommand.Unknown; result.Message = message; } } } return(result); }
public void Start() { if (this.State == RunState.Initialized || this.State == RunState.Stopped) { this.State = RunState.Starting; if (ConfigurationManager.Instance.State == RunState.Running) { if (LogManager.Instance.State == RunState.Running) { this.logger.Log(0, "Start: Called"); ServiceListFile fil = ServiceListFile.Open(this.fileName); if (fil != null) { string tmpName; foreach (string name in fil) { tmpName = Path.Combine(name, "AppHost.exe"); if (File.Exists(tmpName)) { if (!processNames.Contains(tmpName)) //new for this startup { try { Process p = this.CreateProcess(tmpName); if (p.Start()) { HostCommunication comm = new HostCommunication(p.StandardOutput, p.StandardInput); //we need to also init this thing HostMessage response = this.SendMessage(p, comm, HostCommand.Init, null); if (response != null && response.Command == HostCommand.Init) { if (response.Message.StartsWith("success")) { this.logger.Log(0, "Start: initialized " + tmpName); this.comms[tmpName] = comm; this.procs[tmpName] = p; this.processNames.Add(tmpName); } } else { p.Kill(); } } else { p.Kill(); } } catch { } } if (processNames.Contains(tmpName)) { HostCommunication comm = this.comms[tmpName]; HostMessage response = this.SendMessage(this.procs[tmpName], comm, HostCommand.Start, null); if (response != null && response.Command == HostCommand.Start) { if (response.Message.StartsWith("success")) { this.logger.Log(0, "Start: started " + tmpName); } else { this.logger.Log(100, "Start: failed starting " + tmpName); } } } else { this.logger.Log(0, "Start: no process " + tmpName); } } else { this.logger.Log(0, "Start: failed to find " + tmpName); } } //we've started what we can, now we start monitoring this.logger.Log(0, "Start: Succeeded, starting monitoring"); Task t = new Task(this.MonitorServices); t.Start(); this.State = RunState.Running; } else { this.State = RunState.FailedStarting; this.logger.Log(0, "Start: Failed: no processFiles"); } } else { this.State = RunState.FailedStarting; this.logger.Log(0, "Start: Failed: Logging didn't start"); } } } }