diff --git a/docker-compose.yml b/docker-compose.yml index 6d87e4b..c2e80e2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,6 +16,12 @@ services: depends_on: db: condition: service_healthy + healthcheck: + test: ["CMD-SHELL", "php /var/www/html/health.php --check"] + interval: 10s + timeout: 5s + retries: 10 + start_period: 30s db: image: mariadb:11.4 diff --git a/health.php b/health.php new file mode 100644 index 0000000..27ffd1e --- /dev/null +++ b/health.php @@ -0,0 +1,68 @@ +connect_error) { + error_log("Health check database failed ({$label}): " . $connection->connect_error); + return [ + 'name' => $label, + 'status' => 'error', + ]; + } + + $ok = $connection->query('SELECT 1') !== false; + if (!$ok) { + error_log("Health check query failed ({$label}): " . $connection->error); + } + + $connection->close(); + + return [ + 'name' => $label, + 'status' => $ok ? 'ok' : 'error', + ]; +} + +$project01User = env_value('APP_DB_USER') ?: env_value('DB_USER', 'root'); +$project01Pass = env_value('APP_DB_PASSWORD') ?: env_value('DB_PASS', ''); + +$checks = [ + check_database('project_01', env_value('DB_01_NAME', 'db_webgis_01'), $project01User, $project01Pass), + check_database('poverty_mapping', env_value('DB_WEBGIS_NAME', 'db_webgis')), +]; + +$healthy = true; +foreach ($checks as $check) { + if ($check['status'] !== 'ok') { + $healthy = false; + break; + } +} + +http_response_code($healthy ? 200 : 500); +header('Content-Type: application/json; charset=utf-8'); + +echo json_encode([ + 'status' => $healthy ? 'ok' : 'error', + 'checks' => $checks, +], JSON_UNESCAPED_SLASHES); + +if (PHP_SAPI === 'cli') { + echo PHP_EOL; + exit($healthy ? 0 : 1); +}