heartbeat.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare(strict_types=1);
  3. // POST api/v1/heartbeat.php
  4. // Instance status report. The response doubles as a cheap update check, so a
  5. // cron heartbeat is enough to keep the dashboard current and to learn about a
  6. // pending update in one request.
  7. //
  8. // JSON body: version, php_version, disk_free, pending_migrations, last_backup_at
  9. require_once __DIR__ . "/../../includes/api.php";
  10. require_once __DIR__ . "/../../includes/releases.php";
  11. manageApiRequireMethod("POST");
  12. $instance = manageApiAuthenticate();
  13. $body = manageApiReadJsonBody();
  14. $report = [
  15. "version" => trim((string) ($body["version"] ?? "")),
  16. "php_version" => trim((string) ($body["php_version"] ?? "")),
  17. "disk_free" => (int) ($body["disk_free"] ?? 0),
  18. "pending_migrations" => max(0, (int) ($body["pending_migrations"] ?? 0)),
  19. ];
  20. manageInstanceTouch($instance["id"], $report);
  21. if (isset($body["last_backup_at"])) {
  22. try {
  23. manageInstanceUpdate($instance["id"], [
  24. "last_backup_at" => trim((string) $body["last_backup_at"]),
  25. ]);
  26. } catch (Throwable $exception) {
  27. manageLogError("Heartbeat backup timestamp update failed", [
  28. "instance" => $instance["id"],
  29. "error" => $exception->getMessage(),
  30. ]);
  31. }
  32. }
  33. $latest = manageReleaseLatestVersion();
  34. $updateAvailable = false;
  35. if ($latest !== "" && $report["version"] !== "") {
  36. $updateAvailable = version_compare(
  37. ltrim($latest, "vV"),
  38. ltrim($report["version"], "vV"),
  39. ">",
  40. );
  41. }
  42. manageApiSendJson(200, [
  43. "success" => true,
  44. "instance" => $instance["id"],
  45. "latest" => $latest,
  46. "update_available" => $updateAvailable,
  47. "server_time" => date(DATE_ATOM),
  48. ]);