Dev #9
+3
-88
@@ -3,111 +3,26 @@ set -euo pipefail
|
|||||||
|
|
||||||
cd /var/www/html
|
cd /var/www/html
|
||||||
|
|
||||||
# region agent log
|
|
||||||
fc_debug_log() {
|
|
||||||
local hypothesis="$1"
|
|
||||||
local message="$2"
|
|
||||||
local data_json="${3:-{}}"
|
|
||||||
local data_b64
|
|
||||||
data_b64=$(printf '%s' "$data_json" | base64 | tr -d '\n')
|
|
||||||
FC_DEBUG_HYPOTHESIS="$hypothesis" FC_DEBUG_MESSAGE="$message" FC_DEBUG_DATA_B64="$data_b64" php -r '
|
|
||||||
$json = base64_decode(getenv("FC_DEBUG_DATA_B64") ?: "");
|
|
||||||
$data = json_decode($json, true);
|
|
||||||
if (!is_array($data) && str_ends_with($json, "}}")) {
|
|
||||||
$data = json_decode(substr($json, 0, -1), true);
|
|
||||||
}
|
|
||||||
$payload = [
|
|
||||||
"sessionId" => "885d37",
|
|
||||||
"runId" => "coolify-bad-gateway",
|
|
||||||
"hypothesisId" => getenv("FC_DEBUG_HYPOTHESIS") ?: "",
|
|
||||||
"location" => "docker/entrypoint.sh",
|
|
||||||
"message" => getenv("FC_DEBUG_MESSAGE") ?: "",
|
|
||||||
"data" => [
|
|
||||||
"raw_json" => $json,
|
|
||||||
"parsed" => is_array($data) ? $data : null,
|
|
||||||
],
|
|
||||||
"timestamp" => (int) round(microtime(true) * 1000),
|
|
||||||
];
|
|
||||||
$line = json_encode($payload, JSON_UNESCAPED_SLASHES) . PHP_EOL;
|
|
||||||
@file_put_contents("/var/www/html/debug-885d37.log", $line, FILE_APPEND);
|
|
||||||
fwrite(STDERR, "agent_debug " . $line);
|
|
||||||
'
|
|
||||||
}
|
|
||||||
# endregion
|
|
||||||
|
|
||||||
echo "[flixcooks] Waiting for database..."
|
echo "[flixcooks] Waiting for database..."
|
||||||
TRIES=0
|
TRIES=0
|
||||||
MAX_TRIES="${DB_WAIT_MAX_TRIES:-30}"
|
MAX_TRIES="${DB_WAIT_MAX_TRIES:-30}"
|
||||||
|
|
|||||||
start_data=$(php -r 'echo json_encode([
|
|
||||||
"database_url_present" => getenv("DATABASE_URL") !== false && getenv("DATABASE_URL") !== "",
|
|
||||||
"run_db_seed" => getenv("RUN_DB_SEED") ?: "",
|
|
||||||
"db_wait_max_tries" => getenv("DB_WAIT_MAX_TRIES") ?: "30",
|
|
||||||
"entrypoint_args" => array_slice($argv, 1),
|
|
||||||
"schema_file_exists" => file_exists("/var/www/html/scripts/schema.sql"),
|
|
||||||
"seed_file_exists" => file_exists("/var/www/html/data/recipes.json"),
|
|
||||||
]);' -- "$@")
|
|
||||||
fc_debug_log "H1,H2,H4" "entrypoint started" "$start_data"
|
|
||||||
|
|
||||||
until db_check_output=$(php scripts/db-check.php 2>&1); do
|
until php scripts/db-check.php >/dev/null 2>&1; do
|
||||||
TRIES=$((TRIES + 1))
|
TRIES=$((TRIES + 1))
|
||||||
fail_data=$(php -r 'echo json_encode([
|
|
||||||
"attempt" => (int) $argv[1],
|
|
||||||
"max_tries" => (int) $argv[2],
|
|
||||||
"db_check_output" => $argv[3],
|
|
||||||
]);' -- "$TRIES" "$MAX_TRIES" "$db_check_output")
|
|
||||||
fc_debug_log "H1" "db-check failed while waiting" "$fail_data"
|
|
||||||
if [ "$TRIES" -ge "$MAX_TRIES" ]; then
|
if [ "$TRIES" -ge "$MAX_TRIES" ]; then
|
||||||
echo "[flixcooks] Database not reachable after ${MAX_TRIES} attempts." >&2
|
echo "[flixcooks] Database not reachable after ${MAX_TRIES} attempts." >&2
|
||||||
fc_debug_log "H1" "entrypoint exiting because database never became reachable" "$fail_data"
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
success_data=$(php -r 'echo json_encode([
|
|
||||||
"attempts_before_success" => (int) $argv[1],
|
|
||||||
"db_check_output" => $argv[2],
|
|
||||||
]);' -- "$TRIES" "$db_check_output")
|
|
||||||
fc_debug_log "H1" "db-check succeeded" "$success_data"
|
|
||||||
|
|
||||||
echo "[flixcooks] Applying schema..."
|
echo "[flixcooks] Applying schema..."
|
||||||
if schema_output=$(php -r "require 'helpers.php'; require_database(); echo \"schema ok\n\";" 2>&1); then
|
php -r "require 'helpers.php'; require_database(); echo \"schema ok\n\";"
|
||||||
schema_data=$(php -r 'echo json_encode(["schema_output" => $argv[1]]);' -- "$schema_output")
|
|
||||||
fc_debug_log "H2,H4" "schema apply succeeded" "$schema_data"
|
|
||||||
else
|
|
||||||
code=$?
|
|
||||||
schema_data=$(php -r 'echo json_encode(["exit_code" => (int) $argv[1], "schema_output" => $argv[2]]);' -- "$code" "$schema_output")
|
|
||||||
fc_debug_log "H2,H4" "schema apply failed, entrypoint exiting" "$schema_data"
|
|
||||||
echo "$schema_output" >&2
|
|
||||||
exit "$code"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "${RUN_DB_SEED:-false}" = "true" ]; then
|
if [ "${RUN_DB_SEED:-false}" = "true" ]; then
|
||||||
echo "[flixcooks] Seeding recipes from data/recipes.json..."
|
echo "[flixcooks] Seeding recipes from data/recipes.json..."
|
||||||
if seed_output=$(php scripts/db-seed.php 2>&1); then
|
php scripts/db-seed.php
|
||||||
seed_data=$(php -r 'echo json_encode(["seed_output" => $argv[1]]);' -- "$seed_output")
|
|
||||||
fc_debug_log "H4" "seed succeeded" "$seed_data"
|
|
||||||
else
|
|
||||||
code=$?
|
|
||||||
seed_data=$(php -r 'echo json_encode(["exit_code" => (int) $argv[1], "seed_output" => $argv[2]]);' -- "$code" "$seed_output")
|
|
||||||
fc_debug_log "H4" "seed failed, entrypoint exiting" "$seed_data"
|
|
||||||
echo "$seed_output" >&2
|
|
||||||
exit "$code"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
fc_debug_log "H4" "seed skipped" '{"run_db_seed":"false"}'
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "[flixcooks] Starting Apache..."
|
echo "[flixcooks] Starting Apache..."
|
||||||
port_data=$(php -r 'echo json_encode([
|
|
||||||
"env_port" => getenv("PORT") ?: "",
|
|
||||||
"env_host" => getenv("HOST") ?: "",
|
|
||||||
"apache_document_root" => getenv("APACHE_DOCUMENT_ROOT") ?: "",
|
|
||||||
"expected_container_port" => 80,
|
|
||||||
"coolify_hint" => "Application port must be 80",
|
|
||||||
]);')
|
|
||||||
fc_debug_log "H3,H6" "proxy port context before apache start" "$port_data"
|
|
||||||
apache_config_output=$(apache2ctl -S 2>&1 || true)
|
|
||||||
apache_config_data=$(php -r 'echo json_encode(["apache2ctl_S" => $argv[1]]);' -- "$apache_config_output")
|
|
||||||
fc_debug_log "H3,H6" "apache virtualhost config before start" "$apache_config_data"
|
|
||||||
fc_debug_log "H3" "executing web server command" "$(php -r 'echo json_encode(["command" => array_slice($argv, 1)]);' -- "$@")"
|
|
||||||
exec "$@"
|
exec "$@"
|
||||||
|
|||||||
-35
@@ -6,43 +6,13 @@ header('Content-Type: application/json; charset=utf-8');
|
|||||||
|
|
||||||
require __DIR__ . '/config.php';
|
require __DIR__ . '/config.php';
|
||||||
|
|
||||||
// #region agent log
|
|
||||||
function agent_debug_health_request_log(string $hypothesisId, string $message, array $data = []): void {
|
|
||||||
$payload = [
|
|
||||||
'sessionId' => '885d37',
|
|
||||||
'runId' => 'coolify-bad-gateway',
|
|
||||||
'hypothesisId' => $hypothesisId,
|
|
||||||
'location' => 'health.php',
|
|
||||||
'message' => $message,
|
|
||||||
'data' => $data,
|
|
||||||
'timestamp' => (int) round(microtime(true) * 1000),
|
|
||||||
];
|
|
||||||
$line = json_encode($payload, JSON_UNESCAPED_SLASHES) . PHP_EOL;
|
|
||||||
@file_put_contents(__DIR__ . '/debug-885d37.log', $line, FILE_APPEND);
|
|
||||||
@error_log('agent_debug ' . $line);
|
|
||||||
}
|
|
||||||
// #endregion
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Wrapping the Wrapping the `load_recipes()` call in a `try...catch` block is essential for handling potential `DatabaseUnavailableException` errors gracefully, especially on pages that are not intended to show a maintenance page.
|
|||||||
agent_debug_health_request_log('H5,H7', 'health request reached PHP', [
|
|
||||||
'sapi' => PHP_SAPI,
|
|
||||||
'request_uri' => $_SERVER['REQUEST_URI'] ?? '',
|
|
||||||
'http_host' => $_SERVER['HTTP_HOST'] ?? '',
|
|
||||||
'server_port' => $_SERVER['SERVER_PORT'] ?? '',
|
|
||||||
'database_url_getenv_present' => getenv('DATABASE_URL') !== false && getenv('DATABASE_URL') !== '',
|
|
||||||
'database_url_server_present' => isset($_SERVER['DATABASE_URL']) && $_SERVER['DATABASE_URL'] !== '',
|
|
||||||
'dot_env_file_exists' => file_exists(__DIR__ . '/.env'),
|
|
||||||
]);
|
|
||||||
if (!extension_loaded('pdo_pgsql')) {
|
if (!extension_loaded('pdo_pgsql')) {
|
||||||
throw new RuntimeException('pdo_pgsql extension missing');
|
throw new RuntimeException('pdo_pgsql extension missing');
|
||||||
}
|
}
|
||||||
require_once __DIR__ . '/helpers.php';
|
require_once __DIR__ . '/helpers.php';
|
||||||
require_database();
|
require_database();
|
||||||
$response = ['status' => 'ok'];
|
$response = ['status' => 'ok'];
|
||||||
agent_debug_health_request_log('H5,H7', 'health request returning ok', [
|
|
||||||
'exit_code' => 0,
|
|
||||||
'response' => $response,
|
|
||||||
]);
|
|
||||||
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
http_response_code(503);
|
http_response_code(503);
|
||||||
@@ -50,11 +20,6 @@ try {
|
|||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => $e->getMessage(),
|
'message' => $e->getMessage(),
|
||||||
];
|
];
|
||||||
agent_debug_health_request_log('H5,H7', 'health request returning error', [
|
|
||||||
'exit_code' => 1,
|
|
||||||
'response' => $response,
|
|
||||||
'error_class' => get_class($e),
|
|
||||||
]);
|
|
||||||
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user
Making the maximum number of retries configurable via an environment variable is a good flexible design. The default of 30 attempts (with a 2-second sleep) provides a generous 60-second wait, which is generally sufficient. Ensure this timeout is documented or understood by users deploying the application.