Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Use cached_property to delay elastic init.
Browse files Browse the repository at this point in the history
Importing a `storage` had been triggering a call to `ElasticSearch`. Delaying
until first use resolves this problem (which is particularly problematic in
testing).
  • Loading branch information
CM Lubinski committed Aug 4, 2017
1 parent 50a7bef commit 41ccb98
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions regcore/db/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
etc.), implemented using Elastic Search as a data store"""
import logging

from cached_property import cached_property
from django.conf import settings
from pyelasticsearch import ElasticSearch
from pyelasticsearch.exceptions import ElasticHttpNotFoundError
Expand All @@ -18,8 +19,10 @@ def sanitize_doc_id(doc_id):

class ESBase(object):
"""Shared code for Elastic Search storage models"""
def __init__(self):
self.es = ElasticSearch(settings.ELASTIC_SEARCH_URLS)

@cached_property
def es(self):
return ElasticSearch(settings.ELASTIC_SEARCH_URLS)

def safe_fetch(self, doc_type, es_id):
"""Attempt to retrieve a document from Elastic Search.
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
packages=find_packages(),
include_package_data=True,
install_requires=[
'cached_property',
'django>=1.8,<1.12',
'django-mptt',
'jsonschema',
Expand Down

0 comments on commit 41ccb98

Please sign in to comment.