-
-
Save profesor79/1d115d5160c4fef0ba0ac76d06aaa870 to your computer and use it in GitHub Desktop.
when your mom wants to evict you
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class LivelinesHelper | |
{ | |
public static string CGroupMemoryFilePath = "/sys/fs/cgroup/memory/memory.usage_in_bytes"; | |
public static string ReadinesFilePath = "/somePathThatKubeCanAccess/readiness.probe"; | |
public static bool KeepConnection = true; | |
public static int CheckIntervalMiliSecs = 5000; | |
public static int MemoryThreshold = 1024 * 1024 * 1024; // 1GB | |
public static void StartMonitoring() | |
{ | |
var t = new TimerHelper(); | |
} | |
} | |
public class TimerHelper | |
{ | |
public TimerHelper() | |
{ | |
var t = new System.Timers.Timer(); | |
t.AutoReset = true; // keep raising events | |
t.Interval = LivelinesHelper.CheckIntervalMiliSecs; // In milliseconds | |
t.Elapsed += TimerElapsed; | |
t.Start(); | |
} | |
void TimerElapsed(Object source, System.Timers.ElapsedEventArgs e) | |
{ | |
GC.Collect(); | |
var memUsed = long.Parse(File.ReadAllLines(LivelinesHelper.CGroupMemoryFilePath)[0]); | |
LivelinesHelper.KeepConnection = memUsed < LivelinesHelper.MemoryThreshold; | |
var isReady = memUsed < LivelinesHelper.MemoryThreshold; | |
if (isReady) | |
{ | |
try | |
{ | |
File.WriteAllText(LivelinesHelper.ReadinesFilePath, $"all is good {DateTime.UtcNow}"); | |
} | |
catch (Exception a) | |
{ | |
Console.WriteLine(a.Message); | |
} | |
} | |
else | |
{ | |
try | |
{ | |
File.Delete(LivelinesHelper.ReadinesFilePath); | |
} | |
catch (Exception a) | |
{ | |
Console.WriteLine(a.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment