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
#1030)

* fix: handle elasticsearch connection problems on server startup

* Apply suggestions from code review

Co-authored-by: Daniel Vila Suero <daniel@recogn.ai>

* Update src/rubrix/server/server.py

Co-authored-by: Daniel Vila Suero <daniel@recogn.ai>

* fix message

Co-authored-by: Daniel Vila Suero <daniel@recogn.ai>

(cherry picked from commit dd8aefc)
  • Loading branch information
frascuchon committed Jan 31, 2022
1 parent ba88c34 commit e8c8d86
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/rubrix/server/server.py
Expand Up @@ -20,11 +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 brotli_asgi import BrotliMiddleware

from pydantic import ValidationError
from pydantic import ConfigError, ValidationError

from rubrix import __version__ as rubrix_version
from rubrix.server.commons.errors import (
Expand All @@ -35,7 +35,9 @@
from rubrix.server.commons.static_rewrite import RewriteStaticFiles
from rubrix.server.datasets.dao import DatasetsDAO, create_datasets_dao
from rubrix.server.security import auth
from rubrix.server.tasks.commons.dao.dao import DatasetRecordsDAO, dataset_records_dao
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,11 +88,19 @@ 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()
datasets: DatasetsDAO = create_datasets_dao(es=es_wrapper)
dataset_records: DatasetRecordsDAO = dataset_records_dao(es=es_wrapper)
datasets.init()
dataset_records.init()
try:
es_wrapper = create_es_wrapper()
dataset_records: DatasetRecordsDAO = DatasetRecordsDAO(es_wrapper)
datasets: DatasetsDAO = DatasetsDAO.get_instance(es_wrapper)
datasets.init()
dataset_records.init()
except elasticsearch.exceptions.ConnectionError as error:
raise ConfigError(
f"Your Elasticsearch endpoint at {settings.elasticsearch} is not available or not responding."
"\nPlease make sure your Elasticsearch instance is launched and correctly running and you have the necessary access permissions. "
"Once you have verified this, restart the Rubrix server."
f"\nError detail: [{error}]"
)


def configure_app_security(app: FastAPI):
Expand Down

0 comments on commit e8c8d86

Please sign in to comment.