forked from freeciv/freeciv-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civlauncher.py
58 lines (53 loc) · 2.88 KB
/
civlauncher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from threading import Thread
from subprocess import call, PIPE
from shutil import *
import sys
from time import gmtime, strftime
import time
pubscript = "pubscript_"
logdir = "../logs/"
# The Civlauncher class launches a new instance of a Freeciv-web server in a
# separate thread and restarts the process when the game ends.
class Civlauncher(Thread):
def __init__ (self, gametype, new_port, metahostpath, savesdir):
Thread.__init__(self)
self.new_port = new_port;
self.gametype = gametype;
self.metahostpath = metahostpath;
self.savesdir = savesdir;
self.started_time = strftime("%Y-%m-%d %H:%M:%S", gmtime());
self.num_start = 0;
self.num_error = 0;
def run(self):
while 1:
try:
print("Starting new Freeciv-web server at port " + str(self.new_port) +
" and Freeciv-proxy server at port " + str(1000 + self.new_port) + ".");
rank_cmd = "";
if (self.gametype=="pbem"): rank_cmd = " --Ranklog ../resin/webapps/data/ranklogs/rank_"+str(self.new_port)+".log ";
retcode = call("ulimit -t 10000 && export FREECIV_SAVE_PATH=\"" + self.savesdir + "\";" +
"rm -f ../resin/webapps/data/scorelogs/score-" + str(self.new_port) + ".log; " +
"python3.4 ../freeciv-proxy/freeciv-proxy.py " +
str(1000 + self.new_port) + " > " + logdir + "freeciv-proxy-" +
str(1000 + self.new_port) +".log 2>&1 " +
"& proxy_pid=$! && " +
"freeciv-web --debug=1 --port " + str(self.new_port) +
" -q 20 --Announce none -e " +
" -m -M http://" + self.metahostpath + " --type \"" + self.gametype +
"\" --read " + pubscript + self.gametype + ".serv" +
" --log " + logdir + "freeciv-web-log-" + str(self.new_port) + ".log " +
rank_cmd + "--saves " + self.savesdir + " > /dev/null " +
" 2> " + logdir + "freeciv-web-stderr-" + str(self.new_port) + ".log " +
" ; rc=$?; kill -9 $proxy_pid; exit $rc", shell=True)
self.num_start += 1;
if retcode > 0:
print("Freeciv-web port " + str(self.new_port) + " was terminated by signal",
-retcode, file=sys.stderr)
self.num_error += 1;
else:
print("Freeciv-web port " + str(self.new_port) + " returned",
retcode, file=sys.stderr)
except OSError as e:
print("Execution failed:", e, file=sys.stderr)
self.num_error += 1;
time.sleep(5)