def send_match_to_worker(self, match, workerfile): worker_data = aiweb_tools.worker.Worker_data() worker_data.read(workerfile) filepath = config.matchmaker_path + "match" + config.delimiter + worker_data.uuid + match.short_string() match.set_for_worker(worker_data.uuid) match.write_file(filepath) comms.send_task_worker_ip(filepath, worker_data.ip_addr) comms.delete_file(filepath)
def request_task(self): """ Send a task request to the task server""" print("Requesting task") srcname = "worker-ready" + config.delimiter + (datetime.datetime.now().isoformat()).replace(":", "-") + config.delimiter + self.uuid.hex f = open(srcname, 'w') f.write(self.task_request_content()) f.close() comms.send_task_worker_ip (srcname, config.task_ip) comms.delete_file(srcname)
def assign_tasks(): """ Assign tasks to all waiting workers """ for file in glob.glob(config.task_worker_path + "worker-ready*"): ending = ".ready" if (file.endswith(ending)): real = file[:-len(ending)] if find_task(real): print(real) comms.delete_file(file) comms.delete_file(real)
def send_match_to_worker(self, match, workerfile): try: worker_data = aiweb_tools.worker.Worker_data() worker_data.read(workerfile) filepath = config.matchmaker_path + "match" + config.delimiter + worker_data.uuid + match.short_string() match.set_for_worker(worker_data.uuid) match.write_file(filepath) comms.send_task_worker_ip(filepath, worker_data.ip_addr) comms.delete_file(filepath) except FileNotFoundError: print("WARNING: worker file " + workerfile + " does not exist")
def send_task_to_worker(task, worker_file): """ send task to the worker specified in worker_file """ print("send_task_to_worker") with open(worker_file) as worker_fo: worker_ip = worker_fo.readline().strip() worker_id = worker_fo.readline().strip() print(worker_ip) with open (task, "a") as taskfile: taskfile.write(" " + worker_id) print("id = " + worker_id) comms.send_task_worker_ip(task, worker_ip) comms.delete_file(task)
def process_report(path, add_submission_report): """ Process the submission report found at path """ real = path[:-len('.ready')] filename = real.split('/')[-1] fsplit = filename.split('.') prefix = fsplit[0] + "." + fsplit[1].split('-')[0] parts = prefix.split(config.delimiter) username = parts[0] game = parts[1] timestamp = parts[2] with open(real) as fo: status = fo.readline().strip() language = fo.readline().strip() content = fo.readlines() print(username) print(game) print(timestamp) print(prefix) print(status) print(content) add_submission_report(username, game, timestamp, prefix, status, language, "".join(content)) comms.delete_file(real) comms.delete_file(path)
def await_request(self): stopfile = (config.matchmaker_path + "stop_matchmaker") while not os.path.isfile(stopfile): ready = ".ready" for file in glob.glob(config.matchmaker_path + "*" + ready): print(file) real = (file[:-len(ready)]) self.process_request(real) comms.delete_file(real) comms.delete_file(file) time.sleep(config.sleeptime) #print("checking for requests") comms.delete_file(stopfile)
def await_task(self): """ Wait for a task and complete it if appropriate """ stopfile = (config.task_worker_path + "stop" + config.delimiter + self.uuid.hex) while not os.path.isfile(stopfile): ready = ".ready" for file in glob.glob(config.task_worker_path + "*" + ready): # print(file) real = (file[:-len(ready)]) prefix = file.split('/')[-1] if (prefix.startswith("worker-ready")): pass else: if (self.task_is_mine(real)): self.do_task(real) comms.delete_file(real) comms.delete_file(file) else: lockfile = file + ".lock" try: with open(lockfile, 'x') as fo: self.do_task(real) comms.delete_file(real) comms.delete_file(file) comms.delete_file(lockfile) except OSError as e: pass time.sleep(config.sleeptime) self.request_task() print("checking for tasks") comms.delete_file(stopfile)
def await_task(self): """ Wait for a task and complete it if appropriate """ notask_count = 0 notask_threshhold = 50 #if no tasks in 50 checks, request again notask_sleeptime = 3 #if no task, sleep for n seconds stopfile = (config.task_worker_path + "stop" + config.delimiter + self.uuid.hex) while not os.path.isfile(stopfile): tasks_done = 0 ready = ".ready" for file in glob.glob(config.task_worker_path + "*" + ready): # print(file) real = (file[:-len(ready)]) prefix = file.split('/')[-1] if (prefix.startswith("worker-ready")): pass else: if (self.task_is_mine(real)): self.do_task(real) comms.delete_file(real) comms.delete_file(file) tasks_done += 1 else: lockfile = file + ".lock" try: with open(lockfile, 'x') as fo: self.do_task(real) comms.delete_file(real) comms.delete_file(file) comms.delete_file(lockfile) tasks_done += 1 except OSError as e: pass time.sleep(config.sleeptime) if tasks_done > 0: self.request_task() else: time.sleep(notask_sleeptime) notask_count +=1 if notask_count > notask_threshhold: notask_count = 0 self.request_task() print("checking for tasks") comms.delete_file(stopfile)
def process_match_result(path): """ Read match result from path and save new result object to db """ real = path[:-len('.ready')] print("processing match result: " + real) with open(real, 'rb') as fo: result_dict = cloudpickle.load(fo) replay_id = comms.get_replay_id() replay_path = config.webserver_results_path + str(replay_id) + ".replay" if 'scores' in result_dict: scores = " ".join([str(x) for x in result_dict['score']]) else: scores = "" if 'status' in result_dict: status= " ".join([str(x) for x in result_dict['status']]) else: status = "" if 'rank' in result_dict: rank= " ".join([str(x) for x in result_dict['rank']]) else: rank = "" errors = [] if 'errors' in result_dict: for error in result_dict['errors']: err_obj = aiweb.models.BotError.objects.create(text = error) errors.append(err_obj) if 'challenge' in result_dict: result = aiweb.models.Result.objects.create( gamename = result_dict['challenge'], player_names = " ".join(result_dict['playernames']), scores = scores, statuses = status, ranks = rank, game_message = "", replay = replay_path.split("/")[-1]) if 'bot_ids' in result_dict: submissions = [] for bot_id in result_dict['bot_ids']: try: submission = aiweb.models.Submission.objects.get(submission_id = bot_id) submission.games_played += 1 submission.save() result.submissions.add(submission) submissions.append(submission) except KeyError: # FIXME logging pass if len(submissions) == len(result_dict['rank']): update_ranks(submissions, result_dict['rank']) else: # FIXME logging print("num submissions and num ranks differ!") for error in errors: error.save() print(error.text) result.bot_errors.add(error) result.save() else: # FIXME logging print("Error: 'challenge' not found in result_dict:\n" + str(result_dict)) if 'replaydata' in result_dict: replay = result_dict['replaydata'] replay['gamename'] = result_dict['challenge'] with open(replay_path, 'w') as fo: json.dump(replay, fo) comms.delete_file(real) comms.delete_file(path)