/** * [CSRD] Uptime Limiter * * There seems to be a memory leak somewhere in the server. Definitely not a handle leak. * Supposedly, it's caused by SourceTV. * * Because I can't be bothered to figure out the full cause at the moment, I'll just let the * server restart itself every day to avoid it filling up swap and getting OOM killed. * * Disconnecting an actively connected user would be rude, so we'll wait until the server is * empty. */ #pragma semicolon 1 #include #pragma newdecls required #define PLUGIN_VERSION "0.1.0" public Plugin myinfo = { name = "[CSRD] Uptime Limiter", author = "nosoop", description = "Forces a server to auto-restart after a certain uptime when empty.", version = PLUGIN_VERSION, url = "https://git.csrd.science/" } int g_nMaxUptimeLimit = 60 * 60 * 12; /** * Called when the Waiting for Players round state begins. * At least one client, human or bot, must be connected for this forward to be called. * * Considering the server runs bots, we can use this forward to check if any human players are * connected to the server, and if not, we can restart the server when desirable. */ public void TF2_OnWaitingForPlayersStart() { if (GetUptime() > g_nMaxUptimeLimit) { if (!IsHumanConnected()) { LogMessage("Server has reached an uptime of %d seconds and is currently empty. " ... "Restarting...", GetUptime()); ServerCommand("quit"); } } } /** * Checks if a human player is connected to the server. * This always returns false between the OnMapEnd and OnMapStart / OnConfigsExecuted callbacks. */ bool IsHumanConnected() { for (int i = 1; i < MaxClients; i++) { if (IsClientConnected(i) && !IsFakeClient(i)) { return true; } } return false; } /** * Engine time is effectively the amount of time since server startup, as far as I'm * concerned. */ int GetUptime() { float flEngineTime = GetEngineTime(); return RoundToFloor(flEngineTime); }