Skip to content

Commit

Permalink
Merge pull request #1994 from uktrade/LTD-4939-revert-optimise-assess…
Browse files Browse the repository at this point in the history
…ment-endpoint

Revert changes which introduced celery-based elasticsearch signal processing
  • Loading branch information
currycoder authored May 17, 2024
2 parents e23c400 + 712b562 commit ad396c2
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 142 deletions.
3 changes: 0 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ jobs:
- <<: *image_python
- <<: *image_postgres
- <<: *image_elasticsearch
- <<: *image_redis
working_directory: ~/lite-api
environment:
<<: *common_env_vars
Expand Down Expand Up @@ -234,7 +233,6 @@ jobs:
- <<: *image_python
- <<: *image_postgres
- <<: *image_elasticsearch
- <<: *image_redis
working_directory: ~/lite-api
environment:
<<: *common_env_vars
Expand All @@ -253,7 +251,6 @@ jobs:
- <<: *image_python
- <<: *image_postgres
- <<: *image_elasticsearch
- <<: *image_redis
working_directory: ~/lite-api
environment:
<<: *common_env_vars
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ faker = "~=23.2.1"
boto3 = "~=1.26.17"
django-activity-stream = "~=0.10.0"
django-allow-cidr = "~=0.5.0"
django-elasticsearch-dsl = "~=7.4"
django-elasticsearch-dsl = "~=7.2.2"
django-elasticsearch-dsl-drf = "~=0.22.5"
django-environ = "~=0.9.0"
django-health-check = "~=3.18.1"
Expand Down
124 changes: 62 additions & 62 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion api/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ def _build_redis_url(base_url, db_number, **query_args):
ELASTICSEARCH_DSL = {
"default": {"hosts": env.str("ELASTICSEARCH_HOST")},
}
ELASTICSEARCH_DSL_SIGNAL_PROCESSOR = "api.search.signals.ElasticsearchDSLSignalProcessor"

ENABLE_SPIRE_SEARCH = env.bool("ENABLE_SPIRE_SEARCH", False)

Expand Down
File renamed without changes.
6 changes: 0 additions & 6 deletions api/search/application/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.db.models import Prefetch

from api.applications import models
from api.cases.models import CaseAssignment


address_analyzer = analysis.analyzer(
Expand Down Expand Up @@ -258,11 +257,6 @@ class Meta:

class Django:
model = models.BaseApplication
related_models = [CaseAssignment]

def get_instances_from_related(self, related_instance):
if isinstance(related_instance, CaseAssignment):
return related_instance.case.baseapplication

def get_queryset(self):
return super().get_queryset().exclude(status__status="draft")
Expand Down
27 changes: 7 additions & 20 deletions api/search/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

from django_elasticsearch_dsl.signals import CelerySignalProcessor
from django_elasticsearch_dsl.registries import registry

from api.applications.models import GoodOnApplication
from api.applications.models import BaseApplication, GoodOnApplication
from api.goods.models import Good
from api.search.celery_tasks import update_search_index

Expand All @@ -32,6 +29,9 @@ def update_search_documents(sender, **kwargs):
"""
to_update = []

if issubclass(sender, BaseApplication):
to_update.append((application_document_model, instance.baseapplication.pk))

if issubclass(sender, GoodOnApplication):
to_update.append((product_document_model, instance.pk))

Expand All @@ -40,7 +40,9 @@ def update_search_documents(sender, **kwargs):
to_update.append((product_document_model, good_on_application.pk))

try:
if app_label == "cases" and model_name == "case":
if app_label == "cases" and model_name == "caseassignment":
to_update.append((application_document_model, instance.case.baseapplication.pk))
elif app_label == "cases" and model_name == "case":
to_update.append((application_document_model, instance.baseapplication.pk))
elif app_label == "goods" and model_name == "good":
for good in instance.goods_on_application.all():
Expand All @@ -57,18 +59,3 @@ def update_search_documents(sender, **kwargs):
if to_update:
to_update = [(model_name, str(pk)) for model_name, pk in to_update]
update_search_index.delay(to_update)


class ElasticsearchDSLSignalProcessor(CelerySignalProcessor):

def handle_save(self, sender, instance, **kwargs):
"""
Custom save handler which firstly checks with the registry whether the
current model is recorded in elasticsearch. This avoids queueing unnecessary
celery tasks.
"""
model_registered = instance.__class__ in registry._models
model_related_to_registered = len(list(registry._get_related_doc(instance))) > 0
if not (model_registered or model_related_to_registered):
return
super().handle_save(sender, instance, **kwargs)
Loading

0 comments on commit ad396c2

Please sign in to comment.