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
1 changed files with 50 additions and 21 deletions

View File

@ -1,30 +1,59 @@
from elasticsearch import Elasticsearch
from environment import AppConfig
from multiprocessing import AuthenticationError
from elasticsearch import (
Elasticsearch,
ConnectionError,
ConnectionTimeout,
AuthenticationException,
AuthorizationException,
)
config = AppConfig().config
from environment import AppConfig
from time import sleep
class ElasticWrapper:
"""
makes calls to elastic search
returns 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=30,
max_retries=10,
retry_on_timeout=True,
)
def handle_err(error):
print("Connection Error: " + str(error))
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
"""
response = ElasticWrapper.es.count(index=index_name)
return response["count"]
config = AppConfig().config
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