HealthCheckController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. ],
  75. $healthy ? 200 : 500,
  76. ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
  77. JSON_UNESCAPED_UNICODE
  78. );
  79. }
  80. /**
  81. * Store a newly created resource in storage.
  82. *
  83. * @param \Illuminate\Http\Request $request
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function store(Request $request)
  87. {
  88. //
  89. }
  90. /**
  91. * Display the specified resource.
  92. *
  93. * @param int $id
  94. * @return \Illuminate\Http\Response
  95. */
  96. public function show($id)
  97. {
  98. //
  99. }
  100. /**
  101. * Update the specified resource in storage.
  102. *
  103. * @param \Illuminate\Http\Request $request
  104. * @param int $id
  105. * @return \Illuminate\Http\Response
  106. */
  107. public function update(Request $request, $id)
  108. {
  109. //
  110. }
  111. /**
  112. * Remove the specified resource from storage.
  113. *
  114. * @param int $id
  115. * @return \Illuminate\Http\Response
  116. */
  117. public function destroy($id)
  118. {
  119. //
  120. }
  121. }