Skip to content

Instantly share code, notes, and snippets.

@brettbeeson
Created July 16, 2023 05:52
Show Gist options
  • Save brettbeeson/23b1bd132d0682b18eb91c755e97b404 to your computer and use it in GitHub Desktop.
Save brettbeeson/23b1bd132d0682b18eb91c755e97b404 to your computer and use it in GitHub Desktop.
A mixin for Redis OM JsonModel which silently ignores connection errors.
class IgnoranceMixin:
"""
Use as first parent, like:
```
class InfluxQuery(IgnoranceMixin, JsonModel):
...
```
Basic calls (get,set,init), try to call Redis but silently
fallover to doing nothing upon connection errors
All get() calls raise NotFoundError.
Not all calls are implemented
If ignorances>0, we have ignored an error
"""
ignorances = 0
def __init__(self, **data):
# Call grandparent directly...
RedisModel.__init__(self, **data)
try:
# ... skipping JsonModel as it raises exception so do its work
if not redis_om.checks.has_redis_json(self.db()):
redis_om.model.log.error(
"Your Redis instance does not have the RedisJson module "
"loaded. JsonModel depends on RedisJson."
)
except redis.exceptions.ConnectionError:
IgnoranceMixin.ignorances += 1
def set(self, *args, **kwargs):
try:
return super().set(*args, **kwargs)
except redis.exceptions.ConnectionError:
IgnoranceMixin.ignorances += 1
return None
@classmethod
def get(cls, pk):
try:
return super().get(pk)
except redis.exceptions.ConnectionError:
IgnoranceMixin.ignorances += 1
raise redis_om.model.NotFoundError("Ignorance is bliss")
def save(self, *args, **kwargs):
try:
return super().save(*args, **kwargs)
except redis.exceptions.ConnectionError:
self.ignorances += 1
return None
def expire(self, *args, **kwargs):
try:
return super().expire(*args, **kwargs)
except redis.exceptions.ConnectionError:
self.ignorances += 1
return None
def update(self, *args, **kwargs):
try:
return super().update(*args, **kwargs)
except redis.exceptions.ConnectionError:
self.ignorances += 1
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment