Skip to content

Commit 6360eb9

Browse files
committed
workers can download and cache maps now
1 parent e441f5b commit 6360eb9

File tree

3 files changed

+20
-97
lines changed

3 files changed

+20
-97
lines changed

ants/playgame.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,13 @@ def run_rounds(opts,args):
207207
def get_cmd_wd(cmd):
208208
new_cmd = []
209209
wd = None
210-
for part in cmd.split():
210+
for i, part in enumerate(cmd.split()):
211211
if wd == None and os.path.exists(part):
212212
wd = os.path.split(os.path.realpath(part))[0]
213-
new_cmd.append(os.path.join(".", os.path.basename(part)))
213+
if i == 0:
214+
new_cmd.append(os.path.join(".", os.path.basename(part)))
215+
else:
216+
new_cmd.append(os.path.basename(part))
214217
else:
215218
new_cmd.append(part)
216219
return wd, ' '.join(new_cmd)

worker/submission_hash.py

-69
This file was deleted.

worker/worker.py

+15-26
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import os
55
import json
66
import urllib
7-
import tempfile
8-
import logging
97
import logging.handlers
8+
import logging
109
import shutil
11-
import hashlib
10+
from hashlib import md5
1211
import time
1312
import stat
1413
import platform
@@ -18,7 +17,6 @@
1817
from optparse import OptionParser
1918

2019
from server_info import server_info
21-
from submission_hash import hash_file_sha
2220

2321
import compiler
2422
from engine import run_game
@@ -28,7 +26,7 @@
2826
log.setLevel(logging.INFO)
2927
log_file = os.path.join(server_info['logs_path'], 'worker.log')
3028
handler = logging.handlers.RotatingFileHandler(log_file,
31-
maxBytes=1000000,
29+
maxBytes=10000000,
3230
backupCount=5)
3331
handler.setLevel(logging.INFO)
3432
handler2 = logging.StreamHandler()
@@ -125,20 +123,20 @@ def get_map(self, map_filename):
125123
url = '%s/maps/%s' % (self.base_url, map_filename)
126124
log.info("Downloading map %s" % url)
127125
data = urllib.urlopen(url).read()
128-
return data.read()
126+
log.debug(data)
127+
return data
129128
except Exception as ex:
130129
log.error("Get map error: %s" % ex)
131130
return None
132131

133-
# TODO: save failed posts locally and retry on worker startup
134132
def post_result(self, method, result):
135133
# retry 10 times or until post is successful
136134
retry = 1
137135
for i in range(retry):
138136
url = self.get_url(method)
139137
log.info(url)
140138
json_data = json.dumps(result)
141-
hash = hashlib.md5(json_data).hexdigest()
139+
hash = md5(json_data).hexdigest()
142140
log.debug("Posting result %s: %s" % (method, json_data))
143141
log.info("Posting hash: %s" % hash)
144142
response = urllib.urlopen(url, json.dumps(result))
@@ -152,7 +150,7 @@ def post_result(self, method, result):
152150
break
153151
elif i < retry-1:
154152
time.sleep(5)
155-
except ValueError as ex:
153+
except ValueError:
156154
log.info("Bad json from server during post result: %s" % data)
157155
if i < retry-1:
158156
time.sleep(5)
@@ -201,7 +199,7 @@ def download_submission(self, submission_id):
201199
if filename != None:
202200
remote_hash = self.cloud.get_submission_hash(submission_id)
203201
with open(filename, 'rb') as f:
204-
local_hash = hashlib.md5(f.read()).hexdigest()
202+
local_hash = md5(f.read()).hexdigest()
205203
if local_hash != remote_hash:
206204
log.error("After downloading submission %s to %s hash didn't match" %
207205
(submission_id, download_dir))
@@ -234,7 +232,7 @@ def unpack(self, submission_id):
234232
for file_name, command in zip_files:
235233
if os.path.exists(file_name):
236234
log.info("unnzip status: %s" % os.system(command))
237-
for dirpath, dirnames, filenames in os.walk(".."):
235+
for dirpath, _, filenames in os.walk(".."):
238236
os.chmod(dirpath, 0755)
239237
for filename in filenames:
240238
filename = os.path.join(dirpath, filename)
@@ -297,21 +295,16 @@ def report(status):
297295
log.info("Functional Test Failure")
298296
report(STATUS_TEST_ERROR)
299297
return False
300-
301-
def check_hash(self, submission_id):
302-
try:
303-
for filename in os.listdir(os.path.join(server_info["compiled_path"], str(submission_id))):
304-
if filename.endswith(".zip") or filename.endswith(".tgz"):
305-
log.info("%s: %s" % (filename, hash_file_sha1(filename)))
306-
except:
307-
log.error("Submission path not found.")
308298

309299
def get_map(self, map_filename):
310300
map_file = os.path.join(server_info["maps_path"], map_filename)
311301
if not os.path.exists(map_file):
312302
data = self.cloud.get_map(map_filename)
313303
if data == None:
314304
raise Exception("map", "Could not download map from main server.")
305+
map_dir = os.path.split(map_file)[0]
306+
if not os.path.exists(map_dir):
307+
os.makedirs(map_dir)
315308
f = open(map_file, 'w')
316309
f.write(data)
317310
f.close()
@@ -405,7 +398,6 @@ def game(self, task, report_status=False):
405398
if report_status:
406399
self.cloud.post_result('api_game_result', result)
407400
except Exception as ex:
408-
import traceback
409401
log.debug(traceback.format_exc())
410402
result = {"post_id": self.post_id,
411403
"matchup_id": matchup_id,
@@ -418,13 +410,13 @@ def task(self, last=False):
418410
task = self.cloud.get_task()
419411
if task:
420412
try:
421-
log.info("Recieved task: %s" % task)
413+
log.info("Received task: %s" % task)
422414
if task['task'] == 'compile':
423415
submission_id = int(task['submission_id'])
424416
try:
425417
if not self.compile(submission_id, True):
426418
self.clean_download(submission_id)
427-
except Exception as e:
419+
except Exception:
428420
log.error(traceback.format_exc())
429421
self.clean_download(submission_id)
430422
elif task['task'] == 'game':
@@ -446,9 +438,6 @@ def main(argv):
446438
parser.add_option("-s", "--submission_id", dest="submission_id",
447439
type="int", default=0,
448440
help="Submission id to use for hash, download and compile")
449-
parser.add_option("--hash", dest="hash",
450-
action="store_true", default=False,
451-
help="Display submission hash")
452441
parser.add_option("-d", "--download", dest="download",
453442
action="store_true", default=False,
454443
help="Download submission")
@@ -465,7 +454,7 @@ def main(argv):
465454
action="store_true", default=False,
466455
help="Set the log level to debug")
467456

468-
(opts, args) = parser.parse_args(argv)
457+
(opts, _) = parser.parse_args(argv)
469458
if opts.debug:
470459
log.setLevel(logging.DEBUG)
471460
worker = Worker(True)

0 commit comments

Comments
 (0)