Files
posthog/bin/docker-server-unit

55 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
set -e
./bin/migrate-check
# To ensure we are able to expose metrics from multiple processes, we need to
# provide a directory for `prometheus_client` to store a shared registry.
export PROMETHEUS_MULTIPROC_DIR=$(mktemp -d)
chmod -R 777 $PROMETHEUS_MULTIPROC_DIR
export PROMETHEUS_METRICS_EXPORT_PORT=8001
export STATSD_PORT=${STATSD_PORT:-8125}
# Dual-mode support: USE_GRANIAN env var switches between Granian and Unit (default)
if [ "${USE_GRANIAN:-false}" = "true" ]; then
echo "🚀 Starting with Granian ASGI server (opt-in via USE_GRANIAN=true)..."
# Granian configuration
export GRANIAN_WORKERS=${GRANIAN_WORKERS:-4}
export GRANIAN_THREADS=2
# Start metrics HTTP server in background on port 8001
python ./bin/granian_metrics.py &
METRICS_PID=$!
# Combined trap: kill metrics server and cleanup temp directory
trap 'kill $METRICS_PID 2>/dev/null; rm -rf "$PROMETHEUS_MULTIPROC_DIR"' EXIT
exec granian \
--interface asgi \
posthog.asgi:application \
--workers $GRANIAN_WORKERS \
--runtime-threads $GRANIAN_THREADS \
--runtime-mode mt \
--loop uvloop \
--host 0.0.0.0 \
--port 8000 \
--log-level warning \
--access-log \
--respawn-failed-workers
else
echo "🔧 Starting with Nginx Unit server (default, stable)..."
# Cleanup temp directory on exit
trap 'rm -rf "$PROMETHEUS_MULTIPROC_DIR"' EXIT
export NGINX_UNIT_PYTHON_PROTOCOL=${NGINX_UNIT_PYTHON_PROTOCOL:-wsgi}
export NGINX_UNIT_APP_PROCESSES=${NGINX_UNIT_APP_PROCESSES:-4}
envsubst < /docker-entrypoint.d/unit.json.tpl > /docker-entrypoint.d/unit.json
# We need to run as --user root so that nginx unit can proxy the control socket for stats
# However each application is run as "nobody"
exec /usr/local/bin/docker-entrypoint.sh unitd --no-daemon --user root
fi