-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle exceptions raised due to cache miss
- Loading branch information
Showing
6 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from logging import Logger | ||
|
||
from celery import shared_task | ||
from celery.utils.log import get_task_logger | ||
from django.core.cache import cache | ||
|
||
from care.facility.static_data.icd11 import load_icd11_diagnosis | ||
from care.facility.static_data.medibase import load_medibase_medicines | ||
from care.hcx.static_data.pmjy_packages import load_pmjy_packages | ||
from care.utils.static_data.models.base import index_exists | ||
|
||
logger: Logger = get_task_logger(__name__) | ||
|
||
|
||
@shared_task | ||
def load_redis_index(): | ||
if cache.get("redis_index_loading"): | ||
logger.info("Redis Index already loading, skipping") | ||
return | ||
|
||
cache.set("redis_index_loading", True, timeout=60 * 2) | ||
logger.info("Loading Redis Index") | ||
if index_exists(): | ||
logger.info("Index already exists, skipping") | ||
return | ||
|
||
load_icd11_diagnosis() | ||
load_medibase_medicines() | ||
load_pmjy_packages() | ||
|
||
cache.delete("redis_index_loading") | ||
logger.info("Redis Index Loaded") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from celery import current_app | ||
from django.core.exceptions import ValidationError as DjangoValidationError | ||
from redis_om.model.model import NotFoundError as RedisModelNotFoundError | ||
from rest_framework.exceptions import APIException | ||
from rest_framework.exceptions import ValidationError as DRFValidationError | ||
from rest_framework.fields import get_error_detail | ||
from rest_framework.views import exception_handler as drf_exception_handler | ||
|
||
|
||
def exception_handler(exc, context): | ||
if isinstance(exc, DjangoValidationError): | ||
exc = DRFValidationError(detail={"detail": get_error_detail(exc)[0]}) | ||
|
||
elif isinstance(exc, RedisModelNotFoundError): | ||
current_app.send_task("care.facility.tasks.redis_index.load_redis_index") | ||
exc = APIException( | ||
detail={"detail": "Something went wrong, please try after a few seconds."} | ||
) | ||
|
||
return drf_exception_handler(exc, context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters