Files
FreshRSS/cli/health.php
Jam Balaya 6351a71938 cli/health.php: output a diagnostic when the health check fails (#8980)
Previously the Docker healthcheck script exited with a bare die(1) /
die(2) and no message, so users only saw 'unhealthy' with no clue why.
Print an informative reason to STDERR before each failing exit (a
connection/HTTP error, or a response that is not a FreshRSS page),
without changing the exit codes relied on by Docker HEALTHCHECK.

For https://github.com/FreshRSS/FreshRSS/issues/8026

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 18:59:30 +02:00

43 lines
1.5 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
declare(strict_types=1);
if (php_sapi_name() !== 'cli') {
echo 'Error: This script may only be invoked from command line!', PHP_EOL;
die(3);
}
$options = getopt('', ['url::', 'connect_timeout::', 'timeout::']);
$address = is_string($options['url'] ?? null) ? $options['url'] : 'http://localhost/api/';
$ch = curl_init($address);
if ($ch === false) {
fwrite(STDERR, 'Error: Failed to initialize cURL!' . PHP_EOL);
die(4);
}
curl_setopt_array($ch, [
CURLOPT_CONNECTTIMEOUT => is_numeric($options['connect_timeout'] ?? null) ? (int)$options['connect_timeout'] : 3,
CURLOPT_TIMEOUT => is_numeric($options['timeout'] ?? null) ? (int)$options['timeout'] : 5,
CURLOPT_ACCEPT_ENCODING => '', //Enable all encodings
CURLOPT_HTTPHEADER => [
'Connection: close',
],
CURLOPT_RETURNTRANSFER => true,
]);
$content = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200 || !is_string($content)) {
$curlError = curl_error($ch);
fwrite(STDERR, 'Error: Health check failed for ' . $address . ': ' .
($curlError !== '' ? $curlError : ('unexpected HTTP status ' . $httpCode)) . PHP_EOL);
die(1);
}
$content = rtrim($content, "\n\r");
if (!str_starts_with($content, '<!DOCTYPE html>') || !str_ends_with($content, '</html>') || !str_contains($content, '/scripts/api.js')) {
fwrite(STDERR, 'Error: Health check failed for ' . $address .
': the response does not look like a FreshRSS page' .
' (check that the URL points to a working FreshRSS instance with the API enabled).' . PHP_EOL);
die(2);
}