HealthCheckController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Services\RabbitMQService;
  8. use App\Services\OpenSearchService;
  9. class HealthCheckController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index()
  17. {
  18. //
  19. if (file_exists(base_path('.stop'))) {
  20. return response()->json(
  21. ['createdAt' => now(), 'checks' => []],
  22. 503,
  23. ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
  24. JSON_UNESCAPED_UNICODE
  25. );
  26. }
  27. $checks = [];
  28. $healthy = true;
  29. // Cache
  30. try {
  31. Cache::put('health_check', true, 5);
  32. Cache::get('health_check');
  33. $checks['cache'] = true;
  34. } catch (\Throwable $e) {
  35. $checks['cache'] = false;
  36. $healthy = false;
  37. Log::error('Health check failed: cache', ['error' => $e->getMessage()]);
  38. }
  39. // Database
  40. try {
  41. DB::connection()->getPdo();
  42. $checks['db'] = true;
  43. } catch (\Throwable $e) {
  44. $checks['db'] = false;
  45. $healthy = false;
  46. Log::error('Health check failed: db', ['error' => $e->getMessage()]);
  47. }
  48. // OpenSearch
  49. try {
  50. $service = app(OpenSearchService::class);
  51. [$ok, $msg] = $service->testConnection();
  52. if (!$ok) throw new \Exception($msg);
  53. $checks['opensearch'] = true;
  54. } catch (\Throwable $e) {
  55. $checks['opensearch'] = false;
  56. $healthy = false;
  57. Log::error('Health check failed: opensearch', ['error' => $e->getMessage()]);
  58. }
  59. // RabbitMQ
  60. try {
  61. $service = app(RabbitMQService::class);
  62. $service->isConnected() ? null : throw new \Exception('Not connected');
  63. $checks['rabbitmq'] = true;
  64. } catch (\Throwable $e) {
  65. $checks['rabbitmq'] = false;
  66. $healthy = false;
  67. Log::error('Health check failed: rabbitmq', ['error' => $e->getMessage()]);
  68. }
  69. return response()->json(
  70. [
  71. 'createdAt' => now(),
  72. 'services' => $checks,
  73. 'host' => $_SERVER['HTTP_HOST'],
  74. 'document-root' => $_SERVER['DOCUMENT_ROOT']
  75. ],
  76. $healthy ? 200 : 500,
  77. ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
  78. JSON_UNESCAPED_UNICODE
  79. );
  80. }
  81. /**
  82. * Store a newly created resource in storage.
  83. *
  84. * @param \Illuminate\Http\Request $request
  85. * @return \Illuminate\Http\Response
  86. */
  87. public function store(Request $request)
  88. {
  89. //
  90. }
  91. /**
  92. * Display the specified resource.
  93. *
  94. * @param int $id
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function show($id)
  98. {
  99. //
  100. }
  101. /**
  102. * Update the specified resource in storage.
  103. *
  104. * @param \Illuminate\Http\Request $request
  105. * @param int $id
  106. * @return \Illuminate\Http\Response
  107. */
  108. public function update(Request $request, $id)
  109. {
  110. //
  111. }
  112. /**
  113. * Remove the specified resource from storage.
  114. *
  115. * @param int $id
  116. * @return \Illuminate\Http\Response
  117. */
  118. public function destroy($id)
  119. {
  120. //
  121. }
  122. }