89 lines
3.2 KiB
PHP
89 lines
3.2 KiB
PHP
<?php
|
|
// ride_server/intaleq/ride/rides/getRideStatus.php
|
|
// Serves https://rides.intaleq.xyz/intaleq/ride/rides/getRideStatus.php
|
|
// Same response contract as the legacy backend/ride/rides/getRideStatus.php:
|
|
// {"status":"success","data":"<ride status string>"}
|
|
//
|
|
// Reads Redis truth via loction_server's internal HTTP API first (same
|
|
// "ask location server, fall back to DB" shape as backend/ride/location/get.php);
|
|
// on any miss/timeout/error, falls back to a direct MySQL SELECT so behavior
|
|
// never regresses to "no answer."
|
|
|
|
require_once __DIR__ . '/../../connect.php';
|
|
|
|
$id = filterRequest("id");
|
|
$rideId = (int) $id;
|
|
|
|
if (empty($id) || $rideId <= 0) {
|
|
printFailure("Missing ride ID.");
|
|
exit;
|
|
}
|
|
|
|
// ── 1. Try Redis via the location server's internal HTTP API ──────────────
|
|
$status = null;
|
|
|
|
$locationServerUrl = getenv('LOCATION_SERVER_URL') ?: 'http://nginx/loction_server/driver_socket.php';
|
|
$internalKey = getInternalSocketKey();
|
|
|
|
if ($internalKey) {
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $locationServerUrl);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
|
'action' => 'get_ride_state',
|
|
'ride_id' => $rideId,
|
|
]));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
// Foreground/synchronous read — the answer IS the response, so this is
|
|
// deliberately longer than the 200-500ms fire-and-forget write timeouts
|
|
// used elsewhere in this codebase.
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 500);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1500);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["x-internal-key: $internalKey"]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curlErr = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if (!$curlErr && $httpCode === 200 && $response) {
|
|
$json = json_decode($response, true);
|
|
if (is_array($json) && ($json['status'] ?? false) === true
|
|
&& !empty($json['data']['status'])) {
|
|
$status = $json['data']['status'];
|
|
}
|
|
} else {
|
|
error_log("[getRideStatus] location-server read miss/error for ride=$rideId: "
|
|
. ($curlErr ?: "HTTP $httpCode"));
|
|
}
|
|
} else {
|
|
error_log("[getRideStatus] internal key not configured — skipping Redis read, using DB fallback.");
|
|
}
|
|
|
|
// ── 2. Fallback: direct MySQL read (identical query to the legacy file) ───
|
|
if ($status === null) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT `status` FROM `ride` WHERE `id` = :id");
|
|
$stmt->bindParam(':id', $rideId, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if ($row && isset($row['status'])) {
|
|
$status = $row['status'];
|
|
}
|
|
} catch (PDOException $e) {
|
|
error_log("[getRideStatus] DB fallback error for ride=$rideId: " . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode(["status" => "failure", "message" => "An internal error occurred."]);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if ($status !== null) {
|
|
echo json_encode([
|
|
"status" => "success",
|
|
"data" => $status
|
|
]);
|
|
} else {
|
|
printFailure("Ride not found.");
|
|
}
|