Commit 6db708d5 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 4e2dfd87
......@@ -10,6 +10,7 @@
# @Description :
"""
import time
from typing import Any
from elasticsearch import Elasticsearch
......@@ -65,11 +66,17 @@ class FlaskElasticsearch:
if not hasattr(ctx, 'elasticsearch'):
cfg = self._get_config()
ctx.elasticsearch = Elasticsearch(**cfg)
if ctx.elasticsearch.ping():
logger.info('Connected to Elasticsearch')
# Retry connection on failure
for i in range(5): # Retry up to 5 times
if ctx.elasticsearch.ping():
logger.info('Connected to Elasticsearch')
break
else:
logger.warning(f'Attempt {i + 1} to connect to Elasticsearch failed. Retrying...')
time.sleep(2 ** i) # Exponential backoff
else:
logger.error('Can not connect to Elasticsearch')
raise ConnectionError('Can not connect to Elasticsearch')
logger.error('Can not connect to Elasticsearch after 5 attempts')
raise ConnectionError('Can not connect to Elasticsearch after 5 attempts')
return getattr(ctx.elasticsearch, item)
@staticmethod
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment