#!/usr/bin/env bash set -euo pipefail echo "===== Application Startup at $(date) =====" # start.sh - robust startup flow: start Ollama, wait for readiness, create model (if provided), then run gunicorn APP_DIR=/app MODEL_NAME=aj-mini cd "$APP_DIR" # Check if ollama is installed if ! command -v ollama >/dev/null 2>&1; then echo "Error: ollama binary not found in container. Make sure Ollama was installed." exit 1 fi # Start ollama serve in background echo "Starting ollama serve..." ollama serve > /tmp/ollama.log 2>&1 & OLLAMA_PID=$! echo "Ollama PID: $OLLAMA_PID" # Tail Ollama logs in background for visibility tail -n +1 -f /tmp/ollama.log & # Wait for ollama to be ready (with retries) echo "Waiting for Ollama to be ready (http://localhost:11434)..." for i in {1..60}; do if curl -s http://127.0.0.1:11434/api/tags > /dev/null 2>&1; then echo "Ollama is ready (after $i attempts)!" break fi echo "Waiting for Ollama to start... ($i/60)" sleep 2 if [ $i -eq 60 ]; then echo "Warning: Ollama did not become ready after retries. Check /tmp/ollama.log for details but continuing startup." >&2 fi done # Function to create model with retries create_model() { local tries=0 local max=5 until [ $tries -ge $max ]; do if ollama list | grep -q "^$MODEL_NAME\b"; then echo "Model $MODEL_NAME already exists." return 0 fi echo "Attempting to create model $MODEL_NAME (try $((tries+1))/$max)..." if ollama create "$MODEL_NAME" -f Modelfile-aj-mini-v2 >> /tmp/ollama.log 2>&1; then echo "Model $MODEL_NAME created successfully." return 0 fi tries=$((tries+1)) sleep 3 done echo "Failed to create model $MODEL_NAME after $max attempts. Continuing; API may return errors until model is available." >&2 return 1 } # Create model if Modelfile exists if [ -f "$APP_DIR/Modelfile-aj-mini-v2" ]; then echo "Found Modelfile at $APP_DIR/Modelfile-aj-mini-v2" create_model || true else echo "Modelfile not found at $APP_DIR/Modelfile-aj-mini-v2 - skipping model creation. If you expect a local model, add the Modelfile and push again." >&2 fi # Start Gunicorn for Flask app echo "Starting gunicorn for API..." echo "===== API Server Ready =====" exec gunicorn api_server:app --bind 0.0.0.0:5000 --workers 2 --timeout 120