Make legacy payment delivery retryable and keep gateway paths responsive
This commit is contained in:
@@ -8,7 +8,70 @@ _log = logging.getLogger("gwerr")
|
||||
import os as _os
|
||||
import sqlite3 as _sq
|
||||
|
||||
_NEWAPI_DB = _os.environ.get("GW_NEWAPI_DB", "/opt/grok-gateway/new-api/one-api.db")
|
||||
|
||||
def _load_runtime_config():
|
||||
"""runtime_config is the single source of truth for the effective paths.
|
||||
|
||||
Importing it directly works for both real entrypoints; the file-based fallback keeps
|
||||
gateway-only importers (and tests) from silently reverting to the legacy literal.
|
||||
"""
|
||||
try:
|
||||
import runtime_config
|
||||
return runtime_config
|
||||
except ImportError:
|
||||
pass
|
||||
candidate = _os.path.join(_os.path.dirname(_os.path.dirname(_os.path.abspath(__file__))),
|
||||
"bot", "runtime_config.py")
|
||||
try:
|
||||
import importlib.util as _iu
|
||||
spec = _iu.spec_from_file_location("iishko_gateway_runtime_config", candidate)
|
||||
if spec is None or spec.loader is None:
|
||||
return None
|
||||
module = _iu.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
_runtime_config = _load_runtime_config()
|
||||
_gateway_db_path = getattr(_runtime_config, "gateway_db_path", None)
|
||||
_bot_config_path = getattr(_runtime_config, "bot_config_path", None)
|
||||
_LEGACY_NEWAPI_DB = "/opt/grok-gateway/new-api/one-api.db"
|
||||
|
||||
|
||||
def _config_newapi_db():
|
||||
"""`newapi_db` from the bot config file, or "" when it names none.
|
||||
|
||||
The skin resolves its gateway DB through effective_bot_config(), so a deployment
|
||||
that sets the path only in bot/config.json must not leave 401 diagnostics reading
|
||||
the legacy literal. An unreadable or malformed file means "no opinion": env aliases
|
||||
and the legacy default still apply.
|
||||
"""
|
||||
if _bot_config_path is None:
|
||||
return ""
|
||||
try:
|
||||
with open(_bot_config_path(), encoding="utf-8-sig") as fh:
|
||||
cfg = json.load(fh)
|
||||
except Exception:
|
||||
return ""
|
||||
value = cfg.get("newapi_db") if isinstance(cfg, dict) else None
|
||||
value = str(value).strip() if value else ""
|
||||
return value
|
||||
|
||||
|
||||
def _newapi_db():
|
||||
"""Effective gateway DB, resolved late on every call.
|
||||
|
||||
Order: IISHKO_GW_DB > GATE_NEWAPI_DB (the shared resolver used by the skin, bot and
|
||||
launcher) > GW_NEWAPI_DB (legacy units) > bot/config.json `newapi_db` (the value the
|
||||
skin itself serves) > legacy literal. Previously this read a hardcoded legacy path
|
||||
even when the process served a different effective database, so 401 diagnostics
|
||||
classified keys against the wrong token table.
|
||||
"""
|
||||
legacy = _os.environ.get("GW_NEWAPI_DB") or _config_newapi_db() or _LEGACY_NEWAPI_DB
|
||||
return _gateway_db_path(legacy) if _gateway_db_path else legacy
|
||||
|
||||
|
||||
# [POLICYMSG 2810] Вердикт модерации апстрима детерминирован: тот же текст на том же
|
||||
# OpenAI «content_filter»). Прежняя формулировка «Повтори попытку или выбери другую
|
||||
@@ -42,7 +105,7 @@ def token_state(api_key):
|
||||
if not api_key:
|
||||
return None
|
||||
try:
|
||||
con = _sq.connect("file:" + _NEWAPI_DB + "?mode=ro", uri=True, timeout=3)
|
||||
con = _sq.connect("file:" + _newapi_db() + "?mode=ro", uri=True, timeout=3)
|
||||
try:
|
||||
row = con.execute(
|
||||
'SELECT status, COALESCE(remain_quota,0), COALESCE(unlimited_quota,0) '
|
||||
|
||||
Reference in New Issue
Block a user