private void WorkerStatusChecker() { while (!_stopFlag) { Thread.Sleep(Configuration.PingInterval); try { if (_console.Worker == null) { if (!_stopFlag) { CreateWorker(); } } else if (ShouldKillWorker()) { RunDebuggerAgainst(_console.Worker); _console.Stop((int)Configuration.MaxOperationTime.TotalMilliseconds); } else { _console.Ping(); } } catch (Exception e) { EggLog.Info("checking worker status\r\n" + e.ToString()); } } }
static Configuration() { try { MaxWorkerLife = TimeSpan.Parse("2.0:0:0"); //TimeSpan.Parse(ConfigurationManager.AppSettings["MaxWorkerLife"]); MaxOperationTime = TimeSpan.FromSeconds(30.0); //string maxOpTime = ConfigurationManager.AppSettings["MaxOperationTime"]; //if (string.IsNullOrEmpty(maxOpTime)) // MaxOperationTime = TimeSpan.FromSeconds(30.0); //else // MaxOperationTime = TimeSpan.Parse(maxOpTime); PingInterval = TimeSpan.Parse("0:5:16"); //TimeSpan.Parse(ConfigurationManager.AppSettings["PingInterval"]); MaxPingInterval = TimeSpan.Parse("0:10:16"); //TimeSpan.Parse(ConfigurationManager.AppSettings["MaxPingInterval"]); VirtualMemory = long.Parse("50") * 1024 * 1024; //long.Parse(ConfigurationManager.AppSettings["VirtualMemory"]) * 1024 * 1024; RunType = "service"; // ConfigurationManager.AppSettings["RunType"]; HAWorker = "xpsrchvwec.exe"; // ConfigurationManager.AppSettings["HAWorker"]; Debugger = null; //ConfigurationManager.AppSettings["Debugger"]; } catch (Exception e) { EggLog.Info("master configuration error"); //EventLog.WriteEntry(Process.ProcessName, e.ToString(), EventLogEntryType.Error); throw; } }
private void ThreadWork() { while (true) { EggLog.Info(DateTime.Now.ToString()); Thread.Sleep(4 * 1000); } }
public void Start(object sender, EventArgs e) { EggLog.Info("work start"); _thead = new Thread(new ThreadStart(this.ThreadWork)); _thead.IsBackground = true; _thead.Name = "thead_egg3_work"; _thead.Start(); _running = true; }
static void Main(string[] args) { bool runAsConsole = false; string magicCookie = string.Empty; for (int i = 0; i < args.Length; ++i) { if (string.IsNullOrEmpty(args[i])) { continue; } if (args[i][0] == '/' || args[i][0] == '-') { if (args[i].Substring(1) == "console") { runAsConsole = true; } } else { magicCookie = args[i]; } } if (string.IsNullOrEmpty(magicCookie)) { runAsConsole = true; magicCookie = Guid.NewGuid().ToString("n"); } try { (new WorkerController(runAsConsole)).Run(magicCookie); } catch (Exception ex) { if (runAsConsole) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex); Console.ResetColor(); } else { EggLog.Info("work run error " + ex.ToString()); //EventLog.WriteEntry(Configuration.Process.ProcessName, ex.ToString(), EventLogEntryType.Error); } } if (runAsConsole) { Console.WriteLine("press ENTER to exit..."); Console.ReadLine(); } }
public void Stop(object sender, EventArgs e) { EggLog.Info("work stop"); if (_thead != null) { _thead.Abort(); _thead = null; } _running = false; }
static void Main() { EggLog.Info("onlytest"); if (string.Compare(Configuration.RunType, "console", true) == 0) { RunAsConsole(); } else { RunAsService(); } }
private bool ShouldKillWorker() { _console.Worker.Refresh(); bool pingInterval = DateTime.Now - _console.LastHeartbeat > Configuration.MaxPingInterval; if (pingInterval) { EggLog.Info(string.Format("master pause service! \r\nreason = {0} over max ping interval", Configuration.HAWorker)); } bool workerLife = DateTime.Now - _console.StartTime > Configuration.MaxWorkerLife; if (DateTime.Now.Hour < 3 || DateTime.Now.Hour >= 7) { workerLife = false; } else if (workerLife) { DateTime baseTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 3, 0, 0); Random r = new Random(); if (baseTime.AddMinutes(r.Next(180)) > DateTime.Now) { workerLife = false; } } if (workerLife) { EggLog.Info(string.Format("master pause service! \r\nreason = {0} over max worker life", Configuration.HAWorker)); } long privateMemory = _console.Worker.PrivateMemorySize64; bool maxMemory = privateMemory > Configuration.VirtualMemory; if (maxMemory) { EggLog.Info(string.Format("master pause service! \r\nreason = {0} over max memory, current={1}", Configuration.HAWorker, privateMemory)); } return(pingInterval || workerLife || maxMemory); }
protected override void OnStart(string[] args) { EggLog.Info("onstart"); monitorService.Start(); EggLog.Info("onstart finish"); }
private void RunDebuggerAgainst(Process p) { if (p == null) { return; } if (string.IsNullOrEmpty(Configuration.Debugger)) { return; } try { DateTime date = DateTime.UtcNow; ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/C " + Configuration.Debugger.Replace("%ld", p.Id.ToString()).Replace("%ln", p.ProcessName).Replace("%dd", date.ToString("yyyyMMdd")).Replace("%dt", date.ToString("HHmmss"))); start.CreateNoWindow = true; start.UseShellExecute = false; start.ErrorDialog = false; start.RedirectStandardError = true; start.RedirectStandardOutput = true; string log = Configuration.HAWorker + ".log"; ExecutionContext ctxOutput = new ExecutionContext(); ExecutionContext ctxError = new ExecutionContext(); try { ctxOutput.File = ctxError.File = new FileStream(log, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); ctxOutput.Buffer = new byte[1024]; ctxError.Buffer = new byte[1024]; } catch (Exception ex) { EggLog.Info(string.Format("master failed to open log file! \r\npath={0}\r\nreason={1}", log, ex.Message)); } using (Process debugger = Process.Start(start)) { if (ctxOutput.File != null) { try { ctxOutput.Source = debugger.StandardOutput.BaseStream; ctxError.Source = debugger.StandardError.BaseStream; ctxOutput.Source.BeginRead(ctxOutput.Buffer, 0, ctxOutput.Buffer.Length, OnDebuggerOutputDataReceived, ctxOutput); ctxError.Source.BeginRead(ctxError.Buffer, 0, ctxError.Buffer.Length, OnDebuggerOutputDataReceived, ctxError); } catch (Exception ex) { EggLog.Info(string.Format("master failed to read from Debugger! \r\ndebugger={0}\r\nreason={1}", Configuration.Debugger, ex.Message)); } } debugger.WaitForExit(); } } catch (Exception ex) { EggLog.Info(string.Format("master failed to run Debugger! \r\ndebugger={0}\r\nreason={1}", Configuration.Debugger, ex.Message)); } }