Files
ai-customer-service/guest-cs-stack/start-chat.sh
T
2026-08-15 14:51:51 +08:00

67 lines
2.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
set -eu
# 强制 production(官方 docker-compose.production.yaml)
export RAILS_ENV=production
export NODE_ENV=production
export INSTALLATION_ENV=docker
export RAILS_SERVE_STATIC_FILES=true
export BUNDLE_WITHOUT="${BUNDLE_WITHOUT:-development:test}"
export EXECJS_RUNTIME="${EXECJS_RUNTIME:-Disabled}"
export APP_DEBUG=false
cd /app
rm -rf /app/tmp/pids/server.pid
rm -rf /app/tmp/cache/*
echo "[guest-chat] Waiting for postgres..."
# shellcheck disable=SC2046
eval $(docker/entrypoints/helpers/pg_database_url.rb)
PG_READY="pg_isready -h $POSTGRES_HOST -p ${POSTGRES_PORT:-5432} -U $POSTGRES_USERNAME"
until $PG_READY; do
sleep 2
done
echo "[guest-chat] Database ready."
echo "[guest-chat] Ensuring Chatwoot DB schema..."
bundle exec rails db:chatwoot_prepare
# 同容器内 Flask 在 7788;避免 host.docker.internal 走 IPv6 失败
echo "[guest-chat] Fixing Agent Bot webhook to 127.0.0.1:7788..."
bundle exec rails runner "bot=AgentBot.find_by(id:1); if bot; bot.update!(outgoing_url:'http://127.0.0.1:7788/webhook/bot1/chatwoot'); puts \"bot##{bot.id} -> #{bot.outgoing_url}\"; else; puts 'agent_bot#1 missing'; end" || true
# 不做 bundle install / pnpm / vite(development 才会需要)
echo "[guest-chat] production assets: $(ls /app/public/vite/assets 2>/dev/null | wc -l) vite files"
echo "[guest-chat] starting sidekiq (production)..."
bundle exec sidekiq -C config/sidekiq.yml &
SIDEKIQ_PID=$!
echo "[guest-chat] starting rails :3000 internal (production static)..."
bundle exec rails s -p 3000 -b 127.0.0.1 &
RAILS_PID=$!
echo "[guest-chat] starting chat UI :7788 (gunicorn)..."
cd /opt/guest-cs
/opt/guest-cs/.venv/bin/gunicorn --bind 0.0.0.0:7788 --workers 2 --threads 4 --timeout 120 wsgi:application &
FLASK_PID=$!
_term() {
echo "[guest-chat] shutting down..."
kill -TERM "$FLASK_PID" "$RAILS_PID" "$SIDEKIQ_PID" 2>/dev/null || true
wait || true
}
trap _term TERM INT
while true; do
for pid in "$SIDEKIQ_PID" "$RAILS_PID" "$FLASK_PID"; do
if ! kill -0 "$pid" 2>/dev/null; then
echo "[guest-chat] process $pid exited"
_term
exit 1
fi
done
sleep 2
done