Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][IMP] fs_storage: invalidate orm cache when connection fails #384

Open
wants to merge 5 commits into
base: 17.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fs_attachment/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def _get_fs_storage_for_code(
code: str,
) -> fsspec.AbstractFileSystem | None:
"""Return the filesystem for the given storage code"""
fs = self.env["fs.storage"].get_fs_by_code(code)
fs = self.env["fs.storage"].sudo().get_fs_by_code(code)
if not fs:
raise SystemError(f"No Filesystem storage for code {code}")
return fs
Expand Down
52 changes: 50 additions & 2 deletions fs_storage/models/fs_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,32 @@
return [s.code for s in self.search([])]

@api.model
@tools.ormcache("code")
def get_fs_by_code(self, code):
"""Return the filesystem associated to the given code.

:param code: the code of the filesystem
"""
fs = self._get_fs_by_code(code)

if not tools.config["test_enable"]:
try:
self._check_connection(fs)
except Exception as e:
_logger.warning(

Check warning on line 208 in fs_storage/models/fs_storage.py

View check run for this annotation

Codecov / codecov/patch

fs_storage/models/fs_storage.py#L205-L208

Added lines #L205 - L208 were not covered by tests
"Error while connecting to the filesystem storage %s: %s",
code,
e,
)
# Generate a new fs instance
self.env.registry.clear_cache()
fs = self._get_fs_by_code(code)

Check warning on line 215 in fs_storage/models/fs_storage.py

View check run for this annotation

Codecov / codecov/patch

fs_storage/models/fs_storage.py#L214-L215

Added lines #L214 - L215 were not covered by tests
return fs

@api.model
@tools.ormcache("code")
def _get_fs_by_code(self, code):
"""Return the filesystem associated to the given code.

:param code: the code of the filesystem
"""
fs = None
Expand Down Expand Up @@ -265,12 +287,38 @@
doc = inspect.getdoc(cls.__init__)
rec.options_properties = f"__init__{signature}\n{doc}"

@api.model
def _get_marker_file_name(self):
return ".odoo_fs_storage.marker"

Check warning on line 292 in fs_storage/models/fs_storage.py

View check run for this annotation

Codecov / codecov/patch

fs_storage/models/fs_storage.py#L292

Added line #L292 was not covered by tests

@api.model
def _marker_file_check_connection(self, fs):
marker_file_name = self._get_marker_file_name()
try:
fs.info(marker_file_name)
except FileNotFoundError:
fs.touch(marker_file_name)

Check warning on line 300 in fs_storage/models/fs_storage.py

View check run for this annotation

Codecov / codecov/patch

fs_storage/models/fs_storage.py#L296-L300

Added lines #L296 - L300 were not covered by tests

@api.model
def _check_connection(self, fs):
self._marker_file_check_connection(fs)
return True

Check warning on line 305 in fs_storage/models/fs_storage.py

View check run for this annotation

Codecov / codecov/patch

fs_storage/models/fs_storage.py#L304-L305

Added lines #L304 - L305 were not covered by tests

@property
def fs(self) -> fsspec.AbstractFileSystem:
"""Get the fsspec filesystem for this backend."""
self.ensure_one()
if not self.__fs:
self.__fs = self._get_filesystem()
if not tools.config["test_enable"]:
# Check whether we need to invalidate FS cache or not.
# Use a marker file to limit the scope of the LS command for performance.
try:
self._check_connection(self.__fs)
except Exception as e:
self.__fs.clear_instance_cache()
self.__fs = None
raise e

Check warning on line 321 in fs_storage/models/fs_storage.py

View check run for this annotation

Codecov / codecov/patch

fs_storage/models/fs_storage.py#L316-L321

Added lines #L316 - L321 were not covered by tests
return self.__fs

def _get_filesystem_storage_path(self) -> str:
Expand Down Expand Up @@ -432,7 +480,7 @@

def action_test_config(self) -> None:
try:
self.fs.ls("", detail=False)
self._check_connection(self.fs)

Check warning on line 483 in fs_storage/models/fs_storage.py

View check run for this annotation

Codecov / codecov/patch

fs_storage/models/fs_storage.py#L483

Added line #L483 was not covered by tests
title = _("Connection Test Succeeded!")
message = _("Everything seems properly set up!")
msg_type = "success"
Expand Down
Loading