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
|
2016-08-28 15:25:47 +00:00
|
|
|
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.
|
|
|
|
: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
|
|
|
|
"""
|
|
|
|
return True if os.name == "posix" else False
|
|
|
|
|
2016-08-28 15:25:47 +00:00
|
|
|
def scheduleShutdown(sendRestartTime, restart, message = "", delay=20):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Schedule a server shutdown/restart
|
|
|
|
|
|
|
|
sendRestartTime -- time (seconds) to wait before sending server restart packets to every client
|
|
|
|
restart -- if True, server will restart. if False, server will shudown
|
|
|
|
message -- if set, send that message to every client to warn about the shutdown/restart
|
|
|
|
"""
|
|
|
|
# Console output
|
2016-08-28 15:25:47 +00:00
|
|
|
log.info("Pep.py will {} in {} seconds!".format("restart" if restart else "shutdown", sendRestartTime+delay))
|
2016-06-04 10:44:54 +00:00
|
|
|
log.info("Sending server restart packets in {} seconds...".format(sendRestartTime))
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
# Send notification if set
|
|
|
|
if message != "":
|
2016-10-01 19:19:03 +00:00
|
|
|
glob.streams.broadcast("main", serverPackets.notification(message))
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
# Schedule server restart packet
|
2016-10-01 19:19:03 +00:00
|
|
|
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
|
|
|
|
|
2016-08-28 15:25:47 +00:00
|
|
|
# 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 script"""
|
2016-06-04 10:44:54 +00:00
|
|
|
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"""
|
2016-08-28 15:25:47 +00:00
|
|
|
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-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
|
2016-08-28 15:25:47 +00:00
|
|
|
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()
|
2016-08-28 15:25:47 +00:00
|
|
|
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
|