90 lines
2.4 KiB
Bash
90 lines
2.4 KiB
Bash
#!/bin/bash
|
||
set -euo pipefail
|
||
|
||
is_worker_command() {
|
||
local joined="$*"
|
||
case "$joined" in
|
||
*"celery -A mobilede_scraper.worker.celery_app worker"*|*" celery -A mobilede_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 "${MOBILEDE_PROXY_SERVER:-}" ]; then
|
||
export MOBILEDE_PROXY_SERVER="http://127.0.0.1:8899"
|
||
elif [ "${MOBILEDE_PROXY_SERVER}" = "http://localhost:8899" ]; then
|
||
export MOBILEDE_PROXY_SERVER="http://127.0.0.1:8899"
|
||
fi
|
||
|
||
echo "[entrypoint] Using browser proxy: ${MOBILEDE_PROXY_SERVER}"
|
||
python -m mobilede_scraper.proxy_bridge &
|
||
BRIDGE_PID=$!
|
||
sleep 1
|
||
|
||
if ! kill -0 "${BRIDGE_PID}" 2>/dev/null; then
|
||
echo "[entrypoint] ERROR: mobilede_scraper.proxy_bridge failed to start"
|
||
exit 1
|
||
fi
|
||
|
||
echo "[entrypoint] Proxy bridge started (PID ${BRIDGE_PID})"
|
||
}
|
||
|
||
start_self_heal_watchdog_if_worker() {
|
||
if ! is_worker_command "$@"; then
|
||
return 0
|
||
fi
|
||
|
||
# После reboot/restart контейнера файловая система контейнера сохраняется,
|
||
# поэтому stale pidfile может остаться от прошлого запуска и блокировать старт Celery.
|
||
# Удаляем его перед запуском worker-процесса.
|
||
rm -f /tmp/celery-worker.pid
|
||
|
||
if [ "${MOBILEDE_SELF_HEAL_ENABLED:-true}" = "false" ]; then
|
||
echo "[entrypoint] Self-heal watchdog disabled"
|
||
return 0
|
||
fi
|
||
|
||
echo "[entrypoint] Starting self-heal watchdog for worker..."
|
||
python -m mobilede_scraper.worker.self_heal &
|
||
SELF_HEAL_PID=$!
|
||
sleep 1
|
||
|
||
if ! kill -0 "${SELF_HEAL_PID}" 2>/dev/null; then
|
||
echo "[entrypoint] ERROR: self-heal watchdog failed to start"
|
||
exit 1
|
||
fi
|
||
|
||
echo "[entrypoint] Self-heal watchdog started (PID ${SELF_HEAL_PID})"
|
||
}
|
||
|
||
start_proxy_bridge_if_needed "$@"
|
||
start_self_heal_watchdog_if_worker "$@"
|
||
|
||
exec "$@"
|