Spaces:
Running
Running
File size: 7,037 Bytes
8f1b6ce e8aac63 8f1b6ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | """Supervise rootless local services for Spaces or use externally managed databases.
Signals and any unexpected child exit shut down the whole unit. No service credentials are logged.
"""
import os
import signal
import subprocess
import sys
import time
from pathlib import Path
children: list[subprocess.Popen] = []
pg_started = False
stopping = False
root = Path(os.environ.get("DATA_DIR", "/data"))
pg_bin = Path(os.environ.get("PG_BIN", "/usr/lib/postgresql/15/bin"))
pg_data = root / "postgres"
def stop(signum: int = 0, frame: object = None) -> None:
global stopping
if stopping:
return
stopping = True
for child in reversed(children):
if child.poll() is None:
child.terminate()
for child in children:
try:
child.wait(timeout=15)
except subprocess.TimeoutExpired:
child.kill()
if pg_started:
subprocess.run(
[str(pg_bin / "pg_ctl"), "-D", str(pg_data), "-m", "fast", "-w", "stop"],
check=False,
)
signal.signal(signal.SIGTERM, stop)
signal.signal(signal.SIGINT, stop)
try:
if not os.environ.get("ENCRYPTION_KEYS"):
raise RuntimeError("Set ENCRYPTION_KEYS before starting the gateway.")
root.mkdir(parents=True, exist_ok=True)
if not os.environ.get("DATABASE_URL"):
if (
os.environ.get("ENVIRONMENT") == "production"
and os.environ.get("PERSISTENT_STORAGE_CONFIRMED") != "true"
):
raise RuntimeError(
"Embedded production PostgreSQL requires durable /data storage and PERSISTENT_STORAGE_CONFIRMED=true."
)
socket_dir = root / "run"
socket_dir.mkdir(exist_ok=True)
if not (pg_data / "PG_VERSION").exists():
subprocess.run(
[
str(pg_bin / "initdb"),
"-D",
str(pg_data),
"-U",
"gateway",
"--auth-local=trust",
"--auth-host=reject",
"--encoding=UTF8",
],
check=True,
)
subprocess.run(
[
str(pg_bin / "pg_ctl"),
"-D",
str(pg_data),
"-l",
str(root / "postgres.log"),
"-o",
f"-k {socket_dir} -c listen_addresses=''",
"-w",
"start",
],
check=True,
)
pg_started = True
exists = subprocess.run(
[
str(pg_bin / "psql"),
"-h",
str(socket_dir),
"-U",
"gateway",
"-d",
"postgres",
"-tAc",
"SELECT 1 FROM pg_database WHERE datname='gateway'",
],
capture_output=True,
text=True,
check=True,
)
if exists.stdout.strip() != "1":
subprocess.run(
[
str(pg_bin / "createdb"),
"-h",
str(socket_dir),
"-U",
"gateway",
"gateway",
],
check=True,
)
os.environ["DATABASE_URL"] = (
f"postgresql+asyncpg://gateway@/gateway?host={socket_dir}"
)
if not os.environ.get("REDIS_URL"):
redis_dir = root / "redis"
redis_dir.mkdir(exist_ok=True)
redis_socket = root / "redis.sock"
children.append(
subprocess.Popen(
[
"redis-server",
"--port",
"0",
"--unixsocket",
str(redis_socket),
"--unixsocketperm",
"700",
"--dir",
str(redis_dir),
"--appendonly",
"yes",
"--loglevel",
"warning",
]
)
)
os.environ["REDIS_URL"] = f"unix://{redis_socket}?db=0"
import redis
connection = redis.Redis.from_url(os.environ["REDIS_URL"])
for i in range(100):
try:
connection.ping()
break
except redis.RedisError:
time.sleep(0.1)
else:
raise RuntimeError("Embedded Redis failed to start.")
connection.close()
# Alembic serializes schema changes with a PostgreSQL advisory lock in env.py.
subprocess.run(
[
sys.executable,
"-m",
"alembic",
"-c",
"backend/alembic.ini",
"upgrade",
"head",
],
check=True,
)
# Server-only provisioning runs before the public application accepts requests.
# A password-hash seed can recreate the owner after an explicitly accepted data reset.
if os.environ.get("ADMIN_BOOTSTRAP_EMAIL"):
subprocess.run(
[
sys.executable,
"scripts/admin.py",
"bootstrap",
"--email",
os.environ["ADMIN_BOOTSTRAP_EMAIL"],
],
check=True,
)
# The web worker does not need access to the operator's provisioning secrets.
os.environ.pop("ADMIN_BOOTSTRAP_PASSWORD_HASH", None)
os.environ.pop("ADMIN_BOOTSTRAP_EMAIL", None)
api = subprocess.Popen(
[
sys.executable,
"-m",
"uvicorn",
"app.main:app",
"--host",
"0.0.0.0",
"--port",
os.environ.get("PORT", "7860"),
"--workers",
os.environ.get("WEB_CONCURRENCY", "1"),
"--limit-concurrency",
os.environ.get("MAX_CONCURRENCY", "32"),
"--timeout-keep-alive",
"5",
"--no-access-log",
"--forwarded-allow-ips",
os.environ.get("TRUSTED_PROXY_IPS", ""),
]
)
children.append(api)
while not stopping:
if any(child.poll() is not None for child in children):
raise RuntimeError("A supervised service exited unexpectedly.")
if pg_started:
result = subprocess.run(
[str(pg_bin / "pg_ctl"), "-D", str(pg_data), "status"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if result.returncode:
raise RuntimeError("Embedded PostgreSQL exited unexpectedly.")
time.sleep(1)
except Exception as exc:
# Do not include command output or database connection URLs in errors.
print(
f"Gateway startup failed ({type(exc).__name__}). Check configuration and service availability.",
file=sys.stderr,
)
stop()
sys.exit(1)
finally:
stop()
|