🚧 Thread-safe manager error reporting

This commit is contained in:
GeoffreyCoulaud
2023-06-03 14:18:07 +02:00
parent 06b6ee4593
commit 10a635fc78

View File

@@ -1,6 +1,7 @@
import logging import logging
from abc import abstractmethod from abc import abstractmethod
from typing import Any, Callable from typing import Any, Callable
from threading import Lock
from src.game import Game from src.game import Game
@@ -20,6 +21,7 @@ class Manager:
max_tries: int = 3 max_tries: int = 3
errors: list[Exception] errors: list[Exception]
errors_lock: Lock = None
@property @property
def name(self): def name(self):
@@ -28,15 +30,18 @@ class Manager:
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
self.errors = [] self.errors = []
self.errors_lock = Lock()
def report_error(self, error: Exception): def report_error(self, error: Exception):
"""Report an error that happened in Manager.run""" """Report an error that happened in Manager.run"""
self.errors.append(error) with self.errors_lock:
self.errors.append(error)
def collect_errors(self) -> list[Exception]: def collect_errors(self) -> list[Exception]:
"""Get the errors produced by the manager and remove them from self.errors""" """Get the errors produced by the manager and remove them from self.errors"""
errors = list(self.errors) with self.errors_lock:
self.errors.clear() errors = self.errors.copy()
self.errors.clear()
return errors return errors
@abstractmethod @abstractmethod