csrd_uptime_limiter.sp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include <stocksoup/log_server>
  16. #pragma newdecls required
  17. #define PLUGIN_VERSION "0.0.3"
  18. public Plugin myinfo = {
  19. name = "[CSRD] Uptime Limiter",
  20. author = "nosoop",
  21. description = "Forces a server to auto-restart after a certain uptime when empty.",
  22. version = PLUGIN_VERSION,
  23. url = "https://git.csrd.science/"
  24. }
  25. int g_nMaxUptimeLimit = 60 * 60 * 12;
  26. /**
  27. * Called when the Waiting for Players round state begins.
  28. * At least one client, human or bot, must be connected for this forward to be called.
  29. *
  30. * Considering the server runs bots, we can use this forward to check if any human players are
  31. * connected to the server, and if not, we can restart the server when desirable.
  32. */
  33. public void TF2_OnWaitingForPlayersStart() {
  34. if (GetUptime() > g_nMaxUptimeLimit) {
  35. if (!IsHumanConnected()) {
  36. LogMessage("Server has reached an uptime of %d seconds and is currently empty. "
  37. ... "Restarting...", GetUptime());
  38. ServerCommand("quit");
  39. }
  40. }
  41. }
  42. /**
  43. * Checks if a human player is connected to the server.
  44. * This always returns false between the OnMapEnd and OnMapStart / OnConfigsExecuted callbacks.
  45. */
  46. bool IsHumanConnected() {
  47. for (int i = 1; i < MaxClients; i++) {
  48. if (IsClientConnected(i) && !IsFakeClient(i)) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. /**
  55. * Engine time is effectively the amount of time since server startup, as far as I'm
  56. * concerned.
  57. */
  58. int GetUptime() {
  59. float flEngineTime = GetEngineTime();
  60. return RoundToFloor(flEngineTime);
  61. }