File size: 3,468 Bytes
e6be672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42d2a35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6be672
 
 
 
42d2a35
 
 
 
 
 
 
e6be672
 
 
 
55f0196
e6be672
 
 
 
 
 
 
 
 
 
 
55f0196
e6be672
 
55f0196
e6be672
55f0196
e6be672
 
 
 
035fe5e
 
 
 
 
 
 
 
e6be672
 
 
 
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
#!/usr/bin/env bash
# entrypoint.sh — boot in-container Postgres, then hand off to the CMD
# (which is supervisord managing uvicorn + celery worker + celery beat).
#
# Runs as uid 1000 on Hugging Face Spaces. Postgres data lives in /tmp
# and is wiped on every container restart — that's intentional, the user
# chose this trade-off in exchange for not depending on an external DB.

set -euo pipefail

PG_BIN=/usr/lib/postgresql/${PG_MAJOR:-15}/bin
PG_LOG=/tmp/postgres.log

echo "[entrypoint] boot — uid=$(id -u) pgdata=$PGDATA"

# --- 1. Initialise Postgres data dir on first boot --------------------------
if [ ! -s "$PGDATA/PG_VERSION" ]; then
    echo "[entrypoint] initdb on $PGDATA"
    "$PG_BIN/initdb" \
        -D "$PGDATA" \
        -U postgres \
        --auth-host=md5 \
        --auth-local=trust \
        --encoding=UTF8 \
        --locale=C.UTF-8 \
        >/dev/null

    # Bind only to localhost — the API and Celery talk to it over the
    # loopback inside the same container.
    cat >> "$PGDATA/postgresql.conf" <<'CONF'
listen_addresses = '127.0.0.1'
port = 5432

# /var/run/postgresql is not writable in HF Spaces; keep the unix socket
# in /tmp where uid 1000 can write.
unix_socket_directories = '/tmp'

# Container memory tuning. HF /dev/shm is typically small (~64MB), so
# keep shared_buffers low. The API processes IFC, not OLAP queries.
shared_buffers = 64MB
work_mem = 4MB
maintenance_work_mem = 16MB
max_connections = 20

# Disable kernel features that containers usually don't grant.
huge_pages = off
dynamic_shared_memory_type = posix
CONF
fi

# --- 2. Start Postgres in the background ------------------------------------
echo "[entrypoint] starting postgres"
if ! "$PG_BIN/pg_ctl" -D "$PGDATA" -l "$PG_LOG" -w -t 30 start; then
    echo "[entrypoint] pg_ctl start FAILED. Dumping $PG_LOG:" >&2
    echo "---------------- postgres.log ----------------" >&2
    cat "$PG_LOG" >&2 || true
    echo "---------------- end log -------------------" >&2
    exit 1
fi

# --- 3. Ensure user / database / extension exist ----------------------------
# Idempotent: safe to run on every boot. On a fresh PGDATA they get created;
# on a warm one nothing happens.
psql -h /tmp -U postgres -d postgres -v ON_ERROR_STOP=1 <<SQL
DO \$\$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '${POSTGRES_USER}') THEN
        CREATE ROLE ${POSTGRES_USER} LOGIN PASSWORD '${POSTGRES_PASSWORD}' SUPERUSER;
    END IF;
END
\$\$;
SQL

# CREATE DATABASE can't run inside a transaction block — issue it separately
# and ignore the "already exists" error.
psql -h /tmp -U postgres -d postgres -tc \
    "SELECT 1 FROM pg_database WHERE datname = '${POSTGRES_DB}'" \
    | grep -q 1 \
    || psql -h /tmp -U postgres -d postgres -c "CREATE DATABASE ${POSTGRES_DB} OWNER ${POSTGRES_USER};"

psql -h /tmp -U postgres -d "${POSTGRES_DB}" -c "CREATE EXTENSION IF NOT EXISTS vector;"

# --- 4. Run alembic migrations ---------------------------------------------
echo "[entrypoint] alembic upgrade head"
cd /app
if ! alembic upgrade head; then
    echo "[entrypoint] FATAL: alembic upgrade failed."
    echo "[entrypoint] Refusing to start uvicorn/celery against an unmigrated database."
    echo "[entrypoint] postgres.log tail:"
    tail -n 50 "$PG_LOG" >&2 || true
    exit 1
fi
echo "[entrypoint] alembic OK"

# --- 5. Hand off to the original CMD (supervisord) -------------------------
echo "[entrypoint] handing off to: $*"
exec "$@"