def __init__(self, queue_name, message_lifetime=300, logger=None): access_key = config.get("worker", "aws_access_key") secret_key = config.get("worker", "aws_secret_key") self.sqs = SQSConnection(access_key, secret_key) self.queue = self.sqs.create_queue(queue_name, message_lifetime) self.queue.set_message_class(JSONMessage) self.logger = logger
def fetch(key, logger=None): """download and extract an archive""" template = "$AICHALLENGE_PREFIX/var/lib/aichallenge/submissions/%s" path = os.path.expandvars(template % key) if os.path.isdir(path): # already decompressed -- move on return if logger is not None: logger.info('downloading ' + key) access_key = config.get('worker', 'aws_access_key') secret_key = config.get('worker', 'aws_secret_key') bucket = config.get('worker', 'submission_bucket') prefix = config.get('worker', 'submission_prefix') s3 = boto.s3.Connection(access_key, secret_key) bucket = s3.get_bucket(bucket) s3_key = bucket.get_key(prefix + key) if s3_key is None: raise KeyError io = cStringIO.StringIO() s3_key.get_contents_to_file(io) try: io.seek(0) zip = zipfile.ZipFile(io) decompress_zip(zip, path) return except zipfile.BadZipfile: pass try: io.seek(0) tar = tarfile.open(fileobj=io) decompress_tar(tar, path) return except tarfile.TarError: pass raise ValueError, "invalid archive"
def run_game(self, body): """run a game based on the parameters of a queue message body""" # fetch players from storage if necessary for team in body['teams']: for player in team: worker.storage.fetch(player['submission_hash'], logger=self.logger) # run the game and log the result current_game = config.get('worker', 'current_game') game_class = worker.games.get_game(current_game) game = game_class(body['teams']) result = game.run() game.stop() self.logger.info('result: ' + repr(result)) self.result_queue.put(result)
def run_game(self, body): """run a game based on the parameters of a queue message body""" # fetch players from storage if necessary teams = self.fetch_and_compile_teams(body['teams']) # run the game and log the result current_game = config.get('worker', 'current_game') game_class = worker.games.get_game(current_game) game = game_class(body, teams) try: result = game.run() self.logger.info('result: ' + repr(result)) self.result_queue.put(result) return True except: return False finally: # Ensure that runner resources are freed game.stop()