improve docker compose setup
This commit is contained in:
@@ -1,57 +1,81 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
set -euo pipefail
|
||||
|
||||
# Start HTTP→SOCKS5 proxy bridge if SOCKS5 upstream is configured
|
||||
if [ -n "$SOCKS5_PROXY_HOST" ]; then
|
||||
echo "[entrypoint] Starting proxy bridge (HTTP :8899 → SOCKS5 $SOCKS5_PROXY_HOST:${SOCKS5_PROXY_PORT:-1002})..."
|
||||
if [ -z "$IAAI_PROXY_SERVER" ]; then
|
||||
is_worker_command() {
|
||||
local joined="$*"
|
||||
case "$joined" in
|
||||
*"celery -A iaai_scraper.worker.celery_app worker"*|*" celery -A iaai_scraper.worker.celery_app worker"*)
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
return 1
|
||||
}
|
||||
|
||||
is_scrape_cli_command() {
|
||||
local joined="$*"
|
||||
case "$joined" in
|
||||
*"collect-listing"*|*"scrape-vehicle"*|*"sync-vehicle"*|*"sync-listing"*)
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
return 1
|
||||
}
|
||||
|
||||
needs_browser_runtime() {
|
||||
is_worker_command "$@" || is_scrape_cli_command "$@"
|
||||
}
|
||||
|
||||
start_proxy_bridge_if_needed() {
|
||||
if ! needs_browser_runtime "$@"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [ -z "${SOCKS5_PROXY_HOST:-}" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "[entrypoint] Starting proxy bridge (HTTP :8899 → SOCKS5 ${SOCKS5_PROXY_HOST}:${SOCKS5_PROXY_PORT:-1002})..."
|
||||
|
||||
if [ -z "${IAAI_PROXY_SERVER:-}" ]; then
|
||||
export IAAI_PROXY_SERVER="http://127.0.0.1:8899"
|
||||
elif [ "$IAAI_PROXY_SERVER" = "http://localhost:8899" ]; then
|
||||
elif [ "${IAAI_PROXY_SERVER}" = "http://localhost:8899" ]; then
|
||||
export IAAI_PROXY_SERVER="http://127.0.0.1:8899"
|
||||
fi
|
||||
echo "[entrypoint] Using browser proxy: $IAAI_PROXY_SERVER"
|
||||
|
||||
echo "[entrypoint] Using browser proxy: ${IAAI_PROXY_SERVER}"
|
||||
python -m iaai_scraper.proxy_bridge &
|
||||
BRIDGE_PID=$!
|
||||
sleep 1
|
||||
if ! kill -0 $BRIDGE_PID 2>/dev/null; then
|
||||
|
||||
if ! kill -0 "${BRIDGE_PID}" 2>/dev/null; then
|
||||
echo "[entrypoint] ERROR: iaai_scraper.proxy_bridge failed to start"
|
||||
exit 1
|
||||
fi
|
||||
echo "[entrypoint] Proxy bridge started (PID $BRIDGE_PID)"
|
||||
fi
|
||||
|
||||
# Xvfb needed only for worker (Playwright) — not for API or beat
|
||||
NEEDS_XVFB=false
|
||||
case "$1" in
|
||||
celery*|python*main*)
|
||||
NEEDS_XVFB=true
|
||||
;;
|
||||
*)
|
||||
# Check if any arg contains "worker"
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
*worker*) NEEDS_XVFB=true; break ;;
|
||||
esac
|
||||
done
|
||||
;;
|
||||
esac
|
||||
echo "[entrypoint] Proxy bridge started (PID ${BRIDGE_PID})"
|
||||
}
|
||||
|
||||
start_xvfb_if_needed() {
|
||||
if ! needs_browser_runtime "$@"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [ "$NEEDS_XVFB" = "true" ]; then
|
||||
# IAAI blocks headless Chromium on Linux; run headed via Xvfb virtual display
|
||||
export DISPLAY=:99
|
||||
export IAAI_HEADLESS=false
|
||||
|
||||
if [ -S /tmp/.X11-unix/X99 ] || [ -f /tmp/.X99-lock ]; then
|
||||
echo "[entrypoint] Reusing existing Xvfb on DISPLAY=${DISPLAY}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
Xvfb :99 -screen 0 1920x1080x24 -nolisten tcp &
|
||||
XVFB_PID=$!
|
||||
sleep 0.5
|
||||
echo "[entrypoint] Xvfb started (PID $XVFB_PID, DISPLAY=$DISPLAY)"
|
||||
fi
|
||||
echo "[entrypoint] Xvfb started (PID ${XVFB_PID}, DISPLAY=${DISPLAY})"
|
||||
}
|
||||
|
||||
# Run Alembic migrations (only for API service, skip for worker/beat)
|
||||
case "$1" in
|
||||
uvicorn*)
|
||||
echo "[entrypoint] Running Alembic migrations..."
|
||||
alembic upgrade head || echo "[entrypoint] WARNING: Alembic migration failed, continuing..."
|
||||
;;
|
||||
esac
|
||||
start_proxy_bridge_if_needed "$@"
|
||||
start_xvfb_if_needed "$@"
|
||||
|
||||
exec "$@"
|
||||
|
||||
Reference in New Issue
Block a user