fix: health.php respuesta instantánea — sin DB ni Redis

Versión anterior intentaba conectar a DB (g1286...) y Redis (zqmfom0...)
que colgaban esperando conexión → todos los workers PHP-FPM bloqueados →
nginx acepta TCP pero no responde HTTP → healthcheck timeout → unhealthy.
This commit is contained in:
Lizandro Guarnizo
2026-03-12 17:04:40 -05:00
parent 6807da8a2b
commit d6d312f72f
+5 -95
View File
@@ -1,99 +1,9 @@
<?php
/**
* Health check endpoint para Docker
* Verifica que todos los servicios estén funcionando
* Health check endpoint — respuesta instantánea.
* NO conecta a DB ni Redis (esas conexiones bloquean PHP-FPM y causan
* healthcheck timeout → contenedor unhealthy → Traefik "no available server").
*/
// Cargar autoload solo si existe (en desarrollo podría no estar)
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
header('Content-Type: application/json');
$health = [
'status' => 'healthy',
'timestamp' => date('Y-m-d H:i:s'),
'checks' => []
];
// 1. Check PHP
$health['checks']['php'] = [
'status' => 'ok',
'version' => PHP_VERSION
];
// 2. Check Database
try {
require_once __DIR__ . '/config/config.php';
$db = Database::getInstance();
$result = $db->query("SELECT 1")->fetch();
$health['checks']['database'] = [
'status' => $result ? 'ok' : 'warning',
'host' => DB_HOST
];
} catch (Exception $e) {
// DB caída = warning, no unhealthy — Traefik debe seguir ruteando
// (mejor mostrar error 500 en la app que "no available server")
$health['checks']['database'] = [
'status' => 'warning',
'error' => $e->getMessage()
];
}
// 3. Check Redis
try {
$redis = new Predis\Client([
'scheme' => getenv('REDIS_SCHEME') ?: 'tcp',
'host' => getenv('REDIS_HOST') ?: 'redis',
'port' => getenv('REDIS_PORT') ?: 6379,
]);
$ping = $redis->ping();
$health['checks']['redis'] = [
'status' => ((string)$ping === 'PONG' || $ping === true) ? 'ok' : 'warning',
'host' => getenv('REDIS_HOST') ?: '127.0.0.1'
];
} catch (Exception $e) {
// Redis es opcional — no marcar el app como unhealthy si Redis falla
$health['checks']['redis'] = [
'status' => 'warning',
'error' => $e->getMessage()
];
}
// 4. Check logs directory
$health['checks']['logs'] = [
'status' => is_writable(__DIR__ . '/logs') ? 'ok' : 'error',
'writable' => is_writable(__DIR__ . '/logs')
];
// 5. Check uploads directory
$health['checks']['uploads'] = [
'status' => is_writable(__DIR__ . '/uploads') ? 'ok' : 'error',
'writable' => is_writable(__DIR__ . '/uploads')
];
// 6. Check Queue stats (opcional)
try {
require_once __DIR__ . '/queue/RedisQueue.php';
$queue = new WhatsApp\Queue\RedisQueue();
$stats = $queue->getStats();
$health['checks']['queue'] = [
'status' => 'ok',
'stats' => $stats
];
} catch (Exception $e) {
$health['checks']['queue'] = [
'status' => 'warning',
'error' => $e->getMessage()
];
}
// HTTP status code
http_response_code($health['status'] === 'healthy' ? 200 : 503);
echo json_encode($health, JSON_PRETTY_PRINT);
http_response_code(200);
echo json_encode(['status' => 'healthy', 'php' => PHP_VERSION, 'ts' => time()]);