From 2d647819783be1c22ce02a615d07a819f95082ce Mon Sep 17 00:00:00 2001 From: Parzival-05 Date: Tue, 20 Aug 2024 21:18:51 +0300 Subject: [PATCH] Move game error handling to 'GameError' --- AIAgent/ml/game/errors_game.py | 32 +++++++++++++++++++++++++++++--- AIAgent/ml/game/play_game.py | 22 +++------------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/AIAgent/ml/game/errors_game.py b/AIAgent/ml/game/errors_game.py index 67887d1..75965e5 100644 --- a/AIAgent/ml/game/errors_game.py +++ b/AIAgent/ml/game/errors_game.py @@ -1,13 +1,39 @@ +import logging +import traceback +from func_timeout import FunctionTimedOut + +from config import FeatureConfig +from ml.protocols import Predictor from ml.training.epochs_statistics import StatisticsCollector -from connection.errors_connection import GameInterruptedError +from connection.errors_connection import GameInterruptedError, ProcessStoppedError from common.game import GameMap2SVM class GameError(Exception): - def __init__(self, game_map2svm: GameMap2SVM, error: Exception) -> None: + + def __init__( + self, with_predictor: Predictor, game_map2svm: GameMap2SVM, error: Exception + ) -> None: self._map = game_map2svm self._error = error - super().__init__(GameError, self._error) + + name_of_predictor = with_predictor.name() + need_to_save = True + + if isinstance(error, FunctionTimedOut): + log_message = f"<{name_of_predictor}> timeouted on map {game_map2svm.GameMap.MapName} with {error.timedOutAfter}s" + elif isinstance(error, ProcessStoppedError): + log_message = f"<{name_of_predictor}> failed on map {game_map2svm.GameMap.MapName}: process suddenly disappeared" + need_to_save = False + else: + log_message = f"<{name_of_predictor}> failed on map {game_map2svm.GameMap.MapName}:\n{traceback.format_exc()}" + logging.warning(log_message) + + if need_to_save: + FeatureConfig.SAVE_IF_FAIL_OR_TIMEOUT.save_model( + with_predictor.model(), with_name=name_of_predictor + ) + super().__init__(with_predictor, game_map2svm, error) def handle_error(self, statistics_collector: StatisticsCollector): if isinstance(self._error, GameInterruptedError): diff --git a/AIAgent/ml/game/play_game.py b/AIAgent/ml/game/play_game.py index 0dc4eb5..594fc2e 100644 --- a/AIAgent/ml/game/play_game.py +++ b/AIAgent/ml/game/play_game.py @@ -1,15 +1,11 @@ import logging from time import perf_counter -import traceback from typing import TypeAlias from common.classes import GameResult, Map2Result from common.game import GameState, GameMap2SVM from config import FeatureConfig from connection.broker_conn.socket_manager import game_server_socket_manager -from connection.errors_connection import ( - ProcessStoppedError, -) from connection.game_server_conn.connector import Connector from func_timeout import FunctionTimedOut, func_set_timeout from ml.protocols import Predictor @@ -168,19 +164,7 @@ def play_game( ) map2result = Map2Result(game_map2svm, game_result) except (FunctionTimedOut, Exception) as error: - need_to_save = True - if isinstance(error, FunctionTimedOut): - log_message = f"<{with_predictor.name()}> timeouted on map {game_map2svm.GameMap.MapName} with {error.timedOutAfter}s" - elif isinstance(error, ProcessStoppedError): - log_message = f"<{with_predictor.name()}> failed on map {game_map2svm.GameMap.MapName}: process suddenly disappeared" - need_to_save = False - else: - log_message = f"<{with_predictor.name()}> failed on map {game_map2svm.GameMap.MapName}:\n{traceback.format_exc()}" - logging.warning(log_message) - if need_to_save: - FeatureConfig.SAVE_IF_FAIL_OR_TIMEOUT.save_model( - with_predictor.model(), with_name=f"{with_predictor.name()}" - ) - raise GameError(game_map2svm, error) - + raise GameError( + with_predictor=with_predictor, game_map2svm=game_map2svm, error=error + ) return map2result