pep.py/helpers/systemHelper.py

121 lines
3.1 KiB
Python
Raw Normal View History

2016-10-02 20:48:14 +00:00
import math
2016-04-19 17:40:59 +00:00
import os
2016-10-02 20:48:14 +00:00
import signal
2016-04-19 17:40:59 +00:00
import sys
import threading
import time
2016-10-02 20:48:14 +00:00
import psutil
from common.constants import bcolors
from common.log import logUtils as log
from constants import serverPackets
from helpers import consoleHelper
from objects import glob
2016-04-19 17:40:59 +00:00
2016-09-04 10:01:10 +00:00
def dispose():
"""
Perform some clean up. Called on shutdown.
2016-09-04 10:01:10 +00:00
:return:
"""
print("> Disposing server... ")
glob.fileBuffers.flushAll()
consoleHelper.printColored("Goodbye!", bcolors.GREEN)
2016-04-19 17:40:59 +00:00
def runningUnderUnix():
"""
Get if the server is running under UNIX or NT
:return: True if running under UNIX, otherwise False
2016-04-19 17:40:59 +00:00
"""
return True if os.name == "posix" else False
def scheduleShutdown(sendRestartTime, restart, message = "", delay=20):
2016-04-19 17:40:59 +00:00
"""
Schedule a server shutdown/restart
:param sendRestartTime: time (seconds) to wait before sending server restart packets to every client
:param restart: if True, server will restart. if False, server will shudown
:param message: if set, send that message to every client to warn about the shutdown/restart
:param delay: additional restart delay in seconds. Default: 20
:return:
2016-04-19 17:40:59 +00:00
"""
# Console output
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))
2016-04-19 17:40:59 +00:00
# Send notification if set
if message != "":
glob.streams.broadcast("main", serverPackets.notification(message))
2016-04-19 17:40:59 +00:00
# Schedule server restart packet
threading.Timer(sendRestartTime, glob.streams.broadcast, ["main", serverPackets.banchoRestart(delay*2*1000)]).start()
2016-04-19 17:40:59 +00:00
glob.restarting = True
# Restart/shutdown
if restart:
action = restartServer
else:
action = shutdownServer
# Schedule actual server shutdown/restart some seconds after server restart packet, so everyone gets it
threading.Timer(sendRestartTime+delay, action).start()
2016-04-19 17:40:59 +00:00
def restartServer():
"""
Restart pep.py
:return:
"""
log.info("Restarting pep.py...")
2016-09-04 10:01:10 +00:00
dispose()
2016-04-19 17:40:59 +00:00
os.execv(sys.executable, [sys.executable] + sys.argv)
def shutdownServer():
"""
Shutdown pep.py
:return:
"""
log.info("Shutting down pep.py...")
2016-09-04 10:01:10 +00:00
dispose()
2016-04-19 17:40:59 +00:00
sig = signal.SIGKILL if runningUnderUnix() else signal.CTRL_C_EVENT
os.kill(os.getpid(), sig)
def getSystemInfo():
"""
Get a dictionary with some system/server info
:return: ["unix", "connectedUsers", "webServer", "cpuUsage", "totalMemory", "usedMemory", "loadAverage"]
2016-04-19 17:40:59 +00:00
"""
2016-09-02 15:45:10 +00:00
data = {"unix": runningUnderUnix(), "connectedUsers": len(glob.tokens.tokens), "matches": len(glob.matches.matches)}
2016-04-19 17:40:59 +00:00
# General stats
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)
2016-04-19 17:40:59 +00:00
data["cpuUsage"] = psutil.cpu_percent()
memory = psutil.virtual_memory()
data["totalMemory"] = "{0:.2f}".format(memory.total/1074000000)
data["usedMemory"] = "{0:.2f}".format(memory.active/1074000000)
2016-04-19 17:40:59 +00:00
# Unix only stats
2016-09-02 15:45:10 +00:00
if data["unix"]:
2016-04-19 17:40:59 +00:00
data["loadAverage"] = os.getloadavg()
else:
data["loadAverage"] = (0,0,0)
return data