Skip to content

Instantly share code, notes, and snippets.

@cstewart90
Last active April 28, 2020 15:53
Show Gist options
  • Save cstewart90/577fe1ba7b4a09fa26e7b238655681b3 to your computer and use it in GitHub Desktop.
Save cstewart90/577fe1ba7b4a09fa26e7b238655681b3 to your computer and use it in GitHub Desktop.
Queries local QL servers and restarts supervisor process if they are not responding
#!/usr/bin/env python3.5
from supervisor.xmlrpc import SupervisorTransport
import xmlrpc.client
import valve.source.a2s as a2s
import socket
import time
supervisor_socket = "unix:///tmp/supervisor.sock"
SERVERS = [{"port": 27960, "process": "qldf:qzeroded_qldf1", "name": "DE Race #1"},
{"port": 27961, "process": "qldf:qzeroded_qldf2", "name": "DE Race #2"}]
def main():
offline = []
for server in SERVERS:
info = get_server_info(server["port"])
if info:
players = "{player_count}/{max_players}".format(**info)
print("{} (localhost:{}) is online {}".format(server["name"], server["port"], players))
else:
offline.append(server)
print("{name} (localhost:{port}) is offline. Will check again in 20 seconds."
.format(**server))
if offline:
time.sleep(20)
for server in offline:
info = get_server_info(server["port"])
if info:
players = "{player_count}/{max_players}".format(**info)
print("{} (localhost:{}) is now online {}".format(server["name"], server["port"], players))
else:
print("{name} (localhost:{port}) is still offline. Restarting...".format(**server))
restart(server["process"])
def get_server_info(port):
try:
return a2s.ServerQuerier(("localhost", port), 2).info()
except a2s.NoResponseError:
return
def restart(process_name):
try:
transport = SupervisorTransport(serverurl=supervisor_socket)
server = xmlrpc.client.ServerProxy("http://127.0.0.1", transport=transport)
if server.supervisor.getProcessInfo(process_name)["statename"] == "RUNNING":
server.supervisor.stopProcess(process_name)
server.supervisor.startProcess(process_name)
print("{} was restarted!".format(process_name))
else:
print("{} status is stopped or fatal so not restarting.".format(process_name))
except socket.error:
print("Error connection to supervisor.")
except xmlrpc.client.Fault:
print("Invalid process name({}).".format(process_name))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment