🚧 Thread-safe manager error reporting
This commit is contained in:
@@ -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,14 +30,17 @@ 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"""
|
||||||
|
with self.errors_lock:
|
||||||
self.errors.append(error)
|
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:
|
||||||
|
errors = self.errors.copy()
|
||||||
self.errors.clear()
|
self.errors.clear()
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user