v1.0.94: lazy schema init — pool.py runs init_db on first DB access, not at import
This commit is contained in:
@@ -24,11 +24,6 @@ app = Flask(__name__, template_folder="templates", static_folder="static")
|
|||||||
app.config["NUBES_API_ENDPOINT"] = os.getenv("NUBES_API_ENDPOINT", "https://lk-api-gateway-test.ngcloud.ru/api/v1/svc")
|
app.config["NUBES_API_ENDPOINT"] = os.getenv("NUBES_API_ENDPOINT", "https://lk-api-gateway-test.ngcloud.ru/api/v1/svc")
|
||||||
app.config["NUBES_API_TOKEN"] = os.getenv("NUBES_API_TOKEN", "")
|
app.config["NUBES_API_TOKEN"] = os.getenv("NUBES_API_TOKEN", "")
|
||||||
app.config["VERSION"] = VERSION
|
app.config["VERSION"] = VERSION
|
||||||
|
|
||||||
# Инициализация схемы БД (идемпотентно, безопасно для нескольких воркеров)
|
|
||||||
from db.init_db import init_db
|
|
||||||
init_db()
|
|
||||||
|
|
||||||
app.register_blueprint(main_bp)
|
app.register_blueprint(main_bp)
|
||||||
app.register_blueprint(api_bp)
|
app.register_blueprint(api_bp)
|
||||||
app.register_blueprint(api_test_bp)
|
app.register_blueprint(api_test_bp)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import psycopg2
|
|||||||
from psycopg2 import pool
|
from psycopg2 import pool
|
||||||
|
|
||||||
_pool = None
|
_pool = None
|
||||||
|
_initialized = False
|
||||||
|
|
||||||
|
|
||||||
def _dsn():
|
def _dsn():
|
||||||
@@ -30,12 +31,26 @@ def _dsn():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _ensure_schema():
|
||||||
|
"""Инициализировать схему при первом обращении к БД."""
|
||||||
|
global _initialized
|
||||||
|
if _initialized:
|
||||||
|
return
|
||||||
|
_initialized = True
|
||||||
|
try:
|
||||||
|
from db.init_db import init_db
|
||||||
|
init_db()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_pool():
|
def get_pool():
|
||||||
global _pool
|
global _pool
|
||||||
if _pool is None:
|
if _pool is None:
|
||||||
if not os.getenv("DB_USER"):
|
if not os.getenv("DB_USER"):
|
||||||
return None
|
return None
|
||||||
_pool = pool.ThreadedConnectionPool(1, 5, _dsn())
|
_pool = pool.ThreadedConnectionPool(1, 5, _dsn())
|
||||||
|
_ensure_schema()
|
||||||
return _pool
|
return _pool
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user