dmartincy commited on
Commit
24c2656
·
1 Parent(s): dbe735d

Improve service handling

Browse files
Files changed (3) hide show
  1. document-authoring.js +1 -1
  2. index.html +62 -0
  3. start-services.sh +41 -65
document-authoring.js CHANGED
@@ -2,7 +2,7 @@
2
  const app = document.getElementById('app');
3
  app.innerHTML = `
4
  <div style="max-width: 1024px; margin: 0 auto; padding: 20px;">
5
- <button id="translateButton" style="margin-bottom: 10px;">Translate to Spanish</button>
6
  <div id="editor" style="border: 1px solid black; height: 600px; position: relative"></div>
7
  </div>
8
  `;
 
2
  const app = document.getElementById('app');
3
  app.innerHTML = `
4
  <div style="max-width: 1024px; margin: 0 auto; padding: 20px;">
5
+ <button id="translateButton" style="margin-bottom: 10px;" disabled class="button-disabled">Translate to Spanish</button>
6
  <div id="editor" style="border: 1px solid black; height: 600px; position: relative"></div>
7
  </div>
8
  `;
index.html CHANGED
@@ -27,10 +27,72 @@
27
  cursor: not-allowed;
28
  opacity: 0.7;
29
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  </style>
31
  </head>
32
  <body>
33
  <div id="app"></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  <script src="/document-authoring.js"></script>
35
  </body>
36
  </html>
 
27
  cursor: not-allowed;
28
  opacity: 0.7;
29
  }
30
+
31
+ /* Add styles for disabled state */
32
+ .button-disabled {
33
+ cursor: not-allowed;
34
+ opacity: 0.5;
35
+ pointer-events: none;
36
+ }
37
+
38
+ /* Add a status indicator */
39
+ #statusIndicator {
40
+ position: fixed;
41
+ bottom: 10px;
42
+ right: 10px;
43
+ padding: 8px;
44
+ border-radius: 4px;
45
+ background: #f0f0f0;
46
+ font-size: 14px;
47
+ }
48
  </style>
49
  </head>
50
  <body>
51
  <div id="app"></div>
52
+ <div id="statusIndicator">Services starting...</div>
53
+ <script>
54
+ // Add this before loading the main script
55
+ window.servicesReady = false;
56
+
57
+ // Create a function to check service status
58
+ function checkServicesStatus() {
59
+ fetch('/client/api/v1/healthcheck')
60
+ .then(response => {
61
+ if (response.ok) {
62
+ window.servicesReady = true;
63
+ const translateButton = document.getElementById('translateButton');
64
+ const statusIndicator = document.getElementById('statusIndicator');
65
+ if (translateButton) {
66
+ translateButton.classList.remove('button-disabled');
67
+ translateButton.disabled = false;
68
+ }
69
+ if (statusIndicator) {
70
+ statusIndicator.style.background = '#e6ffe6';
71
+ statusIndicator.textContent = 'Services ready';
72
+ // Optionally hide after a delay:
73
+ setTimeout(() => {
74
+ statusIndicator.style.display = 'none';
75
+ }, 3000);
76
+ }
77
+ return true;
78
+ }
79
+ throw new Error('Services not ready');
80
+ })
81
+ .catch(error => {
82
+ console.log('Waiting for services...', error);
83
+ return false;
84
+ });
85
+ }
86
+
87
+ // Check status every 2 seconds until ready
88
+ const statusInterval = setInterval(() => {
89
+ if (window.servicesReady) {
90
+ clearInterval(statusInterval);
91
+ } else {
92
+ checkServicesStatus();
93
+ }
94
+ }, 2000);
95
+ </script>
96
  <script src="/document-authoring.js"></script>
97
  </body>
98
  </html>
start-services.sh CHANGED
@@ -1,10 +1,7 @@
1
  #!/bin/sh
2
  set -e
3
 
4
- # 1. Start Flask (already started in the CMD)
5
- echo "Flask is running..."
6
-
7
- # 2. Start Nginx
8
  echo "Starting nginx..."
9
  /usr/sbin/nginx -c /etc/nginx/nginx.conf
10
  if [ $? -ne 0 ]; then
@@ -12,14 +9,14 @@ if [ $? -ne 0 ]; then
12
  exit 1
13
  fi
14
 
15
- # Verify nginx is running
16
  if ! ps aux | grep nginx | grep -v grep > /dev/null; then
17
  echo "Nginx failed to start"
18
  exit 1
19
  fi
20
  echo "Nginx started successfully"
21
 
22
- # 3. Start PostgreSQL
23
  echo "Starting PostgreSQL..."
24
  if [ ! -f "$PGDATA/PG_VERSION" ]; then
25
  mkdir -p $PGDATA
@@ -33,7 +30,7 @@ echo "Creating database user..."
33
  PGPASSWORD=password PGUSER=postgres createuser -s "db-user" || echo "User db-user already exists"
34
  PGPASSWORD=password PGUSER=postgres psql -c 'ALTER USER "db-user" WITH PASSWORD '\''password'\'';' postgres
35
 
36
- # Wait for PostgreSQL (using db-user)
37
  echo "Waiting for PostgreSQL..."
38
  until pg_isready -U db-user; do
39
  echo "Waiting for PostgreSQL to be ready..."
@@ -46,28 +43,7 @@ done
46
 
47
  echo "PostgreSQL is ready!"
48
 
49
- # # Start the web app
50
- # echo "Starting web app..."
51
- # cd /docauth
52
- # export NODE_OPTIONS="--experimental-vm-modules --experimental-json-modules"
53
-
54
- # # Start vite with the existing config.
55
- # npx vite serve --host 0.0.0.0 --port 7860 &
56
-
57
- # # Wait for the web app to start
58
- # echo "Waiting for web app..."
59
- # until curl -s http://127.0.0.1:7860 > /dev/null; do
60
- # echo "Waiting for web app..."
61
- # sleep 2
62
- # if [ $SECONDS -gt 30 ]; then
63
- # echo "Timeout waiting for web app"
64
- # exit 1
65
- # fi
66
- # done
67
-
68
- # echo "Web app is ready!"
69
-
70
- # 4. Start Llamafiler (reusing existing code)
71
  echo "Starting llamafiler services..."
72
  mkdir -p /tmp/llamafiler
73
 
@@ -79,56 +55,56 @@ echo "Starting embedding model..."
79
  TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model $HOME/models/embeddings.gguf --listen 0.0.0.0:8081 &
80
  EMBEDDINGS_PID=$!
81
 
82
- # Wait for the models to start loading
83
- echo "Waiting for initial model loading..."
84
- sleep 15 # Give more time for the models to load into memory
85
-
86
- echo 'Waiting for servers to start...'
87
- until curl -s --fail -X POST http://127.0.0.1:7861/v1/chat/completions \
88
- -H "Content-Type: application/json" \
89
- -d '{"model": "gemma-2b", "messages":[{"role":"user","content":"hi"}]}' >/dev/null 2>&1 && \
90
- curl -s --fail -X POST http://127.0.0.1:7861/v1/embeddings \
91
- -H "Content-Type: application/json" \
92
- -d '{"input":"test"}' >/dev/null 2>&1
93
- do
94
- # Check if processes are still running
95
- if ! kill -0 $GEMMA_PID 2>/dev/null; then
96
- echo "Gemma model process died."
97
- exit 1
98
- fi
99
- if ! kill -0 $EMBEDDINGS_PID 2>/dev/null; then
100
- echo "Embeddings model process died."
101
- exit 1
102
- fi
103
-
104
- sleep 2
105
- echo "Waiting for services... (PIDs: Gemma=$GEMMA_PID, Embeddings=$EMBEDDINGS_PID)"
 
 
 
 
 
 
 
 
 
106
  done
107
 
108
  echo 'Servers are ready!'
109
 
110
  echo 'All services are ready!'
111
 
112
- # Ensure we have the same environment as the base image
113
  export PNPM_HOME=/root/.local/share/pnpm
114
  export PATH=$PNPM_HOME/bin:$PATH
115
  cd /base # Ensure we're in the correct directory
116
 
117
- echo "=== After Environment Setup ==="
118
- echo "New working directory: $(pwd)"
119
- echo "Updated PATH: $PATH"
120
- echo "Contents of prisma directory:"
121
  ls -la prisma/
122
- echo "NODE_PATH environment: $NODE_PATH"
123
- echo "PNPM_HOME contents:"
124
  ls -la $PNPM_HOME
125
- echo "Global node_modules:"
126
  ls -la $PNPM_HOME/global/5/node_modules/@prisma || echo "No global prisma found"
127
- echo "Local prisma version:"
128
  prisma -v
129
- echo "======================="
130
 
131
- # Now execute the entrypoint with the proper environment
132
  echo "Executing: /base/bin/entrypoint.sh node /base/app/main.bundle.js"
133
- set -x # Enable command tracing
134
  exec /base/bin/entrypoint.sh node /base/app/main.bundle.js
 
1
  #!/bin/sh
2
  set -e
3
 
4
+ # Start Nginx.
 
 
 
5
  echo "Starting nginx..."
6
  /usr/sbin/nginx -c /etc/nginx/nginx.conf
7
  if [ $? -ne 0 ]; then
 
9
  exit 1
10
  fi
11
 
12
+ # Verify nginx is running.
13
  if ! ps aux | grep nginx | grep -v grep > /dev/null; then
14
  echo "Nginx failed to start"
15
  exit 1
16
  fi
17
  echo "Nginx started successfully"
18
 
19
+ # Start PostgreSQL.
20
  echo "Starting PostgreSQL..."
21
  if [ ! -f "$PGDATA/PG_VERSION" ]; then
22
  mkdir -p $PGDATA
 
30
  PGPASSWORD=password PGUSER=postgres createuser -s "db-user" || echo "User db-user already exists"
31
  PGPASSWORD=password PGUSER=postgres psql -c 'ALTER USER "db-user" WITH PASSWORD '\''password'\'';' postgres
32
 
33
+ # Wait for PostgreSQL (using db-user).
34
  echo "Waiting for PostgreSQL..."
35
  until pg_isready -U db-user; do
36
  echo "Waiting for PostgreSQL to be ready..."
 
43
 
44
  echo "PostgreSQL is ready!"
45
 
46
+ # Start Llamafiler.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  echo "Starting llamafiler services..."
48
  mkdir -p /tmp/llamafiler
49
 
 
55
  TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model $HOME/models/embeddings.gguf --listen 0.0.0.0:8081 &
56
  EMBEDDINGS_PID=$!
57
 
58
+ # Wait for the models to be ready with a timeout
59
+ echo "Waiting for models to be ready..."
60
+ TIMEOUT=300 # 5 minutes timeout
61
+ START_TIME=$SECONDS
62
+
63
+ wait_for_services() {
64
+ curl -s --fail -X POST http://127.0.0.1:7861/v1/chat/completions \
65
+ -H "Content-Type: application/json" \
66
+ -d '{"model": "gemma-2b", "messages":[{"role":"user","content":"hi"}]}' >/dev/null 2>&1 && \
67
+ curl -s --fail -X POST http://127.0.0.1:7861/v1/embeddings \
68
+ -H "Content-Type: application/json" \
69
+ -d '{"input":"test"}' >/dev/null 2>&1
70
+ }
71
+
72
+ until wait_for_services; do
73
+ ELAPSED=$((SECONDS - START_TIME))
74
+ if [ $ELAPSED -gt $TIMEOUT ]; then
75
+ echo "Timeout waiting for services after ${TIMEOUT} seconds"
76
+ exit 1
77
+ fi
78
+
79
+ # Check if processes are still running
80
+ if ! kill -0 $GEMMA_PID 2>/dev/null; then
81
+ echo "Gemma model process died."
82
+ exit 1
83
+ fi
84
+ if ! kill -0 $EMBEDDINGS_PID 2>/dev/null; then
85
+ echo "Embeddings model process died."
86
+ exit 1
87
+ fi
88
+
89
+ echo "Waiting for services... (${ELAPSED}s elapsed, PIDs: Gemma=$GEMMA_PID, Embeddings=$EMBEDDINGS_PID)"
90
+ sleep 2
91
  done
92
 
93
  echo 'Servers are ready!'
94
 
95
  echo 'All services are ready!'
96
 
97
+ # Ensure we have the same environment as the base image.
98
  export PNPM_HOME=/root/.local/share/pnpm
99
  export PATH=$PNPM_HOME/bin:$PATH
100
  cd /base # Ensure we're in the correct directory
101
 
 
 
 
 
102
  ls -la prisma/
 
 
103
  ls -la $PNPM_HOME
 
104
  ls -la $PNPM_HOME/global/5/node_modules/@prisma || echo "No global prisma found"
 
105
  prisma -v
 
106
 
107
+ # Now execute the entrypoint with the proper environment.
108
  echo "Executing: /base/bin/entrypoint.sh node /base/app/main.bundle.js"
109
+ set -x # Enable command tracing.
110
  exec /base/bin/entrypoint.sh node /base/app/main.bundle.js