#!/bin/sh set -e # The notebook runner shares the image but runs with no network, a read-only # system and only the jobs folder (models.reports.notebook_jobs): it never touches /data. if [ "$1" = "notebooks" ]; then exec /app/WebApi/.venv/bin/python -m models notebooks serve fi mkdir -p /data # SQLite won't create a missing parent directory for its database file. for url in "$CACU_DATABASE_URL" "$CAMA_DATABASE_URL" "$TEA_DATABASE_URL" "$GOVERNANCE_DATABASE_URL"; do case "$url" in sqlite:///*) mkdir -p "$(dirname "${url#sqlite:///}")" ;; esac done cd /app/WebApi # dbt's manifest for the deployment's engine: the image's is SQLite's; on # another engine it's compiled now, against the configured databases, into # /data (webapi.dbt_manifest prints the path to use, or nothing to keep the # image's). if compiled="$(uv run --no-sync python -m webapi.dbt_manifest)"; then [ -n "$compiled" ] && export DBT_MANIFEST="$compiled" else echo "dbt manifest: compilation failed; the measure routes will stay unregistered" >&2 fi # The live indicator worker (webapi.recomputation) shares the image; the # API container runs the migrations, so the worker starts after it. if [ "$1" = "indicadores" ]; then exec uv run --no-sync python -m webapi.recomputation fi # One-shot training of the CaCu model on the loaded data (compose profile # `entrenar`): the trained version goes into production and the worker's next # cycle estimates with it. `--source dbt` (dbt's cohort, not the kit's numbered # builds) is required: a PROD run needs its cohort build named (ML-03), and # --export --source dbt is the only way to do that without knowing the build # number ahead of time; it also makes the promotion gate's cohort-link check # portable (tag-based, no live lookup of a local build row), unlike the kit's. if [ "$1" = "entrenar" ]; then exec uv run --no-sync python -m models run cacu cin2_logistic --export --source dbt --manifest "$DBT_MANIFEST" \ --environment prod --train --promote fi # --no-sync: the environment was synced at build time; syncing at startup # would try to reach the package index (and pull dev deps). for domain in cacu cama tea governance; do uv run --no-sync alembic -c "/app/Persistance/persistance/$domain/alembic.ini" upgrade head done PORT="${PORT:-5000}" # gunicorn, not Flask's development server. Long timeout: OCR over a # multi-page scanned PDF runs inside a single request. exec uv run --no-sync gunicorn \ --bind "0.0.0.0:${PORT}" \ --workers "${WEBAPI_WORKERS:-2}" \ --timeout "${WEBAPI_TIMEOUT:-600}" \ --access-logfile - \ "webapi:create_app()"