From a1d45a4419ceee3d47ad4c31b8d41c4d263c7455 Mon Sep 17 00:00:00 2001 From: Nyo Date: Sun, 28 Aug 2016 17:25:47 +0200 Subject: [PATCH] .BANCHO. Add ir command, more info in system status --- constants/fokabotCommands.py | 12 +++++++++++- helpers/systemHelper.py | 32 ++++++++++++++++++++++++-------- objects/glob.py | 3 +++ pep.py | 15 +++++++-------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/constants/fokabotCommands.py b/constants/fokabotCommands.py index 1390c37..cd2a3ec 100644 --- a/constants/fokabotCommands.py +++ b/constants/fokabotCommands.py @@ -27,6 +27,11 @@ message -- list containing arguments passed from the message return the message or **False** if there's no response by the bot """ +def instantRestart(fro, chan, message): + glob.tokens.enqueueAll(serverPackets.notification("We are restarting Bancho. Be right back!")) + systemHelper.scheduleShutdown(0, True, delay=1) + return False + def faq(fro, chan, message): if message[0] == "rules": return "Please make sure to check (Ripple's rules)[http://ripple.moe/?p=23]." @@ -352,12 +357,13 @@ def systemStatus(fro, chan, message): data = systemHelper.getSystemInfo() # Final message - msg = "pep.py bancho server v{}".format(glob.VERSION) + msg = "pep.py bancho server v{}\n".format(glob.VERSION) msg += "made by the Ripple team\n" msg += "\n" msg += "=== BANCHO STATS ===\n" msg += "Connected users: {}\n".format(data["connectedUsers"]) msg += "Multiplayer matches: {}\n".format(data["matches"]) + msg += "Uptime: {}\n".format(data["uptime"]) msg += "\n" msg += "=== SYSTEM STATS ===\n" msg += "CPU: {}%\n".format(data["cpuUsage"]) @@ -724,6 +730,10 @@ commands = [ }, { "trigger": "!last", "callback": tillerinoLast + }, { + "trigger": "!ir", + "privileges": privileges.ADMIN_MANAGE_SERVERS, + "callback": instantRestart } # # "trigger": "!acc", diff --git a/helpers/systemHelper.py b/helpers/systemHelper.py index a1b5b80..122f546 100644 --- a/helpers/systemHelper.py +++ b/helpers/systemHelper.py @@ -6,6 +6,8 @@ import sys import threading import signal from helpers import logHelper as log +import time +import math def runningUnderUnix(): """ @@ -17,7 +19,7 @@ def runningUnderUnix(): return True if os.name == "posix" else False -def scheduleShutdown(sendRestartTime, restart, message = ""): +def scheduleShutdown(sendRestartTime, restart, message = "", delay=20): """ Schedule a server shutdown/restart @@ -27,7 +29,7 @@ def scheduleShutdown(sendRestartTime, restart, message = ""): """ # Console output - log.info("Pep.py will {} in {} seconds!".format("restart" if restart else "shutdown", sendRestartTime+20)) + log.info("Pep.py will {} in {} seconds!".format("restart" if restart else "shutdown", sendRestartTime+delay)) log.info("Sending server restart packets in {} seconds...".format(sendRestartTime)) # Send notification if set @@ -35,7 +37,7 @@ def scheduleShutdown(sendRestartTime, restart, message = ""): glob.tokens.enqueueAll(serverPackets.notification(message)) # Schedule server restart packet - threading.Timer(sendRestartTime, glob.tokens.enqueueAll, [serverPackets.banchoRestart(50000)]).start() + threading.Timer(sendRestartTime, glob.tokens.enqueueAll, [serverPackets.banchoRestart(delay*2*1000)]).start() glob.restarting = True # Restart/shutdown @@ -44,8 +46,8 @@ def scheduleShutdown(sendRestartTime, restart, message = ""): else: action = shutdownServer - # Schedule actual server shutdown/restart 20 seconds after server restart packet, so everyone gets it - threading.Timer(sendRestartTime+20, action).start() + # Schedule actual server shutdown/restart some seconds after server restart packet, so everyone gets it + threading.Timer(sendRestartTime+delay, action).start() def restartServer(): @@ -56,7 +58,7 @@ def restartServer(): def shutdownServer(): """Shutdown pep.py""" - log.info("> Shutting down pep.py...") + log.info("Shutting down pep.py...") sig = signal.SIGKILL if runningUnderUnix() else signal.CTRL_C_EVENT os.kill(os.getpid(), sig) @@ -76,9 +78,23 @@ def getSystemInfo(): # General stats data["connectedUsers"] = len(glob.tokens.tokens) data["matches"] = len(glob.matches.matches) + delta = time.time()-glob.startTime + days = math.floor(delta/86400) + delta -= days*86400 + + hours = math.floor(delta/3600) + delta -= hours*3600 + + minutes = math.floor(delta/60) + delta -= minutes*60 + + seconds = math.floor(delta) + + data["uptime"] = "{}d {}h {}m {}s".format(days, hours, minutes, seconds) data["cpuUsage"] = psutil.cpu_percent() - data["totalMemory"] = "{0:.2f}".format(psutil.virtual_memory()[0]/1074000000) - data["usedMemory"] = "{0:.2f}".format(psutil.virtual_memory()[3]/1074000000) + memory = psutil.virtual_memory() + data["totalMemory"] = "{0:.2f}".format(memory.total/1074000000) + data["usedMemory"] = "{0:.2f}".format(memory.active/1074000000) # Unix only stats if data["unix"] == True: diff --git a/objects/glob.py b/objects/glob.py index 4182d26..3ec6648 100644 --- a/objects/glob.py +++ b/objects/glob.py @@ -4,6 +4,7 @@ from objects import tokenList from objects import channelList from objects import matchList from objects import fileLocks +import time try: with open("version") as f: @@ -32,3 +33,5 @@ discord = False gzip = False localize = False sentry = False + +startTime = int(time.time()) diff --git a/pep.py b/pep.py index 747f60e..b71e7d1 100644 --- a/pep.py +++ b/pep.py @@ -71,6 +71,13 @@ if __name__ == "__main__": else: consoleHelper.printDone() + # Create data folder if needed + consoleHelper.printNoNl("> Checking folders... ") + paths = [".data"] + for i in paths: + if not os.path.exists(i): + os.makedirs(i, 0o770) + consoleHelper.printDone() # Connect to db try: @@ -108,14 +115,6 @@ if __name__ == "__main__": consoleHelper.printColored("[!] Error while loading chat filters. Make sure there is a filters.txt file present", bcolors.RED) raise - # Create data folder if needed - consoleHelper.printNoNl("> Checking folders... ") - paths = [".data"] - for i in paths: - if not os.path.exists(i): - os.makedirs(i, 0o770) - consoleHelper.printDone() - # Initialize chat channels print("> Initializing chat channels... ") glob.channels.loadChannels()