Skip to content

Commit

Permalink
fix(#1023): handle elasticsearch connection problems on server startup
Browse files Browse the repository at this point in the history
  • Loading branch information
frascuchon committed Jan 25, 2022
1 parent 7667ae8 commit 41a7519
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/rubrix/server/server.py
Expand Up @@ -20,10 +20,11 @@
import os
from pathlib import Path

import elasticsearch
from brotli_asgi import BrotliMiddleware
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import ValidationError
from pydantic import ConfigError, ValidationError

from rubrix import __version__ as rubrix_version
from rubrix.server.commons.errors import (
Expand All @@ -36,6 +37,7 @@
from rubrix.server.security import auth
from rubrix.server.tasks.commons.dao.dao import DatasetRecordsDAO

from .commons.settings import settings
from .commons.settings import settings as api_settings
from .routes import api_router

Expand Down Expand Up @@ -86,12 +88,21 @@ def configure_app_statics(app: FastAPI):
def configure_app_startup(app: FastAPI):
@app.on_event("startup")
async def configure_elasticsearch():
es_wrapper = create_es_wrapper()
dataset_records: DatasetRecordsDAO = DatasetRecordsDAO(es_wrapper)
datasets: DatasetsDAO = DatasetsDAO.get_instance(es_wrapper, dataset_records)

datasets.init()
dataset_records.init()
try:
es_wrapper = create_es_wrapper()
dataset_records: DatasetRecordsDAO = DatasetRecordsDAO(es_wrapper)
datasets: DatasetsDAO = DatasetsDAO.get_instance(
es_wrapper, dataset_records
)
datasets.init()
dataset_records.init()
except elasticsearch.exceptions.ConnectionError as error:
raise ConfigError(
f"We checked the elastic search endpoint at {settings.elasticsearch} and is not responding/available..."
"\nPlease make sure your Elasticsearch is running or you have access to elasticsearch and "
"restart rubrix."
f"\nError detail: [{error}]"
)


def configure_app_security(app: FastAPI):
Expand Down

0 comments on commit 41a7519

Please sign in to comment.