Island: Add FileRepositoryLockingDecorator

This commit is contained in:
Mike Salvatore 2022-07-02 09:53:18 -04:00
parent bc0c4a1c8e
commit e3b5e0c01a
2 changed files with 27 additions and 0 deletions

View File

@ -1,6 +1,7 @@
from .errors import RemovalError, RetrievalError, StorageError
from .i_file_repository import FileNotFoundError, IFileRepository
from .local_storage_file_repository import LocalStorageFileRepository
from .file_repository_locking_decorator import FileRepositoryLockingDecorator
from .file_repository_logging_decorator import FileRepositoryLoggingDecorator
from .i_agent_binary_repository import IAgentBinaryRepository
from .agent_binary_repository import AgentBinaryRepository

View File

@ -0,0 +1,26 @@
from multiprocessing import Lock
from typing import BinaryIO
from . import IFileRepository
class FileRepositoryLockingDecorator(IFileRepository):
def __init__(self, file_repository: IFileRepository):
self._file_repository = file_repository
self._lock = Lock()
def save_file(self, unsafe_file_name: str, file_contents: BinaryIO):
with self._lock:
return self._file_repository.save_file(unsafe_file_name, file_contents)
def open_file(self, unsafe_file_name: str) -> BinaryIO:
with self._lock:
return self._file_repository.open_file(unsafe_file_name)
def delete_file(self, unsafe_file_name: str):
with self._lock:
return self._file_repository.delete_file(unsafe_file_name)
def delete_all_files(self):
with self._lock:
return self._file_repository.delete_all_files()