Skip to content

Commit

Permalink
Possibility to ignore cache errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mikicz committed Feb 15, 2018
1 parent 1720905 commit 27d1b75
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
32 changes: 24 additions & 8 deletions arca/_arca.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,33 @@

class Arca:

single_pull: bool = LazySettingProperty(key="single_pull", default=False)
single_pull: bool = LazySettingProperty(key="single_pull", default=False, convert=bool)
base_dir: str = LazySettingProperty(key="base_dir", default=".arca")
ignore_cache_errors: bool = LazySettingProperty(key="ignore_cache_errors", default=False, convert=bool)

def __init__(self, backend: BackendDefinitionType=NOT_SET,
settings=None,
single_pull=None,
base_dir=None) -> None:
base_dir=None,
ignore_cache_errors=None) -> None:
self.settings: Settings = self._get_settings(settings)

self.region: CacheRegion = self._make_region()

self.backend: BaseBackend = self._get_backend_instance(backend)
self.backend.inject_arca(self)

self._current_hash = defaultdict(lambda: {})
if ignore_cache_errors is not None:
self.ignore_cache_errors = bool(ignore_cache_errors)

if single_pull is not None:
self.single_pull = bool(single_pull)

if base_dir is not None:
self.base_dir = base_dir

self.region: CacheRegion = self._make_region()

self.backend: BaseBackend = self._get_backend_instance(backend)
self.backend.inject_arca(self)

self._current_hash = defaultdict(lambda: {})

def _get_backend_instance(self, backend: BackendDefinitionType) -> BaseBackend:
if backend is NOT_SET:
backend = self.get_setting("backend", "arca.backend.VenvBackend")
Expand Down Expand Up @@ -72,10 +77,17 @@ def _get_settings(self, settings: Optional[Dict[str, Any]]) -> Settings:
def _make_region(self) -> CacheRegion:
arguments = self.get_setting("cache_backend_arguments", None)

def null_cache():
return make_region().configure(
"dogpile.cache.null"
)

if isinstance(arguments, str) and arguments:
try:
arguments = json.loads(arguments)
except ValueError:
if self.ignore_cache_errors:
return null_cache()
raise ArcaMisconfigured("Cache backend arguments couldn't be converted to a dictionary.")

try:
Expand All @@ -86,8 +98,12 @@ def _make_region(self) -> CacheRegion:
)
region.set("last_arca_run", datetime.now().isoformat())
except ModuleNotFoundError:
if self.ignore_cache_errors:
return null_cache()
raise ModuleNotFoundError("Cache backend cannot load a required library.")
except Exception:
if self.ignore_cache_errors:
return null_cache()
raise ArcaMisconfigured("The provided cache is not working - most likely misconfigured.")

return region
Expand Down
33 changes: 33 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,36 @@ def test_invalid_arguments():
"ARCA_CACHE_BACKEND": "dogpile.cache.dbm",
"ARCA_CACHE_BACKEND_ARGUMENTS": json.dumps({"filename": str(Path(BASE_DIR) / "cachefile.dbm")})[:-1]
})

# in case ignore is set, no error thrown, region configred
arca = Arca(base_dir=BASE_DIR,
single_pull=True,
ignore_cache_errors=True,
settings={
"ARCA_CACHE_BACKEND": "dogpile.cache.dbm",
"ARCA_CACHE_BACKEND_ARGUMENTS": json.dumps({"filename": str(Path(BASE_DIR) / "cachefile.dbm")})[:-1]
})

assert arca.region.is_configured


def test_cache_backend_module_not_found():
# redis must not be present in the env
with pytest.raises(ImportError):
import redis # noqa

with pytest.raises(ModuleNotFoundError):
Arca(base_dir=BASE_DIR,
single_pull=True,
settings={
"ARCA_CACHE_BACKEND": "dogpile.cache.redis"
})

arca = Arca(base_dir=BASE_DIR,
single_pull=True,
ignore_cache_errors=True,
settings={
"ARCA_CACHE_BACKEND": "dogpile.cache.redis"
})

assert arca.region.is_configured

0 comments on commit 27d1b75

Please sign in to comment.