def apiCompleteJob(authable): req = request.get_json(silent=True) try: job = Job.fromJson(req['job']) insertRes = env.gameApi.writeAnalysedGames(req['analysedGames']) if insertRes: env.queue.completeEngineAnalysis(job.playerId) player = env.irwin.env.playerDB.byId(job.playerId) analysedGames = env.irwin.env.analysedGameDB.byPlayerId( job.playerId) games = env.irwin.env.gameDB.byIds( [ag.gameId for ag in analysedGames]) predictions = env.irwin.analysedGameModel.predict([ GameAnalysedGame(ag, g) for ag, g in zip(analysedGames, games) if ag.gameLength() <= 60 ]) playerReport = PlayerReport.new(player, zip(analysedGames, predictions), owner=authable.name) logging.warning( f'Sending player report for {playerReport.playerId}, activation {playerReport.activation}%' ) env.lichessApi.postReport(playerReport) return Success except KeyError as e: tb = traceback.format_exc() logging.warning(f'Error completing job: {tb}') return BadRequest
def apiRequestJob(authable): engineQueue = env.queue.nextEngineAnalysis(authable.id) logging.debug(f'EngineQueue for req {engineQueue}') if engineQueue is not None: games = env.gameApi.gamesByIds(engineQueue.requiredGameIds) logging.warning( f'Requesting {authable.name} analyses {engineQueue.requiredGameIds} for {engineQueue.id}' ) job = Job(playerId=engineQueue.id, games=games, analysedPositions=[]) logging.info(f'Job: {job}') return Response(response=json.dumps(job.toJson()), status=200, mimetype='application/json') return NotAvailable
def requestJob(self) -> Opt[Dict]: for i in range(5): try: result = requests.get(f'{self.env.url}/api/request_job', json={'auth': self.env.auth}) return Job.fromJson(result.json()) except (json.decoder.JSONDecodeError, requests.ConnectionError, requests.exceptions.SSLError): logging.warning( f"Error in request job. Trying again in 10 sec. Received: {result.text}" ) time.sleep(10) return None
def completeJob(self, job: Job, analysedGames: List[AnalysedGame]) -> Opt[Response]: payload = { 'auth': self.env.auth, 'job': job.toJson(), 'analysedGames': [ag.toJson() for ag in analysedGames] } for i in range(5): try: result = requests.post(f'{self.env.url}/api/complete_job', json=payload) return result except (json.decoder.JSONDecodeError, requests.ConnectionError, requests.exceptions.SSLError): logging.warning( 'Error in completing job. Trying again in 10 sec') time.sleep(10) return None