Skip to content

Instantly share code, notes, and snippets.

@profesor79
Created February 26, 2023 08:17
Show Gist options
  • Save profesor79/1d115d5160c4fef0ba0ac76d06aaa870 to your computer and use it in GitHub Desktop.
Save profesor79/1d115d5160c4fef0ba0ac76d06aaa870 to your computer and use it in GitHub Desktop.
when your mom wants to evict you
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