Skip to content

Commit

Permalink
logger error fix, added elasticsearck_max_timeout for db callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
milashensky committed Aug 6, 2018
1 parent e7a4e6a commit 8673d71
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions django_elasticsearch/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import logging
import requests

from django import get_version
from django.conf import settings
Expand All @@ -14,7 +15,7 @@
from django_elasticsearch.serializers import EsJsonSerializer
from django_elasticsearch.managers import ElasticsearchManager

from elasticsearch.exceptions import ConnectionError
from elasticsearch.exceptions import ConnectionError as ElasticConnectionError

logger = logging.getLogger(__name__)

Expand All @@ -41,7 +42,7 @@ class Elasticsearch:
completion_fields = None

def __init__(self, *args, **kwargs):
super(EsIndexable, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
# override the manager because we have an instance now
self.es = ElasticsearchManager(self)

Expand All @@ -64,6 +65,8 @@ def add_es_manager(sender, **kwargs):
# Note: the manager needs to know the subclass
if issubclass(sender, EsIndexable):
sender.es = ElasticsearchManager(sender)


class_prepared.connect(add_es_manager)


Expand All @@ -72,18 +75,20 @@ def es_save_callback(sender, instance, **kwargs):
if not issubclass(sender, EsIndexable):
return
try:
requests.get(settings.ELASTICSEARCH_URL, timeout=getattr(settings, 'ELASTICSEARCH_MAX_TIMEOUT', None))
instance.es.do_index()
except ConnectionError:
logger.error("ElasticSearch is do not responding")
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout, ElasticConnectionError) as e:
logger.error("ElasticSearch is do not responding, %s" % e)


def es_delete_callback(sender, instance, **kwargs):
if not issubclass(sender, EsIndexable):
return
try:
requests.get(settings.ELASTICSEARCH_URL, timeout=getattr(settings, 'ELASTICSEARCH_MAX_TIMEOUT', None))
instance.es.delete()
except ConnectionError:
logger.error("ElasticSearch is do not responding")
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout, ElasticConnectionError) as e:
logger.error("ElasticSearch is do not responding, %s" % e)


def es_syncdb_callback(sender, app=None, created_models=[], **kwargs):
Expand All @@ -95,10 +100,11 @@ def es_syncdb_callback(sender, app=None, created_models=[], **kwargs):
for model in models:
if issubclass(model, EsIndexable):
try:
requests.get(settings.ELASTICSEARCH_URL, timeout=getattr(settings, 'ELASTICSEARCH_MAX_TIMEOUT', None))
model.es.create_index()
model.es.reindex_all()
except ConnectionError:
logger.error("ElasticSearch is do not responding")
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout, ElasticConnectionError) as e:
logger.error("ElasticSearch is do not responding, %s" % e)


if getattr(settings, 'ELASTICSEARCH_AUTO_INDEX', False):
Expand Down

0 comments on commit 8673d71

Please sign in to comment.