File size: 3,320 Bytes
9f97180
c6d4e5d
9f97180
24c2656
db9f671
 
 
 
 
9f97180
 
24c2656
db9f671
 
 
4b75d04
db9f671
9f97180
24c2656
c6d4e5d
 
 
 
 
 
 
9f97180
c6d4e5d
 
 
 
 
24c2656
c6d4e5d
 
 
 
 
 
 
 
 
 
 
 
24c2656
c6d4e5d
db9f671
9f97180
db9f671
403c243
 
4b75d04
db9f671
c6d4e5d
9f97180
 
24c2656
 
 
 
 
 
 
 
403c243
24c2656
 
 
 
 
 
 
 
 
 
 
 
 
403c243
 
24c2656
 
 
 
 
 
 
403c243
24c2656
4b75d04
9f97180
4b75d04
9f97180
c6d4e5d
 
24c2656
c6d4e5d
 
 
 
 
 
 
 
 
24c2656
c6d4e5d
24c2656
c6d4e5d
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
#!/bin/sh
set -e

# Start Nginx.
echo "Starting nginx..."
/usr/sbin/nginx -c /etc/nginx/nginx.conf
if [ $? -ne 0 ]; then
    echo "Failed to start nginx"
    exit 1
fi

# Verify nginx is running.
if ! ps aux | grep nginx | grep -v grep > /dev/null; then
    echo "Nginx failed to start"
    exit 1
fi
echo "Nginx started successfully"

# Start PostgreSQL.
echo "Starting PostgreSQL..."
if [ ! -f "$PGDATA/PG_VERSION" ]; then
    mkdir -p $PGDATA
    echo "Current user: $(whoami)"
    initdb -D $PGDATA --username=postgres --pwfile=<(echo "password")
fi
pg_ctl -D $PGDATA start

# Create the db-user (but let Prisma handle the database)
echo "Creating database user..."
PGPASSWORD=password PGUSER=postgres createuser -s "db-user" || echo "User db-user already exists"
PGPASSWORD=password PGUSER=postgres psql -c 'ALTER USER "db-user" WITH PASSWORD '\''password'\'';' postgres

# Wait for PostgreSQL (using db-user).
echo "Waiting for PostgreSQL..."
until pg_isready -U db-user; do
    echo "Waiting for PostgreSQL to be ready..."
    sleep 2
    if [ $SECONDS -gt 30 ]; then
        echo "Timeout waiting for PostgreSQL"
        exit 1
    fi
done

echo "PostgreSQL is ready!"

# Start Llamafiler.
echo "Starting llamafiler services..."
mkdir -p /tmp/llamafiler

echo "Starting chat model..."
TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model $HOME/models/gemma-2b.gguf --listen 0.0.0.0:8082 &
GEMMA_PID=$!

echo "Starting embedding model..."
TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model $HOME/models/embeddings.gguf --listen 0.0.0.0:8081 &
EMBEDDINGS_PID=$!

# Wait for the models to be ready with a timeout
echo "Waiting for models to be ready..."
TIMEOUT=300  # 5 minutes timeout
START_TIME=$SECONDS

wait_for_services() {
    curl -s --fail -X POST http://127.0.0.1:7861/v1/chat/completions \
        -H "Content-Type: application/json" \
        -d '{"model": "gemma-2b", "messages":[{"role":"user","content":"hi"}]}' >/dev/null 2>&1 && \
    curl -s --fail -X POST http://127.0.0.1:7861/v1/embeddings \
        -H "Content-Type: application/json" \
        -d '{"input":"test"}' >/dev/null 2>&1
}

until wait_for_services; do
    ELAPSED=$((SECONDS - START_TIME))
    if [ $ELAPSED -gt $TIMEOUT ]; then
        echo "Timeout waiting for services after ${TIMEOUT} seconds"
        exit 1
    fi
    
    # Check if processes are still running
    if ! kill -0 $GEMMA_PID 2>/dev/null; then
        echo "Gemma model process died."    
        exit 1
    fi
    if ! kill -0 $EMBEDDINGS_PID 2>/dev/null; then
        echo "Embeddings model process died."
        exit 1
    fi
    
    echo "Waiting for services... (${ELAPSED}s elapsed, PIDs: Gemma=$GEMMA_PID, Embeddings=$EMBEDDINGS_PID)"
    sleep 2
done

echo 'Servers are ready!'

echo 'All services are ready!'

# Ensure we have the same environment as the base image.
export PNPM_HOME=/root/.local/share/pnpm
export PATH=$PNPM_HOME/bin:$PATH
cd /base  # Ensure we're in the correct directory

ls -la prisma/
ls -la $PNPM_HOME
ls -la $PNPM_HOME/global/5/node_modules/@prisma || echo "No global prisma found"
prisma -v

# Now execute the entrypoint with the proper environment.
echo "Executing: /base/bin/entrypoint.sh node /base/app/main.bundle.js"
set -x  # Enable command tracing.
exec /base/bin/entrypoint.sh node /base/app/main.bundle.js