private async Task Start() { ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id; plot = new DatePlot("MopsKiller", relativeTime: false, multipleLines: true); openFileChecker = new System.Threading.Timer(checkOpenFiles, null, 30000, 30000); await Task.Delay(-1); }
private void checkOpenFiles(object stateinfo) { try { using (var MopsBot = System.Diagnostics.Process.GetProcessesByName("dotnet").Where(x => x.Id != ProcessId && x.HandleCount > 100).OrderByDescending(x => x.WorkingSet64).First()) { int openSockets = GetCloseWaitSockets(); TimeSpan heartbeat = DateTime.Now - GetLastHeartbeat(); TimeSpan runtime = DateTime.Now - MopsBot.StartTime; long ram = (MopsBot.WorkingSet64 / 1024) / 1024; double cpu = GetCPUUsage(MopsBot.Id); Console.WriteLine($"{System.DateTime.Now} {MopsBot.ProcessName}: {MopsBot.Id}, handles: {MopsBot.HandleCount}, waiting-sockets: {openSockets}, threads: {MopsBot.Threads.Count}, RAM: {ram}, CPU: {cpu}, Runtime: {(runtime).ToString(@"h\h\:m\m\:s\s")}, Heartbeat: {heartbeat.ToString(@"m\m\:s\s")} ago"); //Reset plot if 1 day old if (plot.PlotDataPoints.Count > 23040) { plot = new DatePlot("MopsKiller", relativeTime: false, multipleLines: true); } plot.AddValueSeperate("Runtime", OxyPlot.Axes.DateTimeAxis.ToDouble(runtime), relative: false); plot.AddValueSeperate("Heartbeat", OxyPlot.Axes.DateTimeAxis.ToDouble(TimeSpan.FromSeconds(heartbeat.TotalSeconds * 60)), relative: false); plot.DrawPlot(); if (MopsBot.HandleCount >= OPENFILESLIMIT /*|| openSockets >= OPENSOCKETSLIMIT*/) { if (--COUNTDOWN == 0) { Console.WriteLine($"\nShutting down due to {MopsBot.HandleCount} open files / {openSockets} open sockets!"); MopsBot.Kill(); plot.AddValueSeperate("Mops-Killed", OxyPlot.Axes.DateTimeAxis.ToDouble(TimeSpan.FromMilliseconds(1)), relative: false); plot.AddValueSeperate("Mops-Killed", OxyPlot.Axes.DateTimeAxis.ToDouble(runtime), relative: false); plot.AddValueSeperate("Mops-Killed", OxyPlot.Axes.DateTimeAxis.ToDouble(TimeSpan.FromMilliseconds(1)), relative: false); } } else { COUNTDOWN = 6; } /*if (RAM == ram) * { * if (++RAMRepetition >= REPETITIONTHRESHOLD) * { * Console.WriteLine("\nShutting down due to 10 repetitions!"); * MopsBot.Kill(); * } * } * else * RAMRepetition = 0; * * if((DateTime.Now - GetLastLogEntry()) >= FREEZEDURATION && RAMRepetition >= 6){ * Console.WriteLine("\nShutting down due >= 3m log freeze!"); * MopsBot.Kill(); * }*/ if ((DateTime.Now - GetLastHeartbeat()) >= TimeSpan.FromMinutes(1.5) && (DateTime.Now - MopsBot.StartTime) >= TimeSpan.FromMinutes(1.5)) { Console.WriteLine("\nShutting down due to no heartbeat!"); MopsBot.Kill(); } OpenFilesCount = MopsBot.HandleCount; RAM = ram; if (openSockets > 0) { //CloseCloseWaitSockets(); } } } catch (Exception e) { Console.WriteLine("\n[FILE READING ERROR]: " + System.DateTime.Now + $" {e.Message}\n{e.StackTrace}"); //Environment.Exit(-1); } }