public void ShowStatus() { Pidfile pidfile = new Pidfile(); if (pidfile.IsValid()) { Console.WriteLine("Watchdog is up and running"); } else { Console.WriteLine("Watchdog is down"); } }
public void Start() { Logger.InitLogger(); int port = FindAvaiblePort(); File.WriteAllText(Location.Socket, port.ToString()); WatchdogServer server = new WatchdogServer(); if (!server.Setup(port)) { Logger.log.Error("Failed to setup server."); return; } //Try to start the server if (!server.Start()) { Logger.log.Error("Failed to start server."); return; } Logger.log.Info("Watchdog started"); Pidfile pidfile = new Pidfile(Process.GetCurrentProcess().Id); TimeSpan timeout = TimeSpan.FromSeconds(3); stopEvent.Reset(); try { do { pidfile.Save(); } while (!stopEvent.WaitOne(timeout)); Thread.Sleep(100); //server.Stop(); Logger.log.Debug("Stopping watchdog..."); foreach (Project proj in ProjectManager.instance.GetProjects()) { proj.Clear(); } if (Directory.Exists(Location.Slaves)) { Directory.Delete(Location.Slaves, true); } Logger.log.Info("Watchdog stopped"); } catch (Exception e) { Logger.log.Error("Error while stopping watchdog", e); } }
public void Daemonize() { Pidfile pidfile = new Pidfile(); if (pidfile.IsValid()) { Console.WriteLine("Process already started"); return; } string mono = Environment.GetEnvironmentVariable("_"); string app = Environment.GetCommandLineArgs()[0]; ProcessStartInfo startInfo = new ProcessStartInfo(mono, app + " " + "job"); startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; Process.Start(startInfo); Thread.Sleep(500); }
public void Stop() { Pidfile pidfile = new Pidfile(); if (pidfile.IsValid() && File.Exists(Location.Socket)) { int port = Convert.ToInt32(File.ReadAllText(Location.Socket)); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.NoDelay = true; sock.Blocking = true; sock.Connect("localhost", port); sock.Send(Encoding.ASCII.GetBytes("STOP\r\n")); byte[] buff = new byte[64]; sock.Receive(buff); Thread.Sleep(500); sock.Close(); pidfile.Delete(); Console.WriteLine("Watchdog stopped"); } else { Console.WriteLine("No watchdog is currently running"); } }