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,30 +1,59 @@
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"
es_url = config["es_url"] )
es_user = config["es_user"] print("Sleeping for 10 seconds...")
es_pass = config["es_pass"] sleep(10)
es = Elasticsearch(
[es_url],
basic_auth=(es_user, es_pass),
timeout=30,
max_retries=10,
retry_on_timeout=True,
)
def get_count(index_name): def get_count(index_name):
""" """
Returns the number of documents in the index Returns the number of documents in the index
""" """
response = ElasticWrapper.es.count(index=index_name) config = AppConfig().config
return response["count"] es_url = config["es_url"]
es_user = config["es_user"]
es_pass = config["es_pass"]
es = Elasticsearch(
[es_url],
basic_auth=(es_user, es_pass),
timeout=10,
max_retries=12,
retry_on_timeout=True,
)
response = 0
try:
response = es.count(index=index_name)["count"]
except AuthenticationException as e:
ElasticWrapper.handle_err(e)
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