Refactor of connection wrapper to handle errors connecting to ES properly.

This commit is contained in:
Ainsey11 2022-04-15 01:13:55 +01:00
parent 848c390bd7
commit d05f9aec51

View File

@ -1,15 +1,31 @@
from elasticsearch import Elasticsearch from multiprocessing import AuthenticationError
from environment import AppConfig from elasticsearch import (
Elasticsearch,
ConnectionError,
ConnectionTimeout,
AuthenticationException,
AuthorizationException,
)
config = AppConfig().config from environment import AppConfig
from time import sleep
class ElasticWrapper: class ElasticWrapper:
""" def handle_err(error):
makes calls to elastic search print("Connection Error: " + str(error))
returns response count print("There was a problem connecting to Elasticsearch")
""" print(
"Please see the error above. This may be as Elasticsearch is still starting up or a misconfiguration"
)
print("Sleeping for 10 seconds...")
sleep(10)
def get_count(index_name):
"""
Returns the number of documents in the index
"""
config = AppConfig().config
es_url = config["es_url"] es_url = config["es_url"]
es_user = config["es_user"] es_user = config["es_user"]
es_pass = config["es_pass"] es_pass = config["es_pass"]
@ -17,14 +33,27 @@ class ElasticWrapper:
es = Elasticsearch( es = Elasticsearch(
[es_url], [es_url],
basic_auth=(es_user, es_pass), basic_auth=(es_user, es_pass),
timeout=30, timeout=10,
max_retries=10, max_retries=12,
retry_on_timeout=True, retry_on_timeout=True,
) )
def get_count(index_name): response = 0
""" try:
Returns the number of documents in the index response = es.count(index=index_name)["count"]
""" except AuthenticationException as e:
response = ElasticWrapper.es.count(index=index_name) ElasticWrapper.handle_err(e)
return response["count"] except ConnectionError as e:
ElasticWrapper.handle_err(e)
except ConnectionTimeout as e:
ElasticWrapper.handle_err(e)
except AuthenticationError as e:
ElasticWrapper.handle_err(e)
except AuthorizationException as e:
ElasticWrapper.handle_err(e)
except:
print("Unknown error occurred. Check your credentials, url and try again.")
print("Sleeping for 10 seconds...")
sleep(10)
else:
return response