🎨 Improved manager error handling structure

This commit is contained in:
GeoffreyCoulaud
2023-06-10 14:51:52 +02:00
parent e7fd01f509
commit 8eb2a270f6

View File

@@ -58,42 +58,54 @@ class Manager:
* May raise other exceptions that will be reported * May raise other exceptions that will be reported
""" """
def execute_resilient_manager_logic( def execute_resilient_manager_logic(self, game: Game, additional_data: dict):
self, game: Game, additional_data: dict, try_index: int = 0 """Handle errors (retry, ignore or raise) that occur the manager logic"""
) -> None:
"""Execute the manager logic and handle its errors by reporting them or retrying""" # Keep track of the number of tries
try: tries = 1
self.manager_logic(game, additional_data)
except Exception as error: # pylint: disable=broad-exception-caught def handle_error(error: Exception):
logging_args = ( nonlocal tries
log_args = (
type(error).__name__, type(error).__name__,
self.name, self.name,
f"{game.name} ({game.game_id})", f"{game.name} ({game.game_id})",
) )
out_of_retries_format = "Out of retries dues to %s in %s for %s"
retrying_format = "Retrying %s in %s for %s"
unretryable_format = "Unretryable %s in %s for %s"
if error in self.continue_on: if error in self.continue_on:
# Handle skippable errors (skip silently) # Handle skippable errors (skip silently)
return return
if error in self.retryable_on: if error in self.retryable_on:
if try_index < self.max_tries: if tries > self.max_tries:
# Handle retryable errors
logging.error("Retrying %s in %s for %s", *logging_args)
sleep(self.retry_delay)
self.execute_resilient_manager_logic(
game, additional_data, try_index + 1
)
else:
# Handle being out of retries # Handle being out of retries
logging.error( logging.error(out_of_retries_format, *log_args)
"Out of retries dues to %s in %s for %s", *logging_args
)
self.report_error(error) self.report_error(error)
else:
# Handle retryable errors
logging.error(retrying_format, *log_args)
sleep(self.retry_delay)
tries += 1
try_manager_logic()
else: else:
# Handle unretryable errors # Handle unretryable errors
logging.error( logging.error(unretryable_format, *log_args, exc_info=error)
"Unretryable %s in %s for %s", *logging_args, exc_info=error
)
self.report_error(error) self.report_error(error)
def try_manager_logic():
try:
self.manager_logic(game, additional_data)
except Exception as error: # pylint: disable=broad-exception-caught
handle_error(error)
try_manager_logic()
def process_game( def process_game(
self, game: Game, additional_data: dict, callback: Callable[["Manager"], Any] self, game: Game, additional_data: dict, callback: Callable[["Manager"], Any]
) -> None: ) -> None: