HeartbeatController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Http\Controllers\V3;
  3. use App\Exceptions\BusinessException;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Resources\V3\HeartbeatResource;
  6. class HeartbeatController extends Controller
  7. {
  8. /**
  9. * 心跳
  10. *
  11. * 取代 `GET /v2/heartbeat`(`HeartbeatController@index`)。v2 那份在
  12. * dashboard-v4 下线前原样保留,不要改动。
  13. *
  14. * **这不是健康检查。** 它不查数据库、不查 OpenSearch / RabbitMQ / Cache,
  15. * 只回答一件事:进程还在服务吗、是不是被运维停机了。供前端与负载均衡做
  16. * 存活探测(liveness)用,开销接近零,可以高频调用。
  17. *
  18. * 真正的健康检查是 `GET /v2/health-check`(会逐项探测外部依赖并返回 checks
  19. * 明细),将来单独迁为 `/v3/health-check`,与本端点是两件事,不要混用。
  20. *
  21. * 仓库根目录存在 `.stop` 文件时返回 503(运维停机开关),此时响应体是
  22. * RFC 9457 Problem Details 而非正常结构。
  23. *
  24. * @unauthenticated
  25. *
  26. * @responseStatus 503 服务已进入停机维护状态
  27. */
  28. public function show(): HeartbeatResource
  29. {
  30. if (file_exists(base_path('.stop'))) {
  31. // 用 BusinessException 而不是 abort(503):兜底处理器会屏蔽 5xx 的 detail
  32. // (防止泄露内部错误),而停机是预期状态,维护文案必须原样送到客户端
  33. throw new BusinessException(__('site.maintenance'), 503, 'maintenance');
  34. }
  35. return HeartbeatResource::make([
  36. 'status' => 'ok',
  37. 'checked_at' => now(),
  38. ]);
  39. }
  40. }