#!/bin/bash

# 队列处理器健康检查脚本
# 用于Docker健康检查

set -e

# 检查队列进程是否运行
if ! pgrep -f "queue:work" > /dev/null; then
    echo "队列处理器进程未运行"
    exit 1
fi

# 检查Laravel应用是否可以连接到数据库和Redis
if ! php -r "
try {
    require_once '/var/www/html/vendor/autoload.php';
    \$app = require_once '/var/www/html/bootstrap/app.php';
    \$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
    
    // 检查数据库连接
    \Illuminate\Support\Facades\DB::connection()->getPdo();
    
    // 检查Redis连接
    if (config('cache.default') === 'redis') {
        \Illuminate\Support\Facades\Cache::store('redis')->put('queue_health_check', 'ok', 10);
        \Illuminate\Support\Facades\Cache::store('redis')->forget('queue_health_check');
    }
    
    echo 'OK';
} catch (Exception \$e) {
    echo 'ERROR: ' . \$e->getMessage();
    exit(1);
}
"; then
    echo "队列处理器依赖服务检查失败"
    exit 1
fi

echo "队列处理器健康检查通过"
exit 0