diff --git a/apps/terminal/backends/command/es.py b/apps/terminal/backends/command/es.py index d8197391d..e03847e0a 100644 --- a/apps/terminal/backends/command/es.py +++ b/apps/terminal/backends/command/es.py @@ -7,6 +7,7 @@ import pytz from uuid import UUID import inspect +from django.utils.translation import gettext_lazy as _ from django.db.models import QuerySet as DJQuerySet from elasticsearch import Elasticsearch from elasticsearch.helpers import bulk @@ -14,12 +15,18 @@ from elasticsearch.exceptions import RequestError from common.utils.common import lazyproperty from common.utils import get_logger +from common.exceptions import JMSException from .models import AbstractSessionCommand logger = get_logger(__file__) +class InvalidElasticsearch(JMSException): + default_code = 'invalid_elasticsearch' + default_detail = _('Invalid elasticsearch config') + + class CommandStore(): def __init__(self, config): hosts = config.get("HOSTS") @@ -33,6 +40,8 @@ class CommandStore(): self.es = Elasticsearch(hosts=hosts, max_retries=0, **kwargs) def pre_use_check(self): + if not self.ping(timeout=3): + raise InvalidElasticsearch self._ensure_index_exists() def _ensure_index_exists(self):