release: bump version to 0.74.7
This commit is contained in:
1 parent
83a10a6c34
commit
60eefb19c9
25 files changed
+1079
-116
No files matched your search
@@ -5,6 +5,8 @@ import asyncio
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
@@ -16,6 +18,7 @@ from sqlalchemy.pool import NullPool
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
CONNECT_TIMEOUT_SECONDS = 10
|
||||
DEFAULT_POSTGRES_PORT = 5432
|
||||
DEFAULT_REDIS_PORT = 6379
|
||||
POSTGRES_CONTAINER = "planet_postgres"
|
||||
POSTGRES_CONTAINER_PORT = "5432/tcp"
|
||||
LOOPBACK_HOSTS = {"localhost", "127.0.0.1", "::1"}
|
||||
@@ -36,6 +39,55 @@ def backend_database_url() -> str:
|
||||
return settings.DATABASE_URL
|
||||
|
||||
|
||||
def configured_ports() -> tuple[int, int]:
|
||||
database = make_url(backend_database_url())
|
||||
from app.core.config import settings
|
||||
|
||||
redis = make_url(settings.REDIS_URL)
|
||||
postgres_port = database.port or DEFAULT_POSTGRES_PORT
|
||||
redis_port = redis.port or DEFAULT_REDIS_PORT
|
||||
targets = []
|
||||
if database.host in LOOPBACK_HOSTS:
|
||||
targets.append(("PostgreSQL", POSTGRES_CONTAINER, postgres_port))
|
||||
if redis.host in LOOPBACK_HOSTS:
|
||||
targets.append(("Redis", "planet_redis", redis_port))
|
||||
check_port_conflicts(targets)
|
||||
return postgres_port, redis_port
|
||||
|
||||
|
||||
def check_port_conflicts(targets: list[tuple[str, str, int]]) -> None:
|
||||
result = subprocess.run(
|
||||
["docker", "ps", "--format", "{{.Names}}\t{{.Ports}}"],
|
||||
capture_output=True, text=True, timeout=CONNECT_TIMEOUT_SECONDS, check=False,
|
||||
)
|
||||
if result.returncode:
|
||||
raise DatabaseReadinessError("无法读取 Docker 端口占用;请检查 daemon 和当前用户权限。")
|
||||
for service, expected_container, port in targets:
|
||||
own_binding = False
|
||||
for line in result.stdout.splitlines():
|
||||
name, _, bindings = line.partition("\t")
|
||||
if not re.search(rf":{port}->", bindings):
|
||||
continue
|
||||
if name == expected_container:
|
||||
own_binding = True
|
||||
else:
|
||||
raise DatabaseReadinessError(
|
||||
f"PLANET_DB_PORT_OCCUPIED: {service} 端口 {port} 被 {name} 占用;"
|
||||
"请调整 Planet 后端连接端口。"
|
||||
)
|
||||
if not own_binding:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener:
|
||||
try:
|
||||
# Docker can reuse recently closed TCP connections; TIME_WAIT is not a listener.
|
||||
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
listener.bind(("127.0.0.1", port))
|
||||
except OSError as error:
|
||||
raise DatabaseReadinessError(
|
||||
f"PLANET_DB_PORT_OCCUPIED: {service} 端口 {port} 被宿主机服务占用;"
|
||||
"请检查监听进程或调整 Planet 后端连接端口。"
|
||||
) from error
|
||||
|
||||
|
||||
def check_published_port(url: URL) -> None:
|
||||
if url.host not in LOOPBACK_HOSTS:
|
||||
return # An explicitly configured external database has no local container mapping.
|
||||
@@ -106,6 +158,9 @@ def connection_diagnostic(error: BaseException) -> str:
|
||||
|
||||
def main() -> int:
|
||||
try:
|
||||
if sys.argv[1:] == ["--ports"]:
|
||||
print(*configured_ports())
|
||||
return 0
|
||||
url = make_url(backend_database_url())
|
||||
print(
|
||||
f"后端数据库目标: host={url.host!r} port={url.port or DEFAULT_POSTGRES_PORT} database={url.database!r}"
|
||||
|
||||
Reference in new issue
Block a user