Files
CFDivePlatform/docker/php/docker-entrypoint.sh
T
a620906209 07618fb1b3
Run Tests / test (pull_request) Successful in 1m56s
perf(docker): OPcache revalidate 調校 + composer 重裝條件修正 + nginx healthcheck 修復
實測修正根因:fpm 的 OPcache 本來就啟用(初版診斷是 CLI 假象),
真凶是 revalidate_freq=2 在 Windows bind mount 上每 2 秒重新 stat
全部腳本——窗口內 0.23s、窗口外 2.5s。

- local.ini:revalidate_freq 2→30、memory 192M、max_files 20000
  (程式碼變更最多 30s 生效,立即生效用 kill -USR2 1 平滑重載)
- entrypoint:composer 重裝改用 lock 內容比對,git 分支操作不再
  觸發開機重裝(原 mtime 比對造成數分鐘 502 窗口)
- healthcheck:localhost→127.0.0.1(busybox wget 先解析 ::1 而
  nginx 只 listen IPv4,導致永遠 unhealthy)

驗收:穩態 0.20~0.27s(修復前 2.3~2.5s,約 10 倍);nginx healthy。
量測數據已寫回優化計畫文檔。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 23:15:55 +08:00

67 lines
2.3 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/bash
set -e
echo "=== CFDivePlatform 容器初始化開始 ==="
# 確保目錄與權限(php-fpm 啟動前必須完成)
[ ! -d "/var/www/storage" ] && mkdir -p /var/www/storage
[ ! -d "/var/www/bootstrap/cache" ] && mkdir -p /var/www/bootstrap/cache
chown -R www-data:www-data /var/www
chmod -R 775 /var/www/storage /var/www/bootstrap/cache
# 確保 .env 存在
if [ ! -f .env ]; then
cp .env.example .env
php artisan key:generate
fi
# 強制 DB_HOST=dbDocker service name,不能用 localhost
php -r "
\$env = file_get_contents('/var/www/.env');
\$env = preg_replace('/^DB_HOST=.*$/m', 'DB_HOST=db', \$env);
file_put_contents('/var/www/.env', \$env);
"
# Composer 依賴(vendor 不存在或 lock 內容變更時才裝)
# 用內容比對而非 mtimegit checkout/pull 會更新 composer.json 的 mtime
# mtime 比對會讓每次分支操作後的開機都重跑 autoload 生成(bind mount 上耗時數分鐘、期間全站 502)
if [ -f "composer.lock" ] && { [ ! -d "vendor" ] || ! cmp -s composer.lock vendor/.composer.lock.installed; }; then
composer install --no-scripts --optimize-autoloader
cp composer.lock vendor/.composer.lock.installed
fi
# 背景執行:等 MySQL → migration → cache clear → storage link → swagger
# php-fpm 不等這些完成就先啟動,避免重啟時 CORS 502
(
echo "⏳ [背景] 等待 MySQL..."
COUNT=0
until mysqladmin ping -h"db" -u"${DB_USERNAME:-cfdiveuser}" -p"${DB_PASSWORD}" --silent 2>/dev/null || [ $COUNT -ge 30 ]; do
sleep 2
COUNT=$((COUNT+1))
done
echo "🗄️ [背景] 執行 migration..."
php artisan migrate --force || echo "⚠️ migration 失敗"
echo "🧹 [背景] 清除 Laravel 緩存..."
php artisan config:clear || true
php artisan cache:clear || true
php artisan route:clear || true
php artisan view:clear || true
echo "🔗 [背景] storage:link..."
php artisan storage:link --force || true
if php -r "echo class_exists('L5Swagger\\L5SwaggerServiceProvider') ? 'yes' : 'no';" 2>/dev/null | grep -q 'yes'; then
php artisan l5-swagger:generate || true
fi
echo "✅ [背景] 初始化完成"
) &
# 啟動 cronLaravel Scheduler
service cron start || cron || true
echo "🚀 啟動 php-fpm..."
exec "$@"