🐛 The backend shall serve requests once the database reachable

This commit is contained in:
2023-05-26 18:02:54 +02:00
parent c44a52b7ae
commit cd700ebd42
3 changed files with 47 additions and 7 deletions

View File

@@ -1,10 +1,11 @@
from asyncio import sleep
from logging import getLogger from logging import getLogger
from typing import Annotated, AsyncIterator from typing import Annotated, AsyncIterator
from fastapi import Depends from fastapi import Depends
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from sqlalchemy import text from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import OperationalError, SQLAlchemyError
from sqlalchemy.ext.asyncio import ( from sqlalchemy.ext.asyncio import (
async_sessionmaker, async_sessionmaker,
AsyncEngine, AsyncEngine,
@@ -56,11 +57,20 @@ class Database:
class_=AsyncSession, class_=AsyncSession,
) )
ret = False
while not ret:
try:
async with self._async_engine.begin() as session: async with self._async_engine.begin() as session:
await session.execute(text("CREATE EXTENSION IF NOT EXISTS pg_trgm;")) await session.execute(
text("CREATE EXTENSION IF NOT EXISTS pg_trgm;")
)
if clear_static_data: if clear_static_data:
await session.run_sync(Base.metadata.drop_all) await session.run_sync(Base.metadata.drop_all)
await session.run_sync(Base.metadata.create_all) await session.run_sync(Base.metadata.create_all)
ret = True
except OperationalError as err:
logger.error(err)
await sleep(1)
return True return True

View File

@@ -1,8 +1,9 @@
app_name: carrramba-encore-rate app_name: carrramba-encore-rate
clear_static_data: false
http: http:
host: 0.0.0.0 host: 0.0.0.0
port: 4443 port: 8080
cert: ./config/cert.pem cert: ./config/cert.pem
db: db:
@@ -12,3 +13,9 @@ db:
driver: postgresql+psycopg driver: postgresql+psycopg
user: cer user: cer
password: cer_password password: cer_password
cache:
enable: true
tracing:
enable: false

View File

@@ -0,0 +1,23 @@
app_name: carrramba-encore-rate
clear_static_data: false
http:
host: 0.0.0.0
port: 8080
# cert: ./config/cert.pem
db:
name: carrramba-encore-rate
host: postgres
port: 5432
driver: postgresql+psycopg
user: cer
password: cer_password
cache:
enable: true
host: redis
# TODO: Add user credentials
tracing:
enable: false