v1.0.93: separate DB_* env vars (DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD, DB_SSLMODE) + TEST endpoint default
This commit is contained in:
+1
-1
@@ -18,7 +18,7 @@ from routes.api import bp as api_bp
|
|||||||
from routes.api_test import bp as api_test_bp
|
from routes.api_test import bp as api_test_bp
|
||||||
|
|
||||||
# Версия — меняется при КАЖДОМ изменении кода. Показывается в топбаре UI.
|
# Версия — меняется при КАЖДОМ изменении кода. Показывается в топбаре UI.
|
||||||
VERSION = "1.0.92"
|
VERSION = "1.0.93"
|
||||||
|
|
||||||
app = Flask(__name__, template_folder="templates", static_folder="static")
|
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")
|
||||||
|
|||||||
+2
-2
@@ -49,8 +49,8 @@ CREATE INDEX IF NOT EXISTS idx_runs_created
|
|||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
"""Применить схему — идемпотентно, безопасно для конкурентного вызова."""
|
"""Применить схему — идемпотентно, безопасно для конкурентного вызова."""
|
||||||
if not os.getenv("DATABASE_URL"):
|
if not os.getenv("DB_USER"):
|
||||||
print("[DB] DATABASE_URL not set — skipping init", flush=True)
|
print("[DB] DB_USER not set — skipping init", flush=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
conn = get_conn()
|
conn = get_conn()
|
||||||
|
|||||||
+20
-9
@@ -2,10 +2,14 @@
|
|||||||
Connection pool для PostgreSQL (psycopg2).
|
Connection pool для PostgreSQL (psycopg2).
|
||||||
|
|
||||||
Lazy-init: пул создаётся при первом обращении к БД в каждом gunicorn-воркере.
|
Lazy-init: пул создаётся при первом обращении к БД в каждом gunicorn-воркере.
|
||||||
Безопасно для fork-модели — соединения открываются ПОСЛЕ fork.
|
|
||||||
|
|
||||||
Переменная окружения:
|
Переменные окружения (КАЖДАЯ отдельно):
|
||||||
DATABASE_URL — postgresql://user:pass@host:5432/dbname?sslmode=require
|
DB_HOST — хост
|
||||||
|
DB_PORT — порт (5432)
|
||||||
|
DB_NAME — имя БД
|
||||||
|
DB_USER — пользователь
|
||||||
|
DB_PASSWORD — пароль
|
||||||
|
DB_SSLMODE — sslmode (require)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@@ -15,25 +19,32 @@ from psycopg2 import pool
|
|||||||
_pool = None
|
_pool = None
|
||||||
|
|
||||||
|
|
||||||
|
def _dsn():
|
||||||
|
return (
|
||||||
|
f"host={os.getenv('DB_HOST')} "
|
||||||
|
f"port={os.getenv('DB_PORT', '5432')} "
|
||||||
|
f"dbname={os.getenv('DB_NAME')} "
|
||||||
|
f"user={os.getenv('DB_USER')} "
|
||||||
|
f"password={os.getenv('DB_PASSWORD')} "
|
||||||
|
f"sslmode={os.getenv('DB_SSLMODE', 'require')}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_pool():
|
def get_pool():
|
||||||
"""Lazy-init ThreadedConnectionPool (1-5 соединений)."""
|
|
||||||
global _pool
|
global _pool
|
||||||
if _pool is None:
|
if _pool is None:
|
||||||
dsn = os.getenv("DATABASE_URL", "")
|
if not os.getenv("DB_USER"):
|
||||||
if not dsn:
|
|
||||||
return None
|
return None
|
||||||
_pool = pool.ThreadedConnectionPool(1, 5, dsn)
|
_pool = pool.ThreadedConnectionPool(1, 5, _dsn())
|
||||||
return _pool
|
return _pool
|
||||||
|
|
||||||
|
|
||||||
def get_conn():
|
def get_conn():
|
||||||
"""Взять соединение из пула."""
|
|
||||||
p = get_pool()
|
p = get_pool()
|
||||||
return p.getconn() if p else None
|
return p.getconn() if p else None
|
||||||
|
|
||||||
|
|
||||||
def put_conn(conn):
|
def put_conn(conn):
|
||||||
"""Вернуть соединение в пул."""
|
|
||||||
p = get_pool()
|
p = get_pool()
|
||||||
if p and conn:
|
if p and conn:
|
||||||
p.putconn(conn)
|
p.putconn(conn)
|
||||||
|
|||||||
Reference in New Issue
Block a user