csrd_uptime_limiter.sp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * [CSRD] Uptime Limiter
  3. *
  4. * There seems to be a memory leak somewhere in the server. Definitely not a handle leak.
  5. * Supposedly, it's caused by SourceTV.
  6. *
  7. * Because I can't be bothered to figure out the full cause at the moment, I'll just let the
  8. * server restart itself every day to avoid it filling up swap and getting OOM killed.
  9. *
  10. * Disconnecting an actively connected user would be rude, so we'll wait until the server is
  11. * empty.
  12. */
  13. #pragma semicolon 1
  14. #include <sourcemod>
  15. #pragma newdecls required
  16. #define PLUGIN_VERSION "0.1.0"
  17. public Plugin myinfo = {
  18. name = "[CSRD] Uptime Limiter",
  19. author = "nosoop",
  20. description = "Forces a server to auto-restart after a certain uptime when empty.",
  21. version = PLUGIN_VERSION,
  22. url = "https://git.csrd.science/"
  23. }
  24. int g_nMaxUptimeLimit = 60 * 60 * 12;
  25. /**
  26. * Called when the Waiting for Players round state begins.
  27. * At least one client, human or bot, must be connected for this forward to be called.
  28. *
  29. * Considering the server runs bots, we can use this forward to check if any human players are
  30. * connected to the server, and if not, we can restart the server when desirable.
  31. */
  32. public void TF2_OnWaitingForPlayersStart() {
  33. if (GetUptime() > g_nMaxUptimeLimit) {
  34. if (!IsHumanConnected()) {
  35. LogMessage("Server has reached an uptime of %d seconds and is currently empty. "
  36. ... "Restarting...", GetUptime());
  37. ServerCommand("quit");
  38. }
  39. }
  40. }
  41. /**
  42. * Checks if a human player is connected to the server.
  43. * This always returns false between the OnMapEnd and OnMapStart / OnConfigsExecuted callbacks.
  44. */
  45. bool IsHumanConnected() {
  46. for (int i = 1; i < MaxClients; i++) {
  47. if (IsClientConnected(i) && !IsFakeClient(i)) {
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. /**
  54. * Engine time is effectively the amount of time since server startup, as far as I'm
  55. * concerned.
  56. */
  57. int GetUptime() {
  58. float flEngineTime = GetEngineTime();
  59. return RoundToFloor(flEngineTime);
  60. }